ok so i need my code to be able to have the balls on the screen detect collision and remove a ball when they collide but for some reason every time i hit the add a ball button they disappear on me. and they dont detect collision in the first place any help would be wonderful :-)
o P.S. the way we are tackling it is by putting a rectangle around each circle and using the intersects method that comes with rectangles
here's my BallPanel class
package lab5;
import java.awt.*;
import javax.swing.*;
import java.util.*;
/**
* A panel containing two bouncing balls. This panel may be placed in a JFrame.
* @author RejectKid
*/
public class BallPanel extends JPanel {
int balls;
ArrayList<Ball> Balls = new <Ball>ArrayList();
/** Creates a new instance of BallPanel */
public BallPanel(int NumberofBalls) {
super();
balls = NumberofBalls;
}
public void intialize() {
Ball b1 = new Ball(getWidth(), getHeight());
for (int X = 0; X < balls; X++) {
Balls.add(b1);
b1 = new Ball(getWidth(), getHeight());
}
}
/**
Paints the balls at their current positions within the panel.
*/
public void paint(Graphics g) {
super.paint(g);
int X;
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
for (X = 0; X < Balls.size(); X++) {
Ball b1 = Balls.get(X);
b1.paint(g);
}
} // end method paintComponent
/**
Computes the next position for the balls and updates their positions.
*/
public void move() {
checkCollision();
for (int X = 0; X < Balls.size(); X++) {
Ball b1 = Balls.get(X);
b1.move();
}
} // end method move
public void addBall() {
Ball b1 = new Ball(getWidth(), getHeight());
Balls.add(b1);
balls = balls + 1;
repaint();
}
public void checkCollision() {
for (int X = 0; X < Balls.size(); X++) {
for (int Y = 0; Y < Balls.size(); Y++) {
if(Balls.get(X).intersects(Balls.get(Y)) == false) {
Balls.remove(X);
}
}
}
}
}
and here's the Ball class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab5;
import java.awt.*; // Old library classes, you still need them
import java.awt.event.*;
import javax.swing.*; // New library classes, Swing extends AWT
/**
* This class creates a ball that has random values for all attributes about it
* and allows them to move, paint, and get height and width
* @author RejectKid
*/
public class Ball{
private int xCoord;
private int yCoord;
private int Height;
private int Width;
private int Rise;
private int Run;
private int wHeight;
private int wWidth;
private Color randomColor;
private Rectangle r;
public Ball(){
}
public Ball(int wWidth, int wHeight){
// Randomize all values
this.wHeight = wHeight;
this.wWidth = wWidth;
xCoord = (int)(Math.random() * 50);
yCoord = (int)(Math.random() * 50);
Rise = (int)(Math.random() * 10);
Run = (int)(Math.random() * 10);
Width= (int)(Math.random() * 10 + 10);
Height = (int)(Math.random() * 10 + 10);
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
randomColor = new Color(red, green, blue);
r = new Rectangle (xCoord / 2, yCoord / 2, Width, Height);
}
public void move () {
// Move Ball 1
// If ball is approaching a wall, reverse direction
if (xCoord < (0 - Run) || xCoord > (wWidth - Width)) {
Run = -Run;
}
if (yCoord < (0 - Rise) || yCoord > (wHeight - Height)) {
Rise = -Rise;
}
// "Move" ball according to values in rise and run
xCoord += Run;
yCoord += Rise;
}
public void paint(Graphics g){
g.setColor(randomColor);
g.fillOval(xCoord, yCoord, Width, Height);
}
public boolean intersects(Ball b) {
if (this.r.intersects(b.r)) {
return true;
} else if (this == b) {
return false;
}
return false;
}
public int getWidth(){
return Width;
}
public int getHeight(){
return Height;
}
public int getX () {
return xCoord;
}
public int getY () {
return yCoord;
}
}