In java, how do you throw a ball on a panel with every mouse click? Say I pressed the panel 3 times, so there should be 3 balls in the panel.
I have this code:
ball = new Thread() {
public void run() {
while (true) {
x += speedX;
y += speedY;
}
}
};
ball.start();
repaint();
try {Thread.sleep(100/3);}catch(InterruptedException ex){}
And the ball drawing method:
public void paintComponent(Graphics ball) {
super.paintComponent(ball);
if(clicks>0){
ball.setColor(Color.BLUE);
ball.fillOval((int) (x - rad), (int) (y - rad), (int)(2 * rad), (int)(2 * rad));
}
}
But this thread only throws 1 ball into the panel. I also tried an array of threads, but that didn't work.
source
share