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);
}
}
Adding to @Sotirios Delimanolis answer that is correct, you may want to take a look to use Keybindings instead of using keyListeners that has 2 big issues, it listens for all keys and components must be focusable and in focus.
Another thing (if you still want to use keyListeners) you may want to extends KeyAdapter that provide default implementation of KeyListener methods (empty methods) therefore you don't need to override methods that you don't use like
keyReleased
orkeyTyped
.Example:
The error meesage is
So obviously your class does not override all the methods it needs to, for example
keyReleased
. If you look at your class, you haveKeyReleased
and it'sprivate
.Rename these
to
keyTyped
andkeyReleased
(note the lowercasek
) and make thempublic
since Java doesn't allow sub types to reduce the visibility of an overriden method. Then annotate each with@Override
.Annotating a method with
@Override
causes a compile error if it doesn't actually override. Section 9.6.1.4 of the JLS says: