1

at the moment i am making a level editor in Java. The level is in a JPanel. Inside this JPanel i put the game elements(Hero, enemy, tree, wall..)For every element i use the class.GameElement.

public class GameElement extends JPanel
 {
protected int posX, posY;
private Color color;


public GameElement(int x, int y, Color color)
{
    this.posX = x;
    this.posY = y;
    this.color = color;

}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.setColor(color);
    g.fillRect(0, 0, 50, 50);
}

}

The class GameElement is also a JPanel. I want the game element hero moveable by using the KeyListener.

import java.awt.Color;
import java.awt.event.*;

public class Hero extends Entity implements KeyListener
{
public Hero(int x, int y, Color color)
{
    super(x, y, color);


    this.setFocusable(true);
    this.addKeyListener(this);
}

public void keyPressed(KeyEvent k)
{
    if(k.getKeyCode() == KeyEvent.VK_UP)
    {
        this.move(0, -1);
    }
    else if(k.getKeyCode() == KeyEvent.VK_DOWN)
    {
        this.move(0, 1);
    }
    else if(k.getKeyCode() == KeyEvent.VK_LEFT)
    {
        this.move(-1, 0);
    }
    else if(k.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        this.move(1, 0);
        System.out.println("test");
    }
}

public void keyTyped(KeyEvent k)
{

}

public void keyReleased(KeyEvent k)
{

}
}

When i start the Level editor. The hero display succesfull on my screen but it doesn't move when i press the key button. Is it because i'm not focussing on the right JPanel?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jordi
  • 61
  • 3
  • 2
    For a game, you probably need to register a global key listener, to make sure you always get the key event. There are a few questions on SO for that, for example: http://stackoverflow.com/questions/4780910/jpanel-keylistener – Ash Jan 09 '13 at 12:08
  • 1
    http://stackoverflow.com/a/10603588/1360074 "KeyListener isn‘t designated for listening of keyboard events for Swing JComponents, use KeyBindings instead" – Nikolay Kuznetsov Jan 09 '13 at 12:14
  • 1
    possible duplicate of [Class extending KeyListener not responding to key presses](http://stackoverflow.com/questions/14221458/class-extending-keylistener-not-responding-to-key-presses) – David Kroukamp Jan 09 '13 at 16:49

2 Answers2

6

It is not that you are focusing on the wrong JPanel but rather that JPanel is not focusable and so cannot interact with KeyEvents. This is why in Swing it is better to use Key Bindings which allow you to map an Action to a KeyStroke even when a component doesn't have focus.

example

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

You may add an AWTEventListener to the default Toolkit to listen for all key events, not depending on which element has the focus (just the application must have the focus):

    AWTEventListener theListener = new AWTEventListener() {
        @Override
        public void eventDispatched(AWTEvent event) {
            System.out.println(event);
        }
    };
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    toolkit.addAWTEventListener(theListener, AWTEvent.KEY_EVENT_MASK);

Edit
AWTEvents are kind of low-level events, prefer to use ActionMap/InputMap (or just registerKeyboardAction )

user85421
  • 28,957
  • 10
  • 64
  • 87