Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:54:19+00:00 2026-05-28T14:54:19+00:00

I want to move a Graphics object towards the cursor when the user clicks

  • 0

I want to move a Graphics object towards the cursor when the user clicks a mousebutton, but I’ve got no idea how…

I have the position of the cursor and the Graphic object, but no clue on how i would change its X and Y to make “smooth” movement from the start location to the new point (where the user clicked)…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:54:20+00:00Added an answer on May 28, 2026 at 2:54 pm

    General approach:

    Have a model backing your graphics, and store the coordinate of each item in the model on the model.

    1. Add a MouseListener and implement mousedown
    2. In your listener grab the X,Y of the mouse event.
    3. Decide how fast you want to move the object (constant speed? Constant time to reach cursor? Acclerating? Decelerating?)
    4. Write an equation that describes the motion towards the cursor, preferably in terms of the distance remaining to be covered. For a very fast decelerating approach that could be distanceRemaining/5. (for example). Other motions require a bit of geometry/algebra, but that’s your work 🙂
    5. Create an object that encapsulates the above logic and has a flag to determine when the animation is complete.
    6. Use a “animation” thread (or timing framework as mentioned above, I haven’t used it) to perform 24 or more updates per second to the X and Y coordinates of the model based on the distance remaining in the X and Y direction and call repaint(). The thread should do this until the animation complete flag is true, and then stop calling repaint().
    7. Java will then call the paint function for you at a rate that it knows it can handle (if you call too fast it will coalesce the extra calls for you and prevent you from locking up the program with a backlog of painting calls). Your paint function should simply draw the model based on it’s current state whenever java chooses to call it.

    It’s important that you update x and y in the same thread, not independent threads. That sounds like a cool idea, but it fails because the thread scheduler will sometimes lag one thread and you can get 2 or 3 updates on one dimension without a corresponding update in the other, which will cause the object to move in a line that is not smooth. In most cases you should do all your updates to the model (for this and any other motion) in the same thread.

    Note that your x & y in your model probably need to be float or double since you don’t want to loose fractions of a pixel, especially if the motion is intended to be slow. Expressing the equation in terms of distance remaining avoids having to calculate the angle of the trajectory and using sine/cosine functions to perform the update. However, in some cases you may find it easier to calculate that angle and use sine/cosine instead, if you want a complex motion that isn’t easily transformed to be expressed in terms of distance remaining. Do what makes your code clear, optimize later if needed.

    24 updates per second is the standard frame rate for movie cameras and that number is a minimum that relates to flicker fusion of the human eye, if you update slower than that people will see the motion as jerky. For maximum smoothness you would want over 60 times a second, but that’s probably only necessary for video games and creates a much bigger performance cost. Pick the minimum update rate that you find visually satisfactory.

    It’s also a good idea to have a flag on the update thread that turns it on and off when there’s something to update so it isn’t requesting repaint for no reason when there’s nothing to animate. Be sure that the flag is marked with the volatile keyword since it will be updated by other threads. If you have multiple animations going on at one time, you might turn the thread on and off based on presence of animation objects in a list (removing them when they are complete)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display graphic file in PictureBox I have: private void btnLoad_Click(object sender,
I have a winform on which i want to allow the user to move
Upon page load I want to move the cursor to a particular field. No
I have an application and I want to move some of the tables from
I want to display an image in graphics mode. I have read the image
I want to move various parts of my app into simple scripts, to allow
I want to move about 800gb of data from an NTFS storage device to
I want to move the form title, icon and close, and help buttons from
I want to move a file with Ruby. How do I do that?
I want to move a div down a page, and I want it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.