title: Trouble with KeyListener
body: <p>This is my first time using <code>KeyListener</code> so excuse me if I made an obvious mistake. My problem is that whenever I press the arrow keys or WASD keys, the ellipses doesn't react. I know there are other flaws in my code but I want to just focus on <code>KeyListener</code> issue.</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.awt.geom.Ellipse2D;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  import java.awt.event.KeyListener;
  
  public class idkdude extends JPanel implements ActionListener, KeyListener {
  
      Timer t = new Timer(5, this);
       int rx = 395, ry = 395, rvelx = 0, rvely = -2, bx = 195, by = 195, bvelx =     0, bvely = 2;
       boolean redWins = false;
       boolean blueWins = false;
  
       public void paint(Graphics g)
          {
              super.paint(g);
               drawSurface(g);
              Graphics2D g2 = (Graphics2D) g;             
              g.setColor(Color.red);
              Ellipse2D rcircle = new Ellipse2D.Double(rx, ry, 10, 10);
              g2.fill(rcircle);
              g2.drawLine(400, 400, rx+5, ry+5);
              g.setColor(Color.blue);
              Ellipse2D bcircle = new Ellipse2D.Double(bx, by, 10, 10);
              g2.fill(bcircle);
              g2.drawLine(200, 200, bx+5, by+5);
              if (redWins == true)
              {
                  redWins(g);
              }
              if (blueWins == true)
              {
                  blueWins(g);
              }
              t.start();
          }
  
       public void init()
       {
           addKeyListener(this);
       }
       public void keyReleased(KeyEvent e){}
          public void keyTyped(KeyEvent e) {}
          public void keyPressed(KeyEvent e)
          { //make sure to put
              //rx+=rvelx;
              //ry+=rvely;
              // and draw a new line
              int key = e.getKeyCode();
              if (key == KeyEvent.VK_LEFT) 
              {rvelx = -2;rvely = 0;}
              if (key == KeyEvent.VK_RIGHT)
              {rvelx = 2; rvely = 0;}
              if (key == KeyEvent.VK_UP)
              {rvelx = 0; rvely = -2;}
              if (key == KeyEvent.VK_DOWN)
              {rvelx = 0; rvely = 2;}
              if (key == KeyEvent.VK_W)
              {bvelx = 0; rvely = -2;}
              if (key == KeyEvent.VK_A)
              {bvelx = -2; rvely = 0;}
              if (key == KeyEvent.VK_S)
              {bvelx = 0; rvely = 2;}
              if (key == KeyEvent.VK_D)
              {bvelx = 2; rvely = 0;}
          }
       public void actionPerformed(ActionEvent e)
          {
              rx+=rvelx;
              ry+=rvely;
              bx+=bvelx;
              by+=bvely;
              if (rx&gt; 600 || rx &lt;0)
              {
                  blueWins = true;
              }
  
              if (ry&lt;0 || ry&gt;600)
              {
                  blueWins = true;
              }
              if (bx&gt;600 || bx&lt;0)
              {
                  redWins = true;
              }
              if (by&gt;600 || by&lt;0)
              {
                  redWins = true;             
              }                
              repaint();              
          }
  
       public void redWins(Graphics g)
       {
           g.setColor(Color.black);
              g.fillRect(0,0,600,600);
              g.setColor(Color.white);
              g.drawString("RED HAS WON!!!!!", 250, 250);
              g.drawString("Click Anywhere to Start Another Game", 250, 350);  
       }
       public void blueWins(Graphics g)
       {
           g.setColor(Color.black);
              g.fillRect(0,0,600,600);
              g.setColor(Color.white);
              g.drawString("BLUE HAS WON!!!!!", 250, 250);
              g.drawString("Click Anywhere to Start Another Game", 250, 350);
       }
       public void drawSurface(Graphics g)
       {
          g.setColor(Color.black);
          g.fillRect(0,0,600,600);
          g.setColor(Color.white);
          for (int x = 0; x &lt;= 600; x+=25)
              g.drawLine(x,0,x,600);
          for (int y= 0; y &lt;=600; y+=25)
              g.drawLine(0,y,600,y);
       }
  }
  </code></pre>