title: Using JButton to start simple animation
body: <p>I am following <strong>HeadFirst Java 2nd Edition</strong> 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.</p>
  
  <p>Code:</p>
  
  <pre><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&lt;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();
              }
          }
  }
  </code></pre>