I am making a little program where some balls are bouncing inside a box. The logic for that has already been done in a class called Ball.java and balls are bouncing and moving normally.
I want to take it a step further and to draw a rectangle inside of the Jframe and have the ball behave a specific way when there's two inside. There should only be 2 balls moving inside the boundaries of the rectangle. Other balls going in the rectangle should freeze until one of the 2 currently moving balls leaves it. A moving ball exiting the rectangle should freeze unless there is another ball in the box meaning the rectangle should never be empty.
Below is my logic where this should be implemented, I drew the rectangle with g.drawRect(75, 75, 200, 200);
in the paincomponents() method, I have trouble making the balls know they're inside that rectangle.
package bouncingball;
import bouncingball.Ball;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
public class BallPanel extends JPanel {
private Ball ball;
private ArrayList<Ball> balls = new ArrayList();
private int ballsInside = 0;
public BallPanel() {
addMouseListener(new Mouse());
}
//method for creating a new ball
private void newBall(MouseEvent click) {
ball = new Ball(this);
balls.add(ball);
Thread t = new Thread(ball);
t.setPriority(Thread.NORM_PRIORITY);
t.start();
System.out.println("New ball created");
}
public void animate() {
while (true) {
repaint();
}
/* public synchronized void countIn() throws InterruptedException {
while (ballsInside < 2){
wait();
}
ballsInside++;
notifyAll();
}
public synchronized void countMaxIn() throws InterruptedException {
while (ballsInside == 2){
wait();
}
ballsInside--;
notifyAll();
}*/
private class Mouse extends MouseAdapter {
@Override
public void mousePressed(final MouseEvent event) {
newBall(event);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(75, 75, 200, 200);
for (Ball ball : balls) {
if (ball != null) {
ball.draw(g);
}
}
}
}
and my ball class
package bouncingball;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Random;
import java.lang.Runnable;
import javax.swing.JPanel;
import java.util.ArrayList;
public class Ball implements Runnable {
public final static Random random = new Random();
final static int SIZE = 30;
final static int MAX_SPEED = 5;
BallPanel panel = new BallPanel();
private int x;
private int y;
private int dx;
private int dy;
private Color color = Color.BLUE;
//private ArrayList<Ball> balls ;
@Override
public void run() {
while (true) {
move();
try {
Thread.sleep(40); // wake up roughly 25 frames per second
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
public Ball(BallPanel panel) {
this.panel = panel;
x = random.nextInt(panel.getWidth());
y = random.nextInt(panel.getHeight());
dx = random.nextInt(2 * MAX_SPEED) - MAX_SPEED;
dy = random.nextInt(2 * MAX_SPEED) - MAX_SPEED;
}
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x, y, SIZE, SIZE);
}
public void move() {
// check for bounce and make the ball bounce if necessary
//
if (x < 0 && dx < 0) {
//bounce off the left wall
x = 0;
dx = -dx;
}
if (y < 0 && dy < 0) {
//bounce off the top wall
y = 0;
dy = -dy;
}
if (x > panel.getWidth() - SIZE && dx > 0) {
//bounce off the right wall
x = panel.getWidth() - SIZE;
dx = -dx;
}
if (y > panel.getHeight() - SIZE && dy > 0) {
//bounce off the bottom wall
y = panel.getHeight() - SIZE;
dy = -dy;
}
//make the ball move
x += dx;
y += dy;
}
}