title: Animating a Rectangle in Java
body: <p>I've been trying to get this rectangle to move that I've created using a for loop. All that's happening with this code is that there is an original rectangle and then a new one next to that rectangle. No animation happens, only those two rectangles show on the window. What are some methods to get this rectangle to animate?   </p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  
  public class Gunman extends JComponent {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      public int x = 10;
      public int y = 10;
      public int width = 8;
      public int height = 10;
      public void paint(Graphics g) {
          g.setColor(Color.red);
          g.drawRect (x, y, width, height);  
          g.fillRect (x, y, width, height);
          for(int i = 0; i&lt;=1024; i++){
              g.setColor(Color.red);
              g.drawRect(x++, y, width, height);
              g.fillRect(x++, y, width, height);
          }
      }
  }
  </code></pre>