Why am I getting this compile error message from the code below?
(The program moves an arrow round in 4 directions depending on which arrow key is pressed on the keyboard:d)
Direction.java:41: error: DirectionPanel.DirectionListener is not abstract and does not override abstract method keyReleased(KeyEvent) in KeyListener
private class DirectionListener implements KeyListener {
//Direction.java
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
class DirectionPanel extends JPanel {
private final int WIDTH = 300, HEIGHT = 200;
private final int JUMP = 10; //increment for image movement
private final int IMAGE_SIZE = 31;
private ImageIcon up, down, right, left, currentImage;
private int x, y;
//constructor:
public DirectionPanel () {
addKeyListener (new DirectionListener());
x =WIDTH / 2;
y = HEIGHT / 2;
up = new ImageIcon ("arrowUp.gif");
down = new ImageIcon ("arrowDown.gif");
left = new ImageIcon ("arrowLeft.gif");
right = new ImageIcon ("arrowRight.gif");
currentImage = right;
setBackground (Color.black);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setFocusable(true);
}
//draws
public void paintComponent (Graphics page) {
super.paintComponent (page);
currentImage.paintIcon (this, page, x, y);
}
//represents the listener for keyboard activity
private class DirectionListener implements KeyListener {
public void keyPressed (KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_UP:
currentImage = up;
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right;
x += JUMP;
break;
}
repaint();
}
//empty definitions for unused methods
private void KeyTyped (KeyEvent event) {}
private void KeyReleased (KeyEvent event) {}
}
}
public class Direction {
public static void main (String[] args) {
JFrame frame = new JFrame ("Direction");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new DirectionPanel());
frame.pack();
frame.setVisible(true);
}
}