title: Java Snake Game Works Correctly Just Once Per Eclipse Restart
body: <p>I've made decent progress coding a snake game as a personal project and managed to get the movement all done, however the movement only works on the first run each time I start Eclipse and then after that when I run the program the snake will not move. I can technically run it twice using the run button and then the coverage run button for the second. After that they both stop being able to produce the snake's movement. This is 100% consistent every time. It also sometimes works one more time after I save a change.</p>
  
  <p>Game Class</p>
  
  <pre><code>package snake;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class Gameplay extends JPanel implements KeyListener, ActionListener{
      private int[] snakexlength = new int[750];
      private int[] snakeylength = new int[750];
  
  private boolean up = false;
  private boolean down = false;
  private boolean left = false;
  private boolean right = false;
  
  private int lengthofsnake = 3;
  
  private int moves = 0;
  
  private Timer timer;
  private int delay = 100;
  
  
  
  public Gameplay() {
      addKeyListener(this);
      setFocusable(true);
      setFocusTraversalKeysEnabled(false);
      timer = new Timer(delay, this);
      timer.start();
  }
  
  public void paint(Graphics graphics) {
      if(moves == 0) {
          snakexlength[2] = 50;
          snakexlength[1] = 60;
          snakexlength[0] = 70;
          snakeylength[2] = 70;
          snakeylength[1] = 70;
          snakeylength[0] = 70;
      }
  
      graphics.setColor(Color.DARK_GRAY);
      graphics.fillRect(0, 0, 905, 700);
  
      for(int a = 0; a &lt; lengthofsnake; a++) {
          //if(a==0 &amp;&amp; (up || down || left || right))
              graphics.setColor(Color.RED);
              graphics.fillRect(snakexlength[a], snakeylength[a], 5, 5);
      //  if(a!=0) {
          //  graphics.setColor(Color.magenta);
          //  graphics.fillRect(snakexlength[a], snakeylength[a], 5, 5);
          //}
      }
  
      graphics.dispose();
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
      timer.start();
  
      if(up){
          for(int r = lengthofsnake - 1; r &gt;= 0; r--) {
              snakexlength[r+1] = snakexlength[r];
          }
  
          for(int r = lengthofsnake; r &gt;= 0; r--) {
              if(r == 0)
                  snakeylength[r] = snakeylength[r] - 10;
              else
                  snakeylength[r] = snakeylength[r-1];
  
              if(snakeylength[r] &lt; 0)
                  snakeylength[r] = 700;
          }
          repaint();
      }
  
      if(down){
          for(int r = lengthofsnake - 1; r &gt;= 0; r--) {
              snakexlength[r+1] = snakexlength[r];
          }
  
          for(int r = lengthofsnake; r &gt;= 0; r--) {
              if(r == 0)
                  snakeylength[r] = snakeylength[r] + 10;
              else
                  snakeylength[r] = snakeylength[r-1];
  
              if(snakeylength[r] &gt; 700)
                  snakeylength[r] = 0;
          }
          repaint();
      }
  
      if(left){
          for(int r = lengthofsnake - 1; r &gt;= 0; r--) {
              snakeylength[r+1] = snakeylength[r];
          }
  
          for(int r = lengthofsnake; r &gt;= 0; r--) {
              if(r == 0)
                  snakexlength[r] = snakexlength[r] - 10;
              else
                  snakexlength[r] = snakexlength[r-1];
  
              if(snakexlength[r] &lt; 0)
                  snakexlength[r] = 905;
          }
          repaint();
      }
  
      if(right){
          for(int r = lengthofsnake - 1; r &gt;= 0; r--) {
              snakeylength[r+1] = snakeylength[r];
          }
  
          for(int r = lengthofsnake; r &gt;= 0; r--) {
              if(r == 0)
                  snakexlength[r] = snakexlength[r] + 10;
              else
                  snakexlength[r] = snakexlength[r-1];
  
              if(snakexlength[r] &gt; 905)
                  snakexlength[r] = 0;
          }
          repaint();
      }
  }
  
  @Override
  public void keyPressed(KeyEvent e) {
      if(e.getKeyCode() == KeyEvent.VK_D) {
          moves++;
          right = true;
          if(!left) {
              right = true;
          }
          else {
              left = true;
              right = false;
          }
          up = false;
          down = false;
      }
      if(e.getKeyCode() == KeyEvent.VK_A) {
          moves++;
          left = true;
          if(!right) {
              left = true;
          }
          else{
              right = true;
              left = false;
          }
          up = false;
          down = false;
      }
      if(e.getKeyCode() == KeyEvent.VK_W) {
          moves++;
          up = true;
          if(!down) {
              up = true;
          }
          else{
              down = true;
              up = false;
          }
          right = false;
          left = false;
      }
      if(e.getKeyCode() == KeyEvent.VK_S) {
          moves++;
          down = true;
          if(!up) {
              down = true;
          }
          else{
              up = true;
              down = false;
          }
          right = false;
          left = false;
      }
  
  
  }
  
  @Override
  public void keyReleased(KeyEvent e) {
  
  }
  
      @Override
      public void keyTyped(KeyEvent e) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>Main Class with JFrame</p>
  
  <pre><code>    package snake;
  
  import java.awt.Color;
  
  import javax.swing.JFrame;
  
  public class Main {
      public static void main(String[] args) {
          JFrame Window = new JFrame();
          Gameplay game = new Gameplay();
          Window.setBounds(0, 0, 905, 700);
          Window.setBackground(Color.BLACK);
          Window.setVisible(true);
          Window.setResizable(false);
          Window.setTitle("Snake Game");
          Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Window.add(game);
      }
  
  }
  </code></pre>