Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple KeyEvent to move graphic

 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



Yours,
Bu.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use a timer to do your animation.

keyPressed starts the timer
keyReleased stops it
 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic