Results 1 to 4 of 4
Thread: Fun With the Mouse
- 06-13-2012, 05:15 AM #1
Fun With the Mouse
I'm having an interesting issue with MouseListeners/MouseMotionListeners, so here me out...
Let's say I have a Ball object that's an Ellipse2D.Double painted on a Graphics2D class. It has an x and y position, width and height. The ball, by default, increases by 5 pixels on the x and 5 pixels on the y. (xPixels, yPixels), so far so good.
Now, when the mouse clicks and holds the ball, the ball will follow the x and y position of the mouse. Pretty simple, more or less. Something I can handle. Where the issue comes in, is this; when I release the ball from it's drag, I want it's x and y position to continue in the same direction the mouse was initially going. I also want to add force; if the mouse was moving quickly, the ball will continue off quickly. If it was moving slowly, I want it to continue off slowly, etc.
This is quite an issue for me, are there any ideas how I could do this? I have no idea how to measure velocity in Java, so I'm wondering how I can add 'force' to the ball when I release the mouse.
Any form of help, big or small would be greatly appreciated!Last edited by CuppaCoffee; 06-13-2012 at 05:18 AM.
- 06-13-2012, 06:10 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Fun With the Mouse
The trajectory and the speed of the ball is dependent upon how hard and the direction you throw it. Sounds obvious, but in terms of implementing this in Swing, you can keep track of mouseDragged event locations (through the MouseEvent.getPoint() method), and use this information to gauge the direction and speed upon mouseReleased. How many previous points you use to calculate speed and direction is dependent upon the behavior you wish to have - either way you can start a Swing Timer upon mouseReleased to animate the movement.
- 06-13-2012, 06:15 AM #3
Re: Fun With the Mouse
So basically, for the direction, you're saying that I'm going to need the vector method and get x1, y1, and x2, y2 and calculate and determine the speed from there? Because I was thinking along those lines as well, and I was just hoping it'd be easier..
As for using a timer to animate the ball, that's not really the issue when dealing with force of the ball. Or are you referring to see how long it takes for the ball to travel from point1 to point2?
- 06-13-2012, 04:47 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Similar Threads
-
Need some help with mouse listener
By Serialcek in forum AWT / SwingReplies: 8Last Post: 01-07-2012, 10:20 AM -
Need Help with Mouse Listener!!!!
By Doggir in forum New To JavaReplies: 11Last Post: 05-05-2011, 11:48 PM -
Is this Mouse program possible?
By gblax123 in forum New To JavaReplies: 4Last Post: 03-26-2011, 10:33 AM -
Mouse
By PhQ in forum AWT / SwingReplies: 18Last Post: 08-18-2010, 04:05 PM -
Mouse Listener for mouse floating over object?
By Krooger in forum AWT / SwingReplies: 1Last Post: 11-18-2009, 04:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks