Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes KeyListener woes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener woes" Watch "KeyListener woes" New topic
Author

KeyListener woes

Dan Kottas
Greenhorn

Joined: Apr 25, 2005
Posts: 9
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

Joined: Apr 21, 2005
Posts: 410
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Dan Kottas
Greenhorn

Joined: Apr 25, 2005
Posts: 9
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

Joined: Apr 25, 2005
Posts: 9
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Apr 25, 2005
Posts: 9
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Apr 25, 2005
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener woes