Hello,
Thank you for the time!
a) I have a Ball Object which implements the Runnable interface and traces the various positions of a ball.
b) I then have a Ball_Bounce JPanel inside a JFrame which creates two instances of the Ball object and then paints them to the JPanel.
As per my understanding, when the main() program in Ball_Bounce.java is started, there a total of three threads running in this program, one for each ball and one for the main(). What I cannot understand is whenever the balls collide, I end up getting the "Collision" message twice even though the collision is checked only in the main() thread.
I would be grateful for an explanation for why I am getting the collision message outputted twice and also any suggestions on how I can improve my design of the program as it feels like I am not doing it the right way.
Thank you!
Program Code:
public class Ball implements Runnable
{
private boolean xUp, yUp, xUp1, yUp1;
private int x, y, xDx, yDy;
private final int MAX_X = 500, MAX_Y = 500;
private boolean flag = true;
private static Thread ball;
public Ball(int xCoordinate, int yCoordinate)
{
x = xCoordinate;
y = yCoordinate;
xUp = false;
yUp = false;
xDx = 1;
yDy = 1;
ball = new Thread(this);
ball.start();
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void run()
{
while ( flag == true )
{
try
{
ball.sleep(12);
}
catch ( InterruptedException exception )
{
System.err.println( exception.toString() );
}
if ( y <= 0 ) {
yUp = true;
yDy = ( int ) ( Math.random() * 5 + 2 );
}
else if ( y >= MAX_Y - 50 ) {
yDy = ( int ) ( Math.random() * 5 + 2 );
yUp = false;
}
if ( x <= 0 ) {
xUp = true;
xDx = ( int ) ( Math.random() * 5 + 2 );
}
else if ( x >= MAX_X - 50 ) {
xUp = false;
xDx = ( int ) ( Math.random() * 5 + 2 );
}
if ( xUp == true )
x += xDx;
else
x -= xDx;
if ( yUp == true )
y += yDy;
else
y -= yDy;
}
}
}[/#]-------------------------------------------------------------------------------------------------------------------------------import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Thread;
public class Ball_Bounce extends JPanel implements ActionListener
{
private Ball ball[] = new Ball[2];
private Timer timer;
private int count = 0;
public Ball_Bounce()
{
timer = new Timer (12, this);
timer.start();
ball[0] = new Ball( 300, 250);
ball[1] = new Ball (450, 450);
}
public void actionPerformed (ActionEvent e)
{
repaint();
}
public void paintComponent( Graphics g )
{
super.paintComponent(g);
for (int i = 0; i < 2; i++)
{
if (i == 0)
{
Color c = Color.RED;
g.setColor (c);
g.fillOval( ball[i].getX(), ball[i].getY(), 50, 50 );
}
if ( i == 1)
{
Color c = Color.BLUE;
g.setColor (c);
g.fillOval( ball[i].getX(), ball[i].getY(), 50, 50 );
}
}
if (Math.abs(ball[0].getX()-ball[1].getX()) <= 50 && Math.abs(ball[0].getY()-ball[1].getY()) <= 50)
{
JOptionPane.showMessageDialog (null, "Collision");
}
}
public static void main (String args [])
{
JFrame f = new JFrame ("Bouncing Ball");
Ball_Bounce b = new Ball_Bounce ();
f.add (b);
f.setSize (500,500);
f.setVisible(true);
}
}