title: Java KeyListener with a moving object
body: <p>I'm trying to incorporate Java KeyListener to my moving object with the left/right arrows affecting the x-axis (xSpeed) coordinates and the up/down arrows affecting the y-axis (ySpeed). I'm just not able to connect the object and the KeyListener for some reason. Help me please? Thanks!</p>
  
  <pre><code>    import java.awt.*;
  import java.awt.geom.*;
  import java.awt.event.*;
  
  public class Action
  {
      private static final int GRAVITY = 1;
      private int ballDegradation = 8;
      private Ellipse2D.Double circle;
      private Color color;
      private int diameter;
      private int xPosition;
      private int yPosition;
      private final int groundPosition; 
      private final int topPosition;
      private final int leftSidePosition;
      private final int rightSidePosition;
      private Canvas canvas;
      private int ySpeed = -1;  
      private int xSpeed = 8;
      public Action(int xPos, int yPos, int ballDiameter, Color ballColor,
      int groundPos, int topPos, int leftSidePos, int rightSidePos, Canvas drawingCanvas)
      {
          xPosition = xPos;
          yPosition = yPos;
          color = ballColor;
          diameter = ballDiameter;
          groundPosition = groundPos;
          topPosition = topPos;
          leftSidePosition = leftSidePos;
          rightSidePosition = rightSidePos;
          canvas = drawingCanvas;
      }
      public void draw()
      {
          canvas.setForegroundColor(color);
          canvas.fillCircle(xPosition, yPosition, diameter);
      }
      public void erase()
      {
          canvas.eraseCircle(xPosition, yPosition, diameter);
      }    
      public void move()
      {
          erase();
          ySpeed += GRAVITY;
          yPosition += ySpeed;
          xPosition += xSpeed;
          if(yPosition &gt;= (groundPosition - diameter) &amp;&amp; ySpeed &gt; 0) 
          {
              yPosition = (int)(groundPosition - diameter);
              ySpeed = -ySpeed + ballDegradation; 
          }
          if(yPosition &lt;= topPosition &amp;&amp; ySpeed &lt; 0)
          {
              yPosition = (int)topPosition;
              ySpeed = -ySpeed + ballDegradation;
          }
          if(xPosition &lt;= leftSidePosition &amp;&amp; xSpeed &lt;0)
          {
              xPosition = (int)leftSidePosition;
              xSpeed = -xSpeed + ballDegradation;
          }
          if(xPosition &gt;= (rightSidePosition - diameter) &amp;&amp; xSpeed &gt; 0) 
          {
              xPosition = (int)(rightSidePosition - diameter);
              xSpeed = -xSpeed + ballDegradation;
          }
          draw();
      }
          public void keyPressed(KeyEvent e) {
              int keyCode = e.getKeyCode();
              switch( keyCode ) { 
          case KeyEvent.VK_UP:
              ySpeed = -ySpeed --;
              break;
          case KeyEvent.VK_DOWN:
              ySpeed = -ySpeed ++;
              break;
          case KeyEvent.VK_LEFT:
              xSpeed = xSpeed --;
              break;
          case KeyEvent.VK_RIGHT :
              xSpeed = xSpeed ++;
              break;
       }
      } 
      }
  </code></pre>