Hello Members,
I have three balls(all JPanels) bouncing in a JFrame. Following are the files:
BouncingBalls.java:
import javax.swing.*;
import java.awt.*;
class BouncingBalls extends JFrame
{
public BouncingBalls()
{
setResizable(false);
setSize(400,400);
Ball ball1 = new Ball();
Ball ball2 = new Ball();
Ball ball3 = new Ball();
ball1.add(ball2);
ball2.add(ball3);
getContentPane().add(ball1);
setVisible(true);
Thread x = new Thread(ball1);
Thread y = new Thread(ball2);
Thread z = new Thread(ball3);
x.start();
y.start();
z.start();
}
public static void main(String[] args)
{
new BouncingBalls();
}
}
Ball.java:
import java.awt.*;
import javax.swing.*;
class Ball extends JPanel implements Runnable
{
private boolean xUp, yUp, bouncing;
private int x, y, xDx, yDy;
private final int MAX_X = 400, MAX_Y = 400;
public Ball()
{
xUp = false;
yUp = false;
xDx = 1;
yDy = 1;
bouncing = true;
setOpaque(false);
setPreferredSize(new Dimension( MAX_X, MAX_Y));
}
public void run()
{
while ( true ) {
try {
Thread.sleep( 20 );
}
catch ( InterruptedException exception ) {
System.err.println( exception.toString() );
}
if ( xUp == true )
x += xDx;
else
x -= xDx;
if ( yUp == true )
y += yDy;
else
y -= yDy;
if ( y <= 0 ) {
yUp = true;
yDy = ( int ) ( Math.random() * 5 + 2 );
}
else if ( y >= MAX_Y - 30 ) {
yDy = ( int ) ( Math.random() * 5 + 2 );
yUp = false;
}
if ( x <= 0 ) {
xUp = true;
xDx = ( int ) ( Math.random() * 5 + 2 );
}
else if ( x >= MAX_X - 30 ) {
xUp = false;
xDx = ( int ) ( Math.random() * 5 + 2 );
}
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if ( bouncing ) {
g.setColor( Color.blue );
g.fillOval( x, y, 30, 30 );
}
}
}
My question:
How do I write a collission check between the three balls given the structure of the above two files? Specifically, in which of the above two files would the function be implemented and what would it's inputs and output be and where would the function be called?
I would be grateful for any input.
Thank you!!
sciprog1