title: repeating animation using java.swingTimer
body: <p>I am writing the code of Game bean machine (Galton box) using animated graphics.
  I am using swing.Timer to make the red ball move, by letting the Timer object take an ActionListener.</p>
  
  <p>Here is the output:
  The red ball here is moving randomly : 
  <a href="http://imgur.com/J9Xq0t5&amp;HIv1udp" rel="nofollow">http://imgur.com/J9Xq0t5&amp;HIv1udp</a>
  And here it reaches the bottom:
  <a href="http://imgur.com/J9Xq0t5&amp;HIv1udp#1" rel="nofollow">http://imgur.com/J9Xq0t5&amp;HIv1udp#1</a></p>
  
  <p>The problem is that I want to make several balls moving not only 1 but I actually cannot, I tried to put the Timer object in a loop but it only speeded up the ball motion.</p>
  
  <p>Here is the code I wrote:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.util.Random;
  import java.util.Scanner;
  import javax.swing.*;
  
  
  public class GameBean extends JFrame{
  private int nSlots;
  private int balls;
  
  public GameBean (int nSlots, int balls){
      this.balls=balls;
      this.nSlots=nSlots;
      Timer timer=new Timer(500,new RandomListener());
  
      timer.start();
  
      JPanel panel = new JPanel();
  
      this.add(canvas); 
  }
  
  Ball canvas = new Ball();
  
  
   public static void main(String[] args) {
  
      Scanner input=new Scanner(System.in);
  
      System.out.println("Please enter number of slots: ");
      int nSlots=input.nextInt();
  
      JFrame frame = new GameBean (nSlots, 5);
  
      frame.setTitle("Bean Game Machine");
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
      frame.setSize(300, 500);
      frame.setVisible(true);
  
     }
   class RandomListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
           canvas.moveRandom();
      }
  }
  
  
  class Ball extends JPanel{
      Random rand=new Random();
      private int n;
      private int raduis = 10;
      private int moveRaduis=12;
      private int L = 0;
      private int R = 0;
      private int D = 0;
  
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
  
          g.setColor(Color.RED);
  
          g.fillOval(getWidth()/2 -raduis/2  + R + L, 0 + D , moveRaduis, moveRaduis);
  
          repaint();
          int  i=0;
          int width=(getWidth()/8);
          int space=(getWidth()-nSlots*30)/2;
          g.setColor(Color.BLACK);
          for(i=0;i&lt;=nSlots;i++){
              g.drawLine(space+(30*i),getHeight()-balls*(raduis) ,space+(30*i) ,getHeight() );
              if(i!=0 &amp;&amp; i!=nSlots)
                  g.fillOval(space+(30*i)-5, getHeight()-balls*(raduis)-5, 10, 10);
              if(i==0){
                  g.drawLine(space,getHeight()-balls*(raduis) , space+((nSlots)*15)-(raduis/2)-15 , getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2);
                  g.drawLine(space+((nSlots)*15)-(raduis/2)-15 , getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 , space+((nSlots)*15)-(raduis/2)-15 , getHeight()/2-nSlots*nSlots);//vertical line
  
              }
              if(i==nSlots){
                  g.drawLine(space+(30*i), getHeight()-balls*(raduis), space+((nSlots)*15)+(raduis/2)+15, getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2);
                  g.drawLine(space+((nSlots)*15)+(raduis/2)+15 , getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 , space+((nSlots)*15)+(raduis/2)+15 , getHeight()/2-nSlots*nSlots);//vertical line
  
              }
  
          }
  
  
          int o=1;
          for(i=1;i&lt;=nSlots-2;i++){
              int k=2;
              for(int j=i;j&lt;=nSlots-2;j++){
                  g.fillOval(space+((i+k)*15)-raduis/2, getHeight()-(balls*raduis)-(30*o)-raduis/2, raduis, raduis); 
                  k=k+2;   
               }o++;
          }
  
        }
  
      public void moveRandom(){
  
          n=1+rand.nextInt(100);
          if(n&lt;=51){
              D+=15;
  
              if(D&lt;=(getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2))//if(D&lt;=14*15)
                  L=0;
              else if(D&gt;getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 &amp;&amp; D&lt;getHeight()-balls*(raduis))
              {
                  D+=15;
                  L-=15;
  
              }
              else if(D&gt;=getHeight()-balls*(raduis))
              {
                  if(D==31*15)D-=15;
                  D=D;
  
              }
  
          }
          else if (n&gt;=51 &amp;&amp; n&lt;=100){
              D+=15;
              if(D&lt;=getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2)
                  R=0;
              else if(D&gt;getHeight()-15-(balls*raduis)-(30*(nSlots-2))-raduis/2 &amp;&amp; D&lt;getHeight()-balls*(raduis))
                  {
                  D+=15;
                  R+=15;
              }
              else if(D&gt;=getHeight()-balls*(raduis)){
  
                  if(D==31*15)D-=15;
                  D=D;
  
              }
  
          }
  
      }
      } 
  
      }
  </code></pre>
  
  <p>Any HELP please !
  Thanks all..</p>