title: animation using repaint()
body: <p>I am trying to create a simple animation which draws random rectangles when a button is pressed. So far I managed to create rectangle on the press of a button. I want to further develop the code so that when I press the button, more than multiple random rectangles are created. I tried to create a for loop which asks the inner class to repaint itself but it still didn't work. can anyone help me please. </p>
  
  <pre><code>    public class TwoButtonsRandomRec {
  
      JFrame frame;
      private int width = 500;
      private int height = 500;
      private DrawPanel dp;
  
      public int getWidth() {
          return width;
      }
  
      public void setWidth(int width) {
          this.width = width;
      }
  
      public int getHeight() {
          return height;
      }
  
      public void setHeight(int height) {
          this.height = height;
      }
  
  
      public static void main (String[] args)
      {   
          TwoButtonsRandomRec test = new TwoButtonsRandomRec(); 
          test.go();
      }
  
      public void go()
      {   
          dp = new DrawPanel();
  
          JButton start = new JButton("Start");
          start.addActionListener(new startListener());
          JButton stop = new JButton("Stop");
          stop.addActionListener(new stopListener());
  
          frame = new JFrame();
          frame.setSize(getWidth(), getHeight());
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(BorderLayout.NORTH, start);
          frame.getContentPane().add(BorderLayout.SOUTH, stop);
      }
  
      class startListener implements ActionListener{
          public void actionPerformed(ActionEvent event){
              frame.getContentPane().add(BorderLayout.CENTER, dp);
              frame.repaint();
              frame.getRootPane().revalidate();       
              for(int i=0; i&lt;10; i++){
                  dp.repaint();
              }
          }
      }
  
      class stopListener implements ActionListener{
          public void actionPerformed(ActionEvent event){
              System.out.println("stop");
          }
      }
  
      class DrawPanel extends JPanel{
  
          public void paintComponent(Graphics g){
              int w = 5+(int)(Math.random() * width-5);
              int h = 5+(int)(Math.random() * height-5);
              int maxX = width-w; // diffX &amp; diffY are used to ensure that rectangle is
              int maxY = width-h; // draw completely inside the window
              int x = (int)(Math.random() * maxX);
              int y = (int)(Math.random() * maxY);
              Color color = new Color((int) (Math.random()*256), // random red
                      (int) (Math.random()*256), // random green
                      (int) (Math.random()*256));// random blue
              g.setColor(color);
              g.fillRect(x,y,w,h);
          }
      }
  }
  </code></pre>