• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Moving an object such as an arc.

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic