title: Graphics not moving automatically and also makes copies instead of moving when pressing arrow keys
body: <p>I'm working on a snake game, and I'm trying to make the snake move to the right. The issue here is the snake isn't actually moving, it just seems to copy itself to the right and also it's not going to the right automatically you have to keep pressing the key.</p>
  <p>I am really not sure what the issue is, I made some  code without any images. That should make the code run able for testing as it is.</p>
  <pre class="lang-java prettyprint-override"><code>public class Game{
      static Graphics g;
  
      public static void main(String[] args) {                   
          JFrame b = new JFrame(&quot;Snake&quot;);
          b.setBounds(300,60,905,700);
          b.setBackground(Color.DARK_GRAY);
          b.setResizable(false);
          Snake snake = new Snake();
  
          b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          b.add(snake);
          b.setVisible(true);
     }
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public class Snake extends JPanel implements KeyListener,ActionListener {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private int[] snakeXlength = new int [750];
      private int[] snakeYlength = new int [750];
      
      private boolean right = true;
      private boolean left = false;
      private boolean up = false;
      private boolean down = false;
      private ImageIcon rightmouth;
      private ImageIcon snakeimage;
      private ImageIcon leftmouth;
      private ImageIcon downmouth;
      private ImageIcon upmouth;
      private ImageIcon enemy;
  
      private Timer timer;
      private int snakeDelay = 100;
      private int moves = 1;
      private int lengtOfSnake = 3;
      public Snake(){
          
          addKeyListener(this);
          setFocusable(true);
          setFocusTraversalKeysEnabled(false);
          timer = new Timer(snakeDelay, this);
          timer.start();
          
      }
      public void paint(Graphics g) {
            g.setColor(Color.WHITE);
              g.drawRect (20, 24, 851, 612);  
                g.setColor(Color.BLACK);
                  g.fillRect (21, 25, 850, 610);
  
          if (moves == 0) {
              snakeXlength[2]= 63;
              snakeXlength[1]= 83;
              snakeXlength[0]= 100;
              
              snakeYlength[2]= 100;
              snakeYlength[1]= 100;
              snakeYlength[0]= 98;
  
               rightmouth = new ImageIcon(&quot;rightmouth.png&quot;);
  
              rightmouth.paintIcon(this, g, snakeXlength[0], snakeYlength[0]);
  
          }
          for(int a = 0; a &lt; lengtOfSnake; a++) {
      
              
              
              if (a == 0 &amp;&amp; right) {
              if (a == 0 &amp;&amp; right) {
                  g.setColor(Color.RED);
                  
                  g.drawRect(5,10,snakeXlength[a],snakeYlength[a]);
  
                 g.fillRect (snakeXlength[a],snakeYlength[a],21,21);
                  g.drawRect(5,10,snakeXlength[a],snakeYlength[a]);
                  // rightmouth = new ImageIcon(&quot;rightmouth.png&quot;);
  
                  //rightmouth.paintIcon(this, g, snakeXlength[a], snakeYlength[a]);
  
              }
  
              if (a == 0 &amp;&amp; left) {
                   leftmouth = new ImageIcon(&quot;leftmouth.png&quot;);
  
                  leftmouth.paintIcon(this, g, snakeXlength[a], snakeYlength[a]);
  
              }
   
              if (a == 0 &amp;&amp; down) {
                   downmouth = new ImageIcon(&quot;downmouth.png&quot;);
  
                  downmouth.paintIcon(this, g, snakeXlength[a], snakeYlength[a]);
  
              }
              if (a == 0 &amp;&amp; up) {
                   upmouth = new ImageIcon(&quot;uptmouth.png&quot;);
  
                  upmouth.paintIcon(this, g, snakeXlength[a], snakeYlength[a]);
          
                  
                  }
              if (a != 0) {
                  g.setColor(Color.GREEN);
  
                  g.fillOval(snakeXlength[a],snakeYlength[a],17,17);
                   //snakeimage = new ImageIcon(&quot;snakeimage.png&quot;);
  
                  //snakeimage.paintIcon(this, g, snakeXlength[a], snakeYlength[a]);
  
              }
              
          }
                  
          g.dispose();
          
      }
  
      public void keyPressed(KeyEvent e) {
          timer.start();
          if(right) {
              for (int r = lengtOfSnake-1; r &gt;= 0;r--) {
                  snakeYlength[r+1] = snakeYlength[r];
              }
              for(int r = lengtOfSnake;r &gt;=0;r--) {
                  if(r==0) {
                      snakeXlength[r] =   snakeXlength[r] +25;
                      
                  }
                  
                  else {
                      snakeXlength[r] =   snakeXlength[r-1];
  
                  }
                  
                  if(snakeXlength[r] &gt; 850){
                      snakeXlength[r] = 25;
                  }
              }
                  repaint();
  
          }
          
          if(left) {
              
          }
          if(up) {
              
          }
          if(down) {
              
          }
      }
      
      @Override
      public void keyTyped(KeyEvent e) {
          if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
              moves++;
              right = true;
  
              if(left != true) {
                  right = true;
  
              }
              else
              {
                  right = false;
                  left = true;
              
                  
              }
              down = false;
              up = false;
  
          }
          
          if(e.getKeyCode() == KeyEvent.VK_LEFT) {
              moves++;
              left = true;
  
              if(right != true) {
                  left = true;
  
              }
              else
              {
                  left = false;
                  right = true;
              
                  
              }
              down = false;
              up = false;
  
          }       if(e.getKeyCode() == KeyEvent.VK_UP) {
              moves++;
              up = true;
  
              if(down != true) {
                  up = true;
  
              }
              else
              {
                  up = false;
                  down = true;
              
                  
              }
              left = false;
              right = false;
  
          }
          
              if(e.getKeyCode() == KeyEvent.VK_DOWN) {
          moves++;
          down = true;
  
          if(up != true) {
              down = true;
          }
          else
          {
              up = true;
              down = false;
          
              
          }
          left = false;
          right = false;
  
      }
      }
  
      @Override
  
  
      
      public void keyReleased(KeyEvent e) {
          // TODO Auto-generated method stub
          
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
          // TODO Auto-generated method stub
          
      }
  }
  </code></pre>