title: JFrame repainting image blinking
body: <p>I am having an issue with the command super.paintComponents(g); when updating a JFrame. The issue I am having is that the image I am loading is blinking while the code is running.  I am pretty sure that the code is double buffering and have not found any helpful resource online to solve this issue. This is my first question so please tolerate any formatting errors.</p>
  
  <p>Here is my entire code (snippets below):</p>
  
  <pre><code>public class GAME extends JFrame implements KeyListener, MouseMotionListener {
  int mouseX, mouseY;
  
  public static ArrayList&lt;Images&gt; images = new ArrayList&lt;Images&gt;();
  public static String lastKeyPressed = null;
  
  public GAME() {
      this.addMouseMotionListener(this);
      this.addKeyListener(this);
  }
  
  public void mouseDragged(MouseEvent e) {
      mouseX = e.getX();
      mouseY = e.getY();
  }
  
  public void mouseMoved(MouseEvent e) {
      mouseX = e.getX();
      mouseY = e.getY();
  }
  
  public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == e.VK_LEFT) {
          lastKeyPressed = "LEFT";
      } else if (e.getKeyCode() == e.VK_RIGHT) {
          lastKeyPressed = "RIGHT";
      } else if (e.getKeyCode() == e.VK_UP) {
          lastKeyPressed = "UP";
      } else if (e.getKeyCode() == e.VK_DOWN) {
          lastKeyPressed = "DOWN";
      }
  }
  
  public void keyReleased(KeyEvent e) {
  
  }
  
  public void keyTyped(KeyEvent e) {
  
  }
  
  @Override
  public void paint(Graphics g) {
      super.paintComponents(g);
      for (int i = 0; i &lt; images.size(); i++) {
          g.drawImage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null);
      }
  }
  
  public static void main(String[] args) throws IOException, InterruptedException {
      GAME frame = new GAME();
      Dimension dim = new Dimension(800, 600);
      frame.setPreferredSize(dim);
      frame.setSize(dim);
      frame.setResizable(false);
      frame.setTitle("GAME");
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      Images test = new Images("unnamed.png");
      images.add(test);
      while (true) {
          if (lastKeyPressed == "LEFT") {
              test.xpos -= 5;
              lastKeyPressed = null;
          } else if (lastKeyPressed == "RIGHT") {
              test.xpos += 5;
              lastKeyPressed = null;
          } else if (lastKeyPressed == "UP") {
              test.ypos -= 5;
              lastKeyPressed = null;
          } else if (lastKeyPressed == "DOWN") {
              test.ypos += 5;
              lastKeyPressed = null;
          }
          frame.repaint();
          Thread.sleep(100);
      }
  }
  
  }
  </code></pre>
  
  <p>Here is the images class:</p>
  
  <pre><code>public class Images {
  
  String name;
  BufferedImage img;
  int xpos;
  int ypos;
  
  public Images (String Name) throws IOException{
      name = Name;
      xpos = 40;
      ypos = 90;
      System.out.println(name);
      System.out.println("PATH: " + GAME.class.getResource(name));
      URL file = getClass().getClassLoader().getResource(name);
      img = ImageIO.read(file);
  }
  }
  </code></pre>
  
  <p>What you probably only want to see is the following:</p>
  
  <p>My paint method:</p>
  
  <pre><code>@Override
  public void paint(Graphics g) {
      super.paintComponents(g);
      for (int i = 0; i &lt; images.size(); i++) {
          g.drawImage(images.get(i).img, images.get(i).xpos, images.get(i).ypos, null);
      }
  }
  </code></pre>
  
  <p>And the block of code in the main class that is running the game:</p>
  
  <pre><code>Images test = new Images("unnamed.png");
      images.add(test);
      while (true) {
          if (lastKeyPressed == "LEFT") {
              test.xpos -= 5;
              lastKeyPressed = null;
          } else if (lastKeyPressed == "RIGHT") {
              test.xpos += 5;
              lastKeyPressed = null;
          } else if (lastKeyPressed == "UP") {
              test.ypos -= 5;
              lastKeyPressed = null;
          } else if (lastKeyPressed == "DOWN") {
              test.ypos += 5;
              lastKeyPressed = null;
          }
          frame.repaint();
          Thread.sleep(100);
      }
  </code></pre>
  
  <p>(I added the Thread.sleep because it seemed to decrease the rate at which the blinking occurred)</p>
  
  <p>Here is a gif of what is happening (I just used the first Google result for 'test' for the image):
  <a href="http://i.imgur.com/WjLCSvu.gif?1" rel="nofollow">http://i.imgur.com/WjLCSvu.gif?1</a></p>
  
  <p>I appreciate any help and general suggestions to better my code.  Thank you.</p>