• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

KeyListener woes

 
Dan Kottas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like this program to move a picture around the screen as someone presses the arrow keys. The problem is, I can't get the KeyListener to listen to keys while it's looping in my page flipping loop.

There is a MouseListener attached to my window that works great. However, the KeyListener I added doesn't do a thing.

I've tried a number of different things and most of it is commented out. So, I apologize for the mess. If anyone could help me out I would greatly appreciate it.


Sorry to post such a big chunk of code,
Dan

[ EJFH: Added code tags. ]
[ April 26, 2005: Message edited by: Ernest Friedman-Hill ]
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the output of the code? When you press a key nothing happens? You don't see the println messages on the screen?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Events are handled in the same thread that does screen painting. You can't loop forever in the painting method, or events will never get handled -- as you've observed.

So what you must do to do animation is create a separate thread which loops and calls repaint() periodically. Then override paint() (or paintComponent(), in a Swing application) and in that method, draw the screen once based on the values of various member variables. In your event handlers, manipulate those member variables. The end result is that periodically, the screen is repainted in a different state.

For examples of this architecture, check out the "demos" directory in your JDK distribution -- there are a large number of examples.
 
Dan Kottas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Gray:
So what is the output of the code? When you press a key nothing happens? You don't see the println messages on the screen?


When I run the program it displays the picture in the center of the screen with a black background and counts the frames in the upper left corner. When I press a key nothing happens. No println or anything. The strange part is that when I left-click the mouse, it exits the program like it's supposed to. I do get the println at the beginning of the program that says "Page flipping is a go."

-Dan
 
Dan Kottas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Hi,

Welcome to JavaRanch!

Events are handled in the same thread that does screen painting. You can't loop forever in the painting method, or events will never get handled -- as you've observed.


Thank you very much for your help! I'll try this out and see if it works for me. Two things, though, that I'm still not grasping. (1)Why is it that the MouseEvent still gets handled inside of the loop, but the KeyEvent does not? (2)When I comment out the loop, it doesn't display the picture at all. I assume it's flipping to a page that hasn't been drawn on and then quits, but how do I preserve the image on screen? I thought it was the strategy.show() function that was responsible for flipping pages, but apparently not. (Obviously I don't fully understand the page-flipping yet.)
[ April 26, 2005: Message edited by: Dan Kottas ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added UBB CODE tags to your first post to make the code easier to see. I'm afraid my reply is offbase -- it doesn't explain what you're seeing at all. IN fact, you're not hogging the event thread, so I don't see why you shouldn't get KeyEvents. I've never worked with the fullscreen mode, so maybe there's a trick here.

I'm going to move this thread to our Swing/AWT forum, where maybe somebody knowledgeable like Gregg will have a look.
 
Dan Kottas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't thank you enough for helping me with all of this! I hope someone can help ease my perplexity. I haven't done anything with fullscreen before either so I suppose that could be it.

-Dan
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed from a window to an undecorated Frame (Window/JWindow is a pain when
used in conjunction with focus/key listeners)

the code in doMovement() is now processed, but I don't know if using a frame
will affect anything else you're trying to do

 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the KeyEvents -
The Viewer class implements KeyListener and provides the required methods but is never
referred to or used as a Frame; you could just as well remove 'extends Frame' from the class
declaration (and, if so, 'implements KeyListener' too, by necessity).

A new Frame is instantiated inside Viewer and added to a Window, this new Frame is not
registered with a KeyListener and so may not respond to KeyEvents. Its enclosing Window is
registered with a KeyListener (but its child Frame may be consuming the KeyEvents). If you
will register the new Frame (instead of win) with both the KeyListener and MouseListener as
Michael did they should work. Or, you could use the enclosing class Frame in lieu of the new
Frame: remove all 'frame' instance variable references so the methods will be called on the
enclosing class (a Frame by extension).

Here's one way to set up an animation in AWT. Single left mouse click starts and stops the
animation. Double click closes the Frame.
 
Dan Kottas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I haven't got back to this in a while. I appreciate all of the great help on using windows! If anyone can figure out how to do this in a full screen window or knows why it's not working for me, please let me know. I'm really curious why this is.

Thanks,
-Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic