title: Sprite Movement in Java
body: <p>I started learning java as a hobby a little while back because I wanted to make a little game. I learned the basics of java, and I decided that I would try to tackle game-dev. My JFrame and all is fine and I technically have no bugs, but my little rectangle guy won't move around the screen. Here's my code:</p>
  <pre><code>package main;
  
  import java.awt.Color;
  import java.awt.Container;
  import java.awt.Graphics;
  import java.awt.Rectangle;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  
  import javax.swing.JFrame;
  
  public class Race extends JFrame {
  
  public int speed=5;
  public int up=1;
  public int left=2;
  public int down=3;
  public int right=4;
  public int direction=0;
  
  Rectangle p1 = new Rectangle (500,400,20,40);
  
  public Race()
  {
      super(&quot;Race&quot;);
      setSize(1000,800);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container cont=getContentPane();
      cont.setBackground(Color.orange);
      setResizable(false);
  }
  
  public void paint(Graphics g)
  {
      super.paint(g);
      g.setColor(Color.red);
      g.fillRect(p1.x, p1.y, p1.width, p1.height);
  }
  
  
  public class Move extends Thread implements KeyListener
  {
      public void run()
      {
          addKeyListener(this);
          
          while(true)
          {
              try
              {
                  repaint();
                  
                  if(direction==up)
                  {
                      p1.y-=(int)speed;
                  }
                  if(direction==down)
                  {
                      p1.y+=(int)speed;
                  }
                  if(direction==right)
                  {
                      p1.x+=(int)speed;
                  }
                  if(direction==left)
                  {
                      p1.x-=(int)speed;
                  }   
                      Thread.sleep(75);
              }
                  catch (Exception e)
                  {
                      break;
                  }
              }
          }
  
      @Override
      public void keyPressed(KeyEvent event) {
          // TODO Auto-generated method stub
          
      }
  
      @Override
      public void keyReleased(KeyEvent event) {
          // TODO Auto-generated method stub
          
      }
  
      @Override
      public void keyTyped(KeyEvent event) {
          // TODO Auto-generated method stub
          if(event.getKeyChar()=='w')
          {
              direction = up;
          }
          if(event.getKeyChar()=='d')
          {
              direction = left;
          }
          if(event.getKeyChar()=='s')
          {
          direction = down;   
          }
          if(event.getKeyChar()=='a')
          {
              direction = left;
          }
      }
      
      }
      
  
  
  
  
  public static void main(String[] args)
  {
      new Race();
  }
  
  }
  </code></pre>