title: JLabel animation in JPanel
body: <p>After scratching around I found that it's best to implement a custom image component by extending a JLabel. So far that has worked great as I can add multiple "images" (jlabels without the layout breaking. I just have a question that I hope someone can answer for me.</p>
  
  <ul>
  <li>I noticed that in order to animate JLabels across the screen I need to <code>setlayout(null);</code> and <code>setbounds</code> of the component and then to animate eventually <code>setlocation(x,y);</code>. Is this a best practice or a terrible way to animate a component? I plan on eventually making an animation class but I don't want to do so and end up having to chuck it.</li>
  </ul>
  
  <p>I have included relevant code for a quick review check.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class GraphicsPanel extends JPanel  {
      private Timer timer;
      private long startTime = 0;
      private int numFrames = 0;
      private float fps = 0.0f;
      private int x = 0;
  
      GraphicsPanel() {
          final Entity ent1 = new Entity();
          ent1.setBounds(x, 0, ent1.getWidth(), ent1.getHeight());
          add(ent1);
  
          //ESSENTIAL
          setLayout(null);
  
          //GAMELOOP
          timer = new Timer(30, new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  getFPS();
                  incX();
                  ent1.setLocation(x, 0);
                  repaint();
              }
          });
          timer.start();
      }
  
      public void incX() { x++; }
  
      @Override
      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g.create();
          g2.setClip(0, 0, getWidth(), getHeight());
  
          g2.setColor(Color.BLACK);
          g2.drawString("FPS: " + fps, 1, 15);
      }
  
      public void getFPS()
      {
          ++numFrames;
          if (startTime == 0) {
              startTime = System.currentTimeMillis();
          } else {
              long currentTime = System.currentTimeMillis();
              long delta = (currentTime - startTime);
              if (delta &gt; 1000) {
                  fps = (numFrames * 1000) / delta;
                  numFrames = 0;
                  startTime = currentTime;              
              }
          }
      }
  }
  </code></pre>
  
  <p>Thank you!</p>