Results 1 to 4 of 4
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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 06:18 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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.

  3. #3
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default 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?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Fun With the Mouse

    Quote Originally Posted by CuppaCoffee View Post
    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?
    Yes, get the vector which not only has magnitude (speed) but also direction.

    Quote Originally Posted by CuppaCoffee View Post
    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?
    I was only referring to how you need to deal with threading to animate the ball.

Similar Threads

  1. Need some help with mouse listener
    By Serialcek in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2012, 11:20 AM
  2. Need Help with Mouse Listener!!!!
    By Doggir in forum New To Java
    Replies: 11
    Last Post: 05-06-2011, 12:48 AM
  3. Is this Mouse program possible?
    By gblax123 in forum New To Java
    Replies: 4
    Last Post: 03-26-2011, 11:33 AM
  4. Mouse
    By PhQ in forum AWT / Swing
    Replies: 18
    Last Post: 08-18-2010, 05:05 PM
  5. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 05:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •