This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Moving an object such as an arc. 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 "Moving an object such as an arc." Watch "Moving an object such as an arc." New topic
Author

Moving an object such as an arc.

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
How would I go about programming an applet so that one could hit the arrow keys on the keyboard to make my arc (which is actually half a circle) move around on the applet screen?


Live And Let Learn.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
You would add a KeyListener, which reacts to the keys you're interested in, then redraws the graphics as appropriate and calls repaint/update.

Since this has nothing to do with applets specifcially, I'm moving the thread to the AWT/Swing/GUI forum.
[ April 15, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
I'm new to this part of building an applet in java. Can somebody give me a reference or something that I could work with to figure it out? I'v never used a key refference before.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
Event handling is discussed in a chapter of the Swing GUI tutorial. Even if you use AWT, not Swing, as your graphics library, the event handling of both is identical.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Heres my code:


I'm stuck here at trying to figure out how to give the first arc controls. Can somebody help me?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
1) KeyPressed() is not keyListeners's keyPressed()
2) all the values in paint() are hard-coded i.e. there will be no 'movement'

here's a (very) simple applet, when run a 'ball' will move from left to right.
use the arrow keys to change direction



and the html to run it
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Thanks! I can use this to figure mine out. Thank u so much!!
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Ok... maybe I can't. I got this far:



I commented all the unneccesary stuff away for now so that I can focus on the one arc. When I push a button (actually any button) on the keyboard, it looks like lines are going through the applet and its trying to do something but I'm not specific enough in my code to have it doing what I want it to do. For now... all I want is to move the arc left. If you want to view it the html code is:



What else do I have to do? Whats wrong with my applet?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's your applet with a few comments


and here it is with a few amendments (will only work with the left arrow)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moving an object such as an arc.