Hello,
I have a program which creates multiple balls and bounces them off when they collide. This works fine.
import java.awt.Rectangle;
public class Ball{
private int x = 0;
private int y = 0;
private int radius;
private int panelwidth = 500;
private int panelheight = 500;
private int xDx = 1;
private int yDy = 1;
private boolean xUp, yUp;
public Ball(int x, int y, int radius){
this.x = x;
this.y = y;
this.radius = radius;
this.xUp = false;
this.yUp = false;
this.xDx = 1;
this.yDy = 1;
}
public void move(){
if ( y <= 0 ) {
yUp = true;
yDy = ( int ) ( Math.random() * 5 + 2 );
}
else if ( y >= this.panelheight - 2* this.radius ) {
yDy = ( int ) ( Math.random() * 5 + 2 );
yUp = false;
}
if ( x <= 0 ) {
xUp = true;
xDx = ( int ) ( Math.random() * 5 + 2 );
}
else if ( x >= this.panelwidth - 2 * this.radius ) {
xUp = false;
xDx = ( int ) ( Math.random() * 5 + 2 );
}
if (xUp)
x += xDx;
else
x -= xDx;
if ( yUp )
y += yDy;
else
y -= yDy;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void changeDirection(Ball b){
if (this.x <= b.x && this.y <= b.y){
this.xUp = false;
this.yUp = false;
b.xUp = true;
b.yUp = true;
}
else if (this.x <= b.x && this.y >= b.y){
this.xUp = false;
this.yUp = true;
b.xUp = true;
b.yUp = false;
}
}
public void setY(int y ){
this.y = y;
}
public int getRadius(){
return this.radius;
}
public Rectangle getBounds(){
return new Rectangle(this.x, this.y, this.radius, this.radius);
}
public boolean collides(Ball b){
return this.getBounds().intersects(b.getBounds());
}
}
import javax.swing.JFrame;
public class Frame{
public static void main( String args[] ) {
JFrame frame = new JFrame( "Bouncing Balls" );
Ball b[] = new Ball[6];
b[0] = new Ball (10, 10,40);
b[1] = new Ball (100, 100, 40);
b[2] = new Ball (20, 188,40);
b[3] = new Ball (144, 100,40);
b[4] = new Ball (220, 138,40);
b[5] = new Ball (14, 10,40);
BallPanel bp = new BallPanel(b);
frame.add( bp );
frame.setSize( 500, 500 );
frame.setVisible( true );
}
}
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Graphics;
import java.awt.Color;
public class BallPanel extends JPanel implements ActionListener {
private int delay = 10;
protected Timer timer;
Ball b[];
public BallPanel(Ball b[]) {
this.b= new Ball[b.length];
for (int i = 0; i <b.length; i++){
this.b[i] = b[i];
}
timer = new Timer(delay, this);
timer.start();
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void paintComponent( Graphics g ) {
super.paintComponent( g );
g.setColor(Color.red);
// move the balls
for (int i = 0; i <b.length; i++){
this.b[i].move();
}
// check for collision and change direction if balls collide
for (int i = 0; i <b.length; i++){
for (int j = 0; j <b.length; j++){
if (i!= j && b[i].collides(b[j])){
b[i].changeDirection(b[j]);
}
}
}
// draw the balls
for (int i = 0; i <b.length; i++){
g.fillOval(this.b[i].getX(), this.b[i].getY() , this.b[i].getRadius(), this.b[i].getRadius());
}
}
}
I would like to add a new ball when two balls collide in the Ball Panel class. When I tried creating a new ball in the nested loop that checks for collision (used an array list), and then called the move and the fill oval methods, the program froze. Can you tell me why? What is the proper way to handle creating a new ball upon collision and make it move etc.?
Thank you