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

}

Hmm maybe your code do like it should, but did you check that they dont colide again? I see that you add random*5 +2 so is it possible that one of the ball be on position 1,1 secound 4,4 first go 2,2 second 3,3 they have "collision" then first go 3,3 second 2,2 another "collision" but it goes so fast you didn't see it?

Is each ball sending a collision message?

Doogledude has got it - ball A collides with ball B, and ball B colides with ball A.

Hello,

Thank you for your answers! If you don't mind, could you tell me where I am going wrong with my understanding of the program. Below is how I see it working:

When the program starts, there are four threads created - main(), ball1, ball2 and the event thread. The paintComponent() is called only in the event thread. So, when the event thread is running and the other threads are waiting their turn, the paintComponent() method should run only once. I fail to see how this method checks for collision of both A with B and B with A when the event thread is running, unless the time slice for this thread is twice the timer delay time.

Thank you!

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.