0

I'm trying to use the method addBall to Paint a ball with it's own thread unto the coloredBallPanel

I'm very stuck and would appreiciate any help at all.

Btw I'm trying to make bouncing ball program in which all the balls run on their own separate threads.

public class ColoredBallPanel extends JPanel
{
    Ball ball;
    public ColoredBallPanel()
    {
        ball= new Ball();
    }
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, 499, 300);
//      ball.paint(g2);

    }
}


import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
public class BallsFrame extends JFrame
{
     private ColoredBallPanel coloredBallPanel;
     private final int FRAME_WIDTH = 500;
     private final int FRAME_HEIGHT = 500;
     private int BallDimensionsx =30,BallDimensionsy=30;
     private Ball ball = new Ball();
     public BallsFrame()
     {
         //sets the size of the JFrame
         setSize(FRAME_WIDTH, FRAME_HEIGHT);
         coloredBallPanel = new ColoredBallPanel();//Initialize a Ball panel
         add(coloredBallPanel, BorderLayout.CENTER);//white square in the centre
         addBall(); // add two balls to panel(Doesn't work yet)
         addBall();

     }

    public void addBall()
     {
        Ball ball = new Ball();
         coloredBallPanel.add(ball);

         Runnable r = new ColoredBallRunnable(ball, coloredBallPanel);
         Thread t = new Thread(r);
         t.start();
     }



}





import java.awt.*;

import javax.swing.*;


public class Ball extends JComponent 
{
    private int x=(int) (Math.random()*(500 -1)),
                y =(int) (Math.random()*(300-1)),
                xVelocity=-10,
                yVelocity=10;
    private int width=30,height=30,size =30;
    /**
     * @param args
     */
    public void update()
    {
        x+=xVelocity;
        y+=yVelocity;
        if(x<=0)
        {
            xVelocity =10;
        }
        else if(x+size>=500)
        {
            xVelocity = -10;
        }
        if(y<=0)
        {
            yVelocity =10;
        }
        else if (y+size>=300)
        {
            yVelocity=-10;
        }
    }

    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.GREEN);
        g2.fillOval(x, y, width, height);
    }


}

    import javax.swing.JComponent;
import javax.swing.JPanel;


public class ColoredBallRunnable implements Runnable 
{
    private Ball ball ;
    public ColoredBallRunnable(Ball ball, ColoredBallPanel coloredBallPanel)
    {
        // TODO Auto-generated constructor stub
         ball = new Ball();
        coloredBallPanel = new ColoredBallPanel();
    }


    public void run()
    {
        Ball ball = new Ball();
        while(true)
        {
        ball.update();
        ball.repaint();
        try{
            Thread.sleep(10);
        }catch(InterruptedException e){
            return;
        }
        }
    }

}
timi95
  • 368
  • 6
  • 23
  • 1
    Where's the `ColoredBallRunnable` class? Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Dec 11 '14 at 00:08
  • I've added in the class – timi95 Dec 11 '14 at 00:18

1 Answers1

2

There are so many problems...

  1. Ball is component been added to a Container which is under the control of a LayoutManager, this means that, even if you got Ball to move, you would fighting the layout manager all the time
  2. Ball has no "size" (or position for that matter), so when it is added to the Container, it's sized to it's default size of 0x0, making it, virtually, invisible
  3. Ball is never painted. This is actually for two reasons, but we'll start with the obvious, it doesn't override any valid paint methods that would allow Swing to paint it...if it was larger than 0x0.

Solutions...?

  • Make Ball just a POJO which knows it's size and location (and can update itself if that's what you need)
  • Create a "model" of some kind that can be shared between the view ColoredBallPanel and the controller (the thread). This model should maintain a List of Balls currently available...
  • Allow the ColoredBallPanel to loop through this list of balls and paint them via it's paintComponent method
  • Allow the controller to loop through this list of balls and update them.
  • Synchronise access to the balls list, so neither the view or controller can mess with the list while the other is using it. You might consider making a read-only version of the list for the view, but that might be beyond the scope right now...
  • Call super.paintComponent before you do any custom painting

Perhaps, something more like Java Bouncing Ball

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366