title: My drawn object doesn't clear previous rectangle after key is pressed
body: <p>My code will not clear the previous rectangle when I press one of the arrow keys. I thought that by using <code>repaint()</code> in my <code>keyPressed()</code> method, that would solve the problem. I also tried using <code>clearRect()</code>, but that doesn't do anything.</p>
  
  <p>This is the idle rectangle: <img src="https://i.stack.imgur.com/lz4l5.png" alt="http://imgur.com/D9orK9F"></p>
  
  <p>This is when I hold down the right arrow key: <img src="https://i.stack.imgur.com/GUKLh.png" alt="enter image description here"></p>
  
  <p>Here is my code:</p>
  
  <pre><code>import java.awt.Component;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.event.KeyEvent;
  import java.awt.geom.Rectangle2D;
  
  import javax.swing.Icon;
  
  import java.awt.*;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.awt.geom.AffineTransform;
  import java.awt.geom.Arc2D;
  import java.awt.geom.PathIterator;
  import java.awt.geom.Point2D;
  import java.awt.geom.Rectangle2D;
  import java.util.ArrayList;
  
  import javax.swing.Icon;
  import javax.swing.JComponent;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  
  
  public class Rectangle extends JPanel implements KeyListener{
  
  private int height;
  private int width;
  private int x;
  private int y;
  private Graphics2D g2;
  
  
  
  public Rectangle(int width, int height,int x, int y)
  {
      this.height = height;
      this.width = width;
      this.x = x;
      this.y = y;
  
      JLabel label = new JLabel();
      label.addKeyListener(this);
      label.setFocusable(true);
      label.setOpaque(true);
      label.addKeyListener(this);
      this.add(label);
      label.requestFocusInWindow();
      setVisible(true);
  
  }
   public Dimension getPreferredSize() {
          return new Dimension(500,500);
      }
  
  public void keyPressed(KeyEvent e)
  {   
  
      if (e.getKeyCode() == KeyEvent.VK_DOWN)
      {
          y = y + 5;
          repaint();
      }
      if (e.getKeyCode() == KeyEvent.VK_LEFT)
      {
          x = x - 5;
          repaint();
      }
      if (e.getKeyCode() == KeyEvent.VK_UP)
      {
          y = y - 5;
          repaint();
      }
  
      if (e.getKeyCode() == KeyEvent.VK_RIGHT)
      {
          x = x + 5;
          repaint();
      }
  
  }
  
  
  public void keyReleased(KeyEvent e) {}
  
  public void keyTyped(KeyEvent e) {}
  
  
  
  
  public void paintComponent(Graphics g) {
  
  this.g2 = (Graphics2D) g;
  g2.drawRect(x, y, width, height);
  
  }
  
  } 
  </code></pre>
  
  <p>Test class</p>
  
  <pre><code>import java.awt.Dimension;
  import java.awt.FlowLayout;
  
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.SwingUtilities;
  
  
  
  
  
  
  public class Test {
  
   public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI(); 
              }
          });
      }
  
      private static void createAndShowGUI() {
  
          JFrame f = new JFrame("Rectangle");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.add( new Rectangle(40,20,250,250));
          f.pack();
          f.setVisible(true);
  
      }
  
  }
  </code></pre>