Reputation: 513
I have a Pong game and am trying to figure out how to detect a couple of keys. Here is my paddle class:
import java.awt.geom.*;
public class Paddle extends Rectangle2D.Float{
public Paddle(int x, int y){
super.x = x;
super.y = y;
super.width = w;
super.height = h;
}
}
Here is the PongBall
class:
import java.awt.geom.*;
public class PongBall extends Ellipse2D.Float{
public PongBall(int x, int y, int w, int h){
super.x = x;
super.y = y;
super.width = w;
super.height = h;
}
}
Here is the class/area that the paddle and ball will be on:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JComponent;
public class PongPlayArea extends JComponent implements KeyListener{
public int width, height;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PongPlayArea ppa = new PongPlayArea(600, 600);
Timer t = new Timer(20, e -> {ppa.repaint();});
t.start();
frame.add(ppa);
frame.setVisible(true);
}
public PongPlayArea(int width, int height){
this.width = width;
this.height = height;
this.addKeyListener(this);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.BLACK);
g2.draw(new Rectangle2D.Float(0, 0, width, height));
g2.fill(getVisibleRect());
g2.setColor(Color.WHITE);
g2.draw(new Paddle());
}
public Dimension getPreferredSize(){
return new Dimension(width, height);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
System.out.println("Success");
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
My problem is that when ever the key is pressed (in this case, the right arrow key) nothing is happening. I don't know what is wrong. Does anyone have a solution?
EDIT: ANSWER: As suggested by camickr, Instead of use KeyListener, I have decided to use key binding, which is much more efficient.
Upvotes: 1
Views: 81
Reputation: 513
As said by camickr, key bindings is the way to go. Do not use key listeners.
Upvotes: 0
Reputation: 73
I think that the better solution is to use KeyboardFocusManager
. You may have problems (as above) with KeyListeners, because it requres to be focused, you have to register Listener and add methods to your PongPlayArea
class (which becomes very long) etc. One I had problems similar to your, and the best solution I found was KeyboardFocusManager
.
To your constructor:
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new MyDispatcher());
And create class:
private class GameDispatcher implements KeyEventDispatcher {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == 82) {
System.out.println("Success");
}
return false;
}
}
Check if it works if you want and tell me is it solved your problems.
Upvotes: 0
Reputation: 12213
You need to call .addKeyListener()
to register your KeyListener
implementation. Call it on the component on which you want to listen for key events. Probably this.addKeyListener(this);
. I would make your KeyListener implementation a separate class.
Upvotes: 3