Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:52:15+00:00 2026-06-12T20:52:15+00:00

Hello everyone I am new to Java so i think the answer to this

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:52:16+00:00Added an answer on June 12, 2026 at 8:52 pm

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

    this.pacman = pacman;
    this.setFocusable(true);
    this.addKeyListener(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Everyone i am new to rails can anyone answer my given questions 1.
Hello everyone don't know why this code works on windows but not on linux
Hello everyone I am new to the forum and I just had a question
Hello Everyone i am new to android and i am currently stuck on this.
I solved this question my own. The filename was wrong lolz. Hello everyone! I'm
Hello everyone I can't seem to figure out why my emulator isn't working and
Hello everyone i am new to rails just wanted to ask What this line
Hello everyone please help me out regarding this this piece of code $user =
Hello everyone I am new to blackberry and I want a textfield that scroll
Hello everyone I am getting this exception: The magic number in GZip header is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.