Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: KeyListener implementation problem

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default KeyListener implementation problem

    I've been trying to work on a Gridworld game for my APCS class, and I've finished everything but key recognition for the Player class.
    I've spent hours trying to understand the KeyListener, but I can't.
    As you can see in the code below, I've made futile attempts to get it working
    Using specific steps, how do I fix my KeyListener problem?
    It would be great if you answered my problem in simple words, and in specific steps.
    (oh yeah, docs.oracle isn't helping me)

    import info.gridworld.actor.Actor;
    import info.gridworld.grid.Grid;
    import info.gridworld.grid.Location;
     
    import java.util.ArrayList;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import javax.swing.*;
     
    public class Player extends Actor {
     
      public void act() {
      }
     
      public void move() {
        Grid<Actor> gr = getGrid();
        if (gr == null)
          return;
        Location loc = getLocation();
        Location next = loc.getAdjacentLocation(getDirection());
        if (gr.isValid(next))
          moveTo(next);
        else
          removeSelfFromGrid();
      }
     
      public Player () {
        super();
        this.addKeyListener(this);
      }
     
      public void moveLeft() {
        setDirection(270);
        move();
      }
     
      public void moveRight() {
        setDirection(90);
        move();
      }
     
      public void moveUp() {
        setDirection(0);
        move();
      }
     
      public void moveDown() {
        setDirection(180);
        move();
      }
     
     
      public void keyPressed(KeyEvent e) {
        int keys = e.getKeyCode();
        if (keys == KeyEvent.VK_A) {
          moveLeft();
        }
        else if (keys == KeyEvent.VK_D) {
          moveRight();
        }
        else if (keys == KeyEvent.VK_W) {
          moveUp();
        }
        else if (keys == KeyEvent.VK_S) {
          moveDown();
        }
      }
      public void keyReleased(KeyEvent e) {
      }
      public void keyTyped(KeyEvent e) {
      }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: KeyListener implementation problem

    Well, your player class doesn't implement the KeyListener interface. I'd start with that.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener implementation problem

    Quote Originally Posted by Parranoia View Post
    Well, your player class doesn't implement the KeyListener interface. I'd start with that.
    I've already done that before, and I tried to go from there.
    Do I need to create a new class?

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: KeyListener implementation problem

    You don't necessarily have to, but if you wanted you could create another class that implements KeyListener and then add an instance of that new Class as a KeyListener to the Player

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: KeyListener implementation problem

    Quote Originally Posted by Parranoia View Post
    You don't necessarily have to, but if you wanted you could create another class that implements KeyListener and then add an instance of that new Class as a KeyListener to the Player
    I found an alternative to that solution. Thanks for trying, though

Similar Threads

  1. ArrayList remove() implementation problem?
    By jean28 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2013, 06:47 AM
  2. Tetris KeyListener Problem
    By Martinnikol in forum AWT / Java Swing
    Replies: 1
    Last Post: November 5th, 2011, 07:11 PM
  3. help with KeyListener
    By all_pro in forum AWT / Java Swing
    Replies: 4
    Last Post: April 14th, 2011, 06:51 AM
  4. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 07:51 PM
  5. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM