title: Animated Sprites with Java Swing
body: <p>Can someone tell me how can I slowdown the sprites appearance to create a more smooth animation? When I run the code it appears the last (27th) sprite in the JPanel. The animation processing is too fast!</p>
  
  <p>Someone told me about Swing Timer, but unfortunately I tried several times with that and I couldn't put the code to run well :(</p>
  
  <p>Here is the code that I have so far: </p>
  
  <pre><code>package sprites;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Sprites extends JFrame {
  
  public static void main(String[] args) {
      JFrame frm1 = new JFrame();
      frm1.setSize(400, 400);
      frm1.setLocationRelativeTo(null);
      frm1.setResizable(false);
      frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Painel1 pn1 = new Painel1();
      frm1.getContentPane().add(pn1);
      frm1.setVisible(true);
    }
  }
  
  class Painel1 extends JPanel {
  
      BufferedImage img;
  
      public Painel1() {
          setBackground(Color.yellow);
      try
      {
        img = ImageIO.read(new File("images/dummy.png"));
      }
      catch (IOException e)
      {}
    }
  
    @Override
    public void paintComponent(Graphics g) {
      int[][] spriteSheetCoords = {{8, 10, 119, 129}, 
                                   {138, 10, 118, 130},
                                   {267, 10, 118, 132},
                                   {402, 11, 113, 132},
                                   {538, 12, 106, 134},
                                   {671, 13, 103, 133},
                                   {804, 12, 102, 132},
                                   {23, 161, 100, 134},
                                   {157, 162, 96, 134},
                                   {287, 159, 95, 135},
                                   {418, 158, 95, 133},
                                   {545, 159, 99, 133},
                                   {673, 159, 102, 134},
                                   {798, 158, 108, 130},
                                   {9, 309, 116, 126},
                                   {137, 309, 118, 127},
                                   {274, 310, 110, 128},
                                   {412, 311, 102, 129},
                                   {541, 312, 103, 130},
                                   {671, 312, 104, 131},
                                   {806, 312, 98, 132},
                                   {29, 463, 94, 135},
                                   {155, 462, 98, 135},
                                   {279, 461, 104, 135},
                                   {409, 461, 106, 135},
                                   {536, 461, 109, 135},
                                   {662, 461, 112, 133}};
      Image subSprite;
      for (int i = 0; i &lt;= 26; i++) {
        super.paintComponent(g);
        subSprite = img.getSubimage(spriteSheetCoords[i][0], spriteSheetCoords[i][1], spriteSheetCoords[i][2], spriteSheetCoords[i][3]);
        g.drawImage(subSprite, 140, 120, null);
      }
    }  
  }
  </code></pre>
  
  <p>It is suppose to create a loop from the 1st sprite to the last (the 27th) sprite.</p>