I'm currently making a game where I want to animate the piece being dropped on the board, but cannot use Thread.sleep() as it will interrupt the game. How can I do something like the code below so that it pauses for a certain amount of time between painting the oval again a bit further down.
for(int counter = 0 ; counter < getMouseX(); counter++) {
g.fillOval(x*70+10, y, 50, 50);
//code to wait for 'x' seconds here
}
Also, is there any way I could disable mouse clicks while this process is going on?
One way to do this would be to use a Swing Timer. Set the Timer to fire X timers per second, then perform your animation there.
It might look something like this:
final Timer t = new Timer(10, new ActionListener(){
public void actionPerformed(ActionEvent e){
//do your updating of your variables here
repaint();
counter++;
if(counter >= mouseX){
t.stop();
}
}
});
t.start();
More info can be found in the Timer tutorial here . You also might consider googling something like "java swing animation".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.