Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyAdapter and ActionListener in Swing  RSS feed

 
Luis Henriquez-Perez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was working on implementing the game Hunting the Wumpus (http://en.wikipedia.org/wiki/Hunt_the_Wumpus) for one of my projects in College. I've got the major parts of the game already implemented. I am working on creating a start screen right now. But I've run into a strange problem. In my code the main method that draws is the PaintComponent method in the private class HTWPanel. I need the Actionlistener to listen to the "QWES" keys while the state of the game is PlayState.Playing so that the user can enter the direction he's going in. If I have the following code for my paint component, the Actionlistener works fine.


But when I try to add a start screen (by changing the background color and adding a label called "StartScreen"), the controller only listens to keys while the start screen is showing, not while the game is playing. I'm pretty sure this problem is due to some internal implementation of KeyAdacpter or ActionListener, but I'm not sure what.



I attached a screenshot of my main class, HuntTheWumpus, and a screenshot of what my game looks like so far. Each square is a different room in the Wumpus's lair. The black rectangles are door representing which direction you can go to. The rooms that are 2 squares or less from the wumpus are colored red. The hunter is represented by the black circle in the upper left hand square. His he can move left right up and down into different rooms with the "QWES" keys. If more of my code is necessary to answer my question please let me know.
Screenshot 2014-11-26 20.45.47.png
[Thumbnail for Screenshot 2014-11-26 20.45.47.png]
 
Luis Henriquez-Perez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize this is my first time posting and i realized i should have posted this in the java Swing forum.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Somebody appears to have moved your posts already.

You should not be playing the game in Swing. You should have games classes which model the cells, the Wumpus, the walls between cells, etc. Those should run as a standalone application with a public interface and no display. You should be able to display things like
Q hunter in cell 97 wumpus in cell 23
W hunter in cell 98 wumpus in cell 22
at the command line. Then you can plug in the display on top. You would have methods like
public void move(char c) . . .
in the public interface, which you pass 'W' 'Q' 'E' or 'R' to.

You do realise that you can have KeyEvents representing the keys ↓↑← and → as well. You can pass those four chars (and qwer) to the same method and get the same effect from 'Q' 'q' or '↓' (or whichever key combination you choose).

You can read about key listeners here, but you should consider whether key bindings would work better.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I try to add a start screen (by changing the background color and adding a label called "StartScreen"), the controller only listens to keys while the start screen is showing, not while the game is playing.


Key events are only dispatched to the component that has focus. So I would guess your component no longer has focus when you use the start screen.

As Campbell as already suggested you should be using Key Bindings, not a KeyListener to listen for key events. With Key Bindings you can handle the key event even when the component doesn't have focus. Check out Motion Using the Keyboard for more information and examples on this concept.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!