Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mouse events. Mouse Listners

Status
Not open for further replies.

Knackers

Technical User
Apr 26, 2001
59
AU
Hi - I am a Java newbie and I need some help in getting started on a problem. I want to create a swing applet thatHas a circle that moves under mouse control. Thats is to say, that when you click on the applet, the circle will start moving toward the position that the mouse was clicked and continue moving in that direction until either the mouse is clicked again or hits the boundary of the applet.

I am comfortable with actually creating the applet and the circle, but would be most appreciative if anyone could give me any ideas on how to get the movement going and how the action listner might work.

Cheers
 
Hi Knackers,

Java receives MouseEvents from your system. There are two listeners for MouseEvent; MouseListener and MouseMotionListener. MouseListener just gets the click events whereas MouseMotionListener gets every change in position of the mouse. Normally you use MouseListener.

There are a couple of tutorials at the Sun site that might be a good starting point and you may be able to adapt them to your described purpose.

scrat


 
The key to your problem is "animation". You need to use a Timer that fires events (say every 100ms) which re-draws the circle in a new position (heading toward the mouse).

The MouseListener is a red-herring, as this simple starts the animation. You will nee to access the current location of the mouse at each call to the re-draw method.

As a clue look at the javax.swing.Timer class. ;-)
 
Thanks to both theKobler and scrat for your replies.
I haven't had the chance to do any further work on this, but at least now I have something to go on! Will post again if I need further ideas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top