title: Java Code Optimization
body: <p>I just did a Tutorial on how to make a character on a 2d background just move around basically like a very limited platformer without gravity or anything and I have run into some lag, Im assuming its because of the way the keypressed/released works but i was wondering if there were any way to optimize it, It could be that I have no animation its just simply a block, but Im not sure, its just like if im holding up and left and then i let go of left and press right while all the time still holding up it just goes up, until i let go, it lags going between left and right etc. I will paste the code below. Thanks!:</p>
  
  <pre><code>//@@@MAIN CLASS, NAMED CHECKERS ARBITRARILY
  
  import javax.swing.*;
  
  public class Checkers {
  
  public static void main(String[] args) {
      JFrame frame = new JFrame("2D Game");
  
      frame.add(new Board());
  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(1200,326);
      frame.setVisible(true);
  }
  }
  
  
  //@@@@@@@@@@@@@@@@@
  //class called Dude this is my character(circle)
  
  import javax.swing.ImageIcon;
  import java.awt.Image;
  import java.awt.event.KeyEvent;
  
  
  public class Dude {
  int x, dx, y, dy;
  Image still;
  
  public Dude(){
      ImageIcon i = new ImageIcon("C:/still.png");
      still = i.getImage();
      x = 10;
      y = 150;
  }
  
  public void move(){
      x = x + dx;
      y = y + dy;
  
  }
  
  public int getX(){
      return x;
  }
  
  public int getY(){
      return y;
  }
  
  public Image getImage(){
      return still;
  }
  
  public void keyPressed(KeyEvent e){
      int key = e.getKeyCode();
  
      if (key == KeyEvent.VK_LEFT)
          dx = -2;
  
     if (key == KeyEvent.VK_RIGHT)
          dx = 2;
  
     if (key == KeyEvent.VK_UP)
         dy = -2;
  
     if (key == KeyEvent.VK_DOWN)
         dy = 2;
  
  }
  
      public void keyReleased(KeyEvent e){
      int key = e.getKeyCode();
  
      if (key == KeyEvent.VK_LEFT);
          dx = 0;
  
      if (key == KeyEvent.VK_RIGHT);
          dx = 0;
  
      if (key == KeyEvent.VK_UP)
         dy = 0;
  
      if (key == KeyEvent.VK_DOWN)
         dy = 0;
  
  }
  
  }
  
  
  //@@@@@@@@@@@@@@@@@@@
  //this is my board class (background)
  
  
  
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.Image;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import javax.swing.*;
  
  public class Board extends JPanel implements ActionListener{
  
  Dude p;
  Image img;
  Timer time;
  
  public Board(){
      p = new Dude();
      addKeyListener(new AL());
      setFocusable(true);
      ImageIcon i = new ImageIcon("C:/test.png");
      img = i.getImage();
      time = new Timer(5, this);
      time.start();
  }
  public void actionPerformed(ActionEvent e){
      p.move();
      repaint();
  }
  
  public void paint(Graphics g){
      super.paint(g);
              Graphics2D g2d = (Graphics2D) g;
  
              g2d.drawImage(img, 0, 0, null);
              g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
  }
  
  private class AL extends KeyAdapter{
      public void keyReleased(KeyEvent e){
          p.keyReleased(e);
      }
      public void keyPressed (KeyEvent e){
          p.keyPressed(e);
      }
  }
  
  }
  </code></pre>