title: My rectangle sometimes moves when I run my java program
body: <p>I should be able to move the rectangle with the arrow keys but after running the program a few times the rectangle does not move. To make the rectangle move I close Netbeans and reopen it. Then the rectangle is able to move but it stops moving after a couple tries. I want to solve this problem so I can make changes. </p>
  
  <pre><code>package project;
  
  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.JFrame;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  //KeyListener is use with keyboard
  public class main extends JPanel implements ActionListener, KeyListener
  {
      Timer tm = new Timer(5, this); //for animation
      int x = 50, y = 50, velX =0, velY = 0; 
  
      public main()
      {
          tm.start(); //starts timer
          addKeyListener(this); //this refearing to KeyListener
          setFocusable(true); //enable KeyListener
          setFocusTraversalKeysEnabled(false); //shift or tab is not use so F
      }
      public void paintComponent(Graphics g)
      {
          super.paintComponent(g);
  
          g.setColor(Color.red);
          g.fillRect(x,y,50,50);
      }        
  
      public void actionPerformed(ActionEvent e)
      {
          if (x&lt;0)
          {
              velX = 0;
              x = 0;
          }    
  
          if (x&gt;500)
          {
              velX = 0;
              x = 500;
          }    
  
          if (y&lt;0)
          {
              velY = 0;
              y=0;
          }  
  
          if (y&gt;300)
          {
              velY = 0;
              y = 300;
          }    
  
          x = x + velX;
          y = y + velY;
  
          repaint(); // repaint rectangle 
      }
  
      public void keyPressed(KeyEvent e)
      {
          int c = e.getKeyCode(); //get key
  
          if (c == KeyEvent.VK_LEFT) // VK_Left is left arrow 
          {
              velX = -3;
              velY = 0;
          }
          if (c == KeyEvent.VK_UP) // VK_UP is up arrow 
          {
              velX = 0;
              velY = -3; // means up 
          }
          if (c == KeyEvent.VK_RIGHT)  
          {
              velX = 3;
              velY = 0; 
          }
          if (c == KeyEvent.VK_DOWN)  
          {
              velX = 0;
              velY = 3; 
          }
      }
  
      public void keyTyped(KeyEvent e){}
      public void keyReleased(KeyEvent e) //when you stop pressing 
      {
          velX = 0;
          velY = 0; 
      }
  
      public static void main(String[] args)
      { 
          main m = new main();
          JFrame jf = new JFrame();
          jf.setTitle("Tutorial");
          jf.setSize(600,400);
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jf.add(m);
      }
  }
  </code></pre>