File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes simple KeyEvent to move graphic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple KeyEvent to move graphic" Watch "simple KeyEvent to move graphic" New topic

simple KeyEvent to move graphic

Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
i'm trying to move a graphic back and forth along the x axis guided by the user pressing the left and right arrow keys. i did get a clumsy solution to work with a KeyEvent, but it was based on multiple messages from the keyboard; the graphic would move once, pause, then resume sliding as the keyboard sent multiple signals.

but i'm after a graceful, solid slide effect: you press the arrow key and keep moving until you release the key. i've tried a few things but cannot get results. here's a snippet from my best attempt:

as you can see, when the arrow key is pressed i set a boolean to true and initiate a loop to move the graphic the entire length of the x axis. i set the key release event up to false the boolean, ending the loop early when the user lets up on the arrow key. this does not work as i intended: it pauses 1 second, then redraws the screen with the graphic at the opposite end of the axis.

is this a simple logic error or am i on the wrong path altogether? i'm also interested in a better solution, if anyone has a code snippet.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi, Krep,

is this a simple logic error or am i on the wrong path altogether?

some comments about your code:

The while loop is finished only when the defX variable reaches the right edge (defX = 465), irrespectively what the key event is.

I'm not sure about the !moveRight in the first line, depends on the rest of your code.
But in case it is not needed, just ask for the key and the defX variable. This is only a proposal, not tested:

I did not test my proposal, as I was too lazy to set up the graphix.
Shouldn't it ask about KeyPressed or something alike?
I'm not sure about the first condition of the first line. Will this method (getKeyCode) constantly ask if the key is pressed, or only once?


all events occur in real time
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use a timer to do your animation.

keyPressed starts the timer
keyReleased stops it
Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
thanks for the suggestions. unfortunately i still can't get what i'm looking for. two attempts are below, one for moving left and one for right:

i'm trying to set up a timer event or loop that takes a single keyboard event and starts moving the graphic right or left. it needs to ignore additional keyPressed events once movement starts, because i don't want it to pause, then restart movement.

movement must stop when a) the graphic reaches the end of its x axis, or b) when the keyReleased event happens.

both of these solutions seem like they should work, but moving left just teleports the graphic all the way to the opposite end of the x axis.

moving right seems to be interpreting each keyboard signal and processes individual keyPressed events. it moves once, stops, then begins movement again. the timer events have no effect. so, despite my booleans this event is firing once for each signal from the keyboard, not once and then looping with the timer.

i appreciate suggestions on this. i'm curious about better implementations too.
I agree. Here's the link:
subject: simple KeyEvent to move graphic
It's not a secret anymore!