0

Hello everyone I am new to Java so i think the answer to this question is very easy but I can't find out what part i'm doing wrong.. I added a keyListener to my pacman game but somehow it won't work.. i've used the following code:

package h04PacMan;

import java.awt.event.*;

import javax.swing.*;

public class PacManBediening extends JPanel implements ActionListener, KeyListener {

private JButton links, rechts, boven, beneden;
PacMan pacman;

public PacManBediening(PacMan pacman) {

    this.pacman = pacman;

    links = new JButton("<");
    links.addActionListener(this);
    add(links);

    rechts = new JButton(">");
    rechts.addActionListener(this);
    add(rechts);

    boven = new JButton("^");
    boven.addActionListener(this);
    add(boven);

    beneden = new JButton("v");
    beneden.addActionListener(this);
    add(beneden);

}

/*
 * bediening bij een klik
 */

@Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource() == links) {

        pacman.setRichtingEnSnelheid( -10 );
        pacman.setBesturing(0);
        pacman.setView(180);
        //System.out.println("links");
    }
    else if(e.getSource() == rechts) {
        pacman.setRichtingEnSnelheid( +10 );
        pacman.setBesturing(0);
        pacman.setView(0);
        //System.out.println("rechts");
    }
    else if(e.getSource() == boven) {
        pacman.setRichtingEnSnelheid( -10);
        pacman.setBesturing(1);
        pacman.setView(90);
        //System.out.println("boven");
    }
    else {
        pacman.setRichtingEnSnelheid( +10);
        pacman.setBesturing(1);
        pacman.setView(270);
        //System.out.println("beneden");
    }


}

@Override
public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT) {
        pacman.setRichtingEnSnelheid( -10 );
        pacman.setBesturing(0);
        pacman.setView(180);
        System.out.println("links");
    }
    else if(key == KeyEvent.VK_RIGHT) {
        pacman.setRichtingEnSnelheid( +10 );
        pacman.setBesturing(0);
        pacman.setView(0);
        System.out.println("rechts");
    }
    else if(key == KeyEvent.VK_UP) {
        pacman.setRichtingEnSnelheid( -10);
        pacman.setBesturing(1);
        pacman.setView(90);
        System.out.println("boven");
    }
    else if(key == KeyEvent.VK_DOWN) {
        pacman.setRichtingEnSnelheid( +10);
        pacman.setBesturing(1);
        pacman.setView(270);
        System.out.println("beneden");
    }

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

}

can someone tell me what to add or do different?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Reshad
  • 2,570
  • 8
  • 45
  • 86
  • well if i use my arrow controls nothing happens and i also don't see the system.out.prinln I wrote under the controls so the whole method is not called i think – Reshad Oct 12 '12 at 14:38
  • 2
    I can only guess. You need to add a key listener to the component that has focus, otherwise the event will not fire. – jjnguy Oct 12 '12 at 14:41

2 Answers2

2

You're missing a couple of lines in your PacManBediening constructor.

this.pacman = pacman;
this.setFocusable(true);
this.addKeyListener(this);
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
2
  • KeyListener isn't designated for Swing JComponents, I wouldn't going this way,

  • I think there is simple and possible to lost the Focus from focusable JComponents

  • use KeyBindings as most scallable workaround, rather that "catching or hunting for Focus" for KeyListener

  • for Swing JComponents are all internal short_cuts, key shortcuts, built_in methods, notifiers, based on KeyBindings

  • code example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • as i mentioned i am new to Java so I am just trying stuff out by creating this pacman game. I experienced the problem of KeyListener and I took a look at the code example but I can't understand everything that is in the code yet so I don't know what it does or what parts are relevant yet.. :( – Reshad Oct 12 '12 at 16:04
  • @Reshad no problem with you nor personally, my answer here is to ---> `can someone tell me what to add or do different?` – mKorbel Oct 12 '12 at 16:07
  • ah I understand! :) well I will try to redesign my code to work with keyBindings but can't promise it'll work! – Reshad Oct 12 '12 at 21:55