title: make a rectangle move on panel
body: <p>I am creating a snake game, and I want to start at the very basic that moving a triangle on the screen. But I am have trouble on how to make a rectangle move across on the screen</p>
  
  <p>Here's the code I have so far:</p>
  
  <p><strong>Screen.java</strong></p>
  
  <pre><code>public class Screen extends JPanel implements ActionListener, KeyListener {
      public static final JLabel statusbar = new JLabel("Default");
      public static final int WIDTH = 800, HEIGHT = 800;
      Timer t = new Timer(50, this);
      int x = 400;
      int y = 400;
      int velx = 0;
      int vely = 0;
  
      private BodyPart b;
  
      public Screen(){
  
          b = new BodyPart(x, y);
          t.start();
          addKeyListener(this);
          setFocusable(true);
          setFocusTraversalKeysEnabled(false);
      }
  
      public static void main(String[] args) {
  
      }
  
      @Override
      public void paintComponent(Graphics g){
          super.paintComponent(g);
          g.setColor(new Color(10, 50, 0));
          g.fillRect(0, 0, WIDTH, HEIGHT);
  
          g.setColor(Color.BLACK);
          for(int i = 0; i &lt; WIDTH / 10; i++) {
              g.drawLine(i * 10, 0, i * 10, HEIGHT);
          }
  
          for(int i = 0; i &lt; HEIGHT / 10; i++) {
              g.drawLine(0, i * 10, WIDTH, i * 10);
          }
  
          b.draw(g);
      }
  
      public void up(){
          vely = -10;
          velx = 0;
      }
      public void down(){
          vely = 10;
          velx = 0;
      }
      public void left(){
          vely = 0;
          velx = -10;
      }
      public void right(){
          vely = 0;
          velx = 10;
      }
  
  
      @Override
      public void actionPerformed(ActionEvent e) {
          repaint();
          x += velx;
          y += vely;
      }
  
      @Override
      public void keyTyped(KeyEvent e) {}
  
      @Override
      public void keyPressed(KeyEvent e) {
          int key = e.getKeyCode();
          switch(key){
          case KeyEvent.VK_DOWN: down();
              System.out.println("Down");
              break;
          case KeyEvent.VK_UP: up();
              System.out.println("up");
              break;
          case KeyEvent.VK_LEFT: left();
              System.out.println("left");
              break;
          case KeyEvent.VK_RIGHT: right();
              System.out.println("right");
              break;
          }        
      }
  
      @Override
      public void keyReleased(KeyEvent e) {}
  }
  </code></pre>
  
  <p><strong>BodyPart.java</strong></p>
  
  <pre><code>public class BodyPart {
  
      int x;
      int y;
  
      public BodyPart(int x, int y) {
          this.x = x;
          this.y = y;
      }
  
      public void draw(Graphics g) {
      g.setColor(Color.red);
          g.fillRect(x, y, 10, 10);
          g.setColor(Color.white);
          g.drawRect(x, y, 10, 10);
      }
  }
  </code></pre>
  
  <p><strong>Frame.java</strong></p>
  
  <pre><code>public class Frame extends JPanel {
      private static JLabel statusbar = new JLabel("Default");
  
      public Frame(){
          statusbar = Screen.statusbar;
      }
  
      public static void main(String[] args) {
          JFrame f = new JFrame();
          Screen s = new Screen();
          BodyPart b = new BodyPart(400,400);
          f.add(s);
          f.add(statusbar, BorderLayout.SOUTH);
          f.setSize(800, 800);
          f.setVisible(true);
          f.setLocationRelativeTo(null);
          f.setResizable(false);
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  }
  </code></pre>
  
  <p>So when I run the code, the rectangle only displays in the center which I set it to, and it doesn't move. Is there anyway to solve it? In addition, the status bar also doesn't move. (There's something weird, when I put everything in a single file, it does work)</p>
  
  <p>Also, I am planning to use <code>linkedlist&lt;Point&gt;</code>, is it possible to mix this program with the <code>linkedlist&lt;&gt;</code> ? or I have to change my code in order to use the <code>linkedlist&lt;&gt;</code> ?</p>
  
  <p>Thank you</p>