Question on providing access to several objects

Hey All,

I am trying to make several balls moving within a rectangle and bouncing off the wall and other balls. I intended to make 5 balls(lines are below) but never make it show more than one. Please point out what goes wrong or suggest a solution. Thanks.

import java.awt.*;

import javax.swing.*;

import java.util.*;

publicclass moreBouncingBalls{

int diameter;

int deltaX;

int deltaY;

int x;

int y;

int r;

int g;

int b;

publicstaticvoid main(String[] args){

moreBouncingBalls BallA=new moreBouncingBalls();

moreBouncingBalls BallB=new moreBouncingBalls();

moreBouncingBalls BallC=new moreBouncingBalls();

moreBouncingBalls BallD=new moreBouncingBalls();

moreBouncingBalls BallE=new moreBouncingBalls();

BallA. setBall();

BallB. setBall();

BallC. setBall();

BallD. setBall();

BallE. setBall();

}

void setBall (){

Random random =new Random( );

deltaX = random.nextInt(100);

deltaY = random.nextInt(100);

r=random.nextInt(255);

g=random.nextInt(255);

b=random.nextInt(255);

diameter = 10+random.nextInt(41);

go();

}

void go(){

JFrame frame=new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyDrawPanel drawPanel=new MyDrawPanel();

frame.getContentPane().add(drawPanel);

frame.setSize(300,300);

frame.setVisible(true);

while(deltaX!=0&&deltaY!=0){

x=x+deltaX;

y=y+deltaY;

drawPanel.repaint();

try{Thread.sleep(50);

}

catch(Exception ex){}

if(x<=0||x+diameter>300){

deltaX=(deltaX)*(-1);

}

if(y<=0||y+diameter>300){

deltaY=(deltaY)*(-1);

}

}

}

class MyDrawPanelextends JPanel{

publicvoid paintComponent (Graphics a){

a.setColor(Color.white);

a.fillRect(0,0,this.getWidth(),this.getHeight());

a.setColor(new Color(r,g,b));

a.fillOval(x,y,diameter,diameter);

}

}

}

[3914 byte] By [kevinchanga] at [2007-11-27 9:25:45]
# 1
I strongly recommend that you use an array of balls and then loop through the array when initializing the balls AND when drawing the balls.
petes1234a at 2007-7-12 22:22:29 > top of Java-index,Java Essentials,Java Programming...
# 2
Also, I hope that you have your box or JPanel in a separate class from the balls, correct? You probably should keep the ball array separate from the Ball class too (you don't have to, but in my mind it's cleaner this way).
petes1234a at 2007-7-12 22:22:29 > top of Java-index,Java Essentials,Java Programming...
# 3

Don't create a separate gui (JFrame and JPanel) for each ball. Create one gui and several balls. Then paint each ball on the same panel each time they move. The only reason you don't have 5 different frames with their own ball is that the first go method never exits. Try putting a break; at the end of the while loop to see the difference.

You should use a swing timer to schedule the balls movement, it's a cleaner design, and prevents multiple timer threads from spawning, but focus on the other problem first.

hunter9000a at 2007-7-12 22:22:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Your code is confusing in several ways.

First off, class names should always start with a capital letter.

You have two classes here, the frame and the ball data, with the frame containing serveral balls. If either of these is to be an inner class is should be the ball class, which allows it to access anything it needs from the frame class.

Then the array or list of balls goes naturally into the Frame classes fields.

I'd set things up so each ball knows how to draw itself, and the paintComponent method in frame just needs to run down the list of balls, asking each to draw itself.

You could make the ball class implement Runnable, and give a Thread to each.

malcolmmca at 2007-7-12 22:22:29 > top of Java-index,Java Essentials,Java Programming...