• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving an object such as an arc.

 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I can use this to figure mine out. Thank u so much!!
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's your applet with a few comments


and here it is with a few amendments (will only work with the left arrow)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic