I am following HeadFirst Java 2nd Edition and trying to perform a simple animation of a circle moving diagonally from one point to another on the click of a button. I am using JPanel to draw the circle and ActionListener interface to get event from the button. When I directly call the animate function from 'main()' , the animation works fine. But when i try to do so after the click of button, the program freezes and directly displays the end result.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleAnimation{
int x=70;
int y=70;
JFrame frame;
MyDrawPanel drawPanel;
public static void main(String[] args) {
SimpleAnimation gui=new SimpleAnimation();
gui.initialize();
// gui.animate(); //animation method
}
public void initialize(){
frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel=new MyDrawPanel();
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.setSize(400,400);
frame.setVisible(true);
JButton button=new JButton("Click me!");
frame.getContentPane().add(BorderLayout.EAST,button);
button.addActionListener(new OnclickListener());
}
public void animate(){
for (int i=0;i<130 ;i++ ) {
x++;
y++;
drawPanel.repaint();
try{
Thread.sleep(50);
}catch(Exception e){}
}
}
class MyDrawPanel extends JPanel{
public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(), this.getHeight());
g.setColor(Color.green);
g.fillOval(x,y,40,40);
}
}
class OnclickListener implements ActionListener{
public void actionPerformed(ActionEvent event){
animate();
}
}
}
The call to repaint() puts a repaint request into a queue; you may be thinking that it immediately repaints the screen. Your requests are all stacking up in this queue waiting to be executed, and then effectively all getting executed at once.
You need a timer, not a call to Thread.sleep(), to put distance between your different animations. There is a timer provided in java.util, but you instead should use the one in Swing.
I recommend a google search on "animation in Java", where there are several sites that explain things in detail.