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;
    }
    
}

Recommended Answers

All 5 Replies

They're being removed because you said

if(Balls.get(X).intersects(Balls.get(Y)) == false) {
Balls.remove(X);
}

That doesn't make logical sense to me. It should be if that == true, then remove one. It's going to be false almost all the time. Your statement removes X every time X and Y don't intersect, whereas you stated above that you want to remove X every time X and Y do intersect.

alright i changed that statement to true now and nothing appears at all.

In place of varible Rectangle r
introduce method

public Rectangle getRectangle() {
        return new Rectangle(xCoord, yCoord, Width, Height);
    }

or update, while move() r varible

public boolean intersects(BallImpl ball) {
        return this.getRectangle().intersects(ball.getRectangle());
    }

In method public void checkCollision()
check only ( (n*n)-n) /2 cases

In place of varible Rectangle r
introduce method

public Rectangle getRectangle() {
        return new Rectangle(xCoord, yCoord, Width, Height);
    }

or update, while move() r varible

public boolean intersects(BallImpl ball) {
        return this.getRectangle().intersects(ball.getRectangle());
    }

In method public void checkCollision()
check only ( (n*n)-n) /2 cases

i'm sorry but i didnt understand that at all its only crashing now everytime i change one of the things you said. and i dont get the (n*n) - N/ 2 thing im not sure what you mean

checkCollision() - method
Imagine, inside dipest loop You want print a dot.
Your loops generates n*n dots.

For size of array=2 , result is 2*2=4 values A,B
AA - the same
AB - ok
BA - compared as AB
BB- the same

Example for size of array=4
#+++
-#++
--#+
---#
You want only (n*n)-n) /2 dots (combinatorics)
This method make unnecesary comparision in
Line 71. } else if (this == b) { //suscipious ? (look at String Comparision problem)

And last remark : remove ball gives me Exception in thread "Thread-3" java.util.ConcurrentModificationException
In place of remove method indroduce for ball special flag, void mark() method, and boolean isMarked(). When ball is marked ignore it in display and exclude from all calculations.

use this methods in checkCollision() - method
if (!ballA.isMarked()) { calculate ...
if (!ballB.isMarked()) { calculate ...

if (test for intersection is ok){
ballA.mark();
ballB.mark();
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.