title: Time Delay using Thread.sleep() for paintComponent(Graphics g) not working as expected
body: <p>I am making an Animated ProgressBar, in which i used multiple <code>fillRect()</code> method of class <code>javax.swing.Graphics</code>.<br>
  To put a delay after each rectangle is painted, I am using <code>Thread.sleep(500)</code> method for making a delay, (Suggested by many Forums, for making a delay).<br>
  The Problem is, instead of making a delay of 0.5sec after each Rectangle box is displayed, it takes the whole delay required by all the rectangles, in the start, and then displays the final image, thats the Progress Bar.<br>
  <strong>Question 1</strong><br>
  TO make a delay for every single bar, i put the delay "<code>Thread.sleep(500)</code>" along with the bars "<code>fillRect()</code>" in a single <code>for() loop</code>, i would like to know, Why does it takes all the delay in the beginning and then dislplays the completed ProgressBar.<br>
  <strong>Question 2</strong><br>
  How can i change my code, so that the delay can occur simultaneously with each rectangle bar, so when i run the program it should generate an Animated Progress Bar.<br>
  <strong>Code:</strong>  </p>
  
  <pre><code>import javax.swing.JOptionPane;
  import java.awt.Graphics;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import java.awt.Color;
  
  class DrawPanel extends JPanel
  {
      public paintComponent(Graphics g)
      {
          super.paintComponent(g);
          g.setColor(new Color(71,12,3));
          g.fillRect(35,30,410,90);
  
          for ( int i=1; i&lt;=40; i+=2)
          {
            Color c = new Color (12*i/2,8*i/2,2*i/2);
            g.setColor(c);
            g.fillRect( 30+10*i,35,20,80);
  
          try
            { Thread.sleep(500); } 
          catch(InterruptedException ex)
            { Thread.currentThread().interrupt(); }
          }
      }
  }
  
  class ProgressBar
  {
      public static void main (String []args)
      {
          DrawPanel panel = new DrawPanel();
          JFrame app = new JFrame();
          app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
          app.add(panel);
          app.setSize(500,200);
          app.setVisible(true);
      }
  }  
  </code></pre>
  
  <p>Your help is highly appreciated, Thankyou.</p>