In java, how do you drop a ball to a panel in every mouse click? Say I clicked the panel 3 times, so there should be 3 balls bouncing in the panel.
I have this code block:
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 method to paint the ball:
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 drops 1 ball in the panel. I also did try an array of threads but it did not work.