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

  • SEARCH
  • Home
  • 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 6150023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:33:08+00:00 2026-05-23T19:33:08+00:00

I have a little image in a JPanel . When i click on the

  • 0

I have a little image in a JPanel. When i click on the panel, the image have to move to that point.
This is the code of the mouse listener (in the constructor):

addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e) {
        if (moving) return;
        moveImageTo(e.getX(), e.getY());
    }
});

The moveImageTo:

public void moveImageTo(int x, int y) {
    moving = true;
    moveThread.start();
}

The thread is the problem, because I have the start position (a java.awt.Point) and the end position (another java.awt.Point), but i don’t know how to calculate the direction and the step of moving. I also think that the thread have to call the paint(Graphics g) method at every step. Anyone have any hints or links?

  • 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-23T19:33:09+00:00Added an answer on May 23, 2026 at 7:33 pm

    I am guessing you don’t want to move the image in one step based on your question but instead you want a “smooth” movement over a few miliseconds — so the image appears it is sliding into position rather than moving in one go in the new position?
    In that case you need to decide how long will this move take — e.g. 500 miliseconds and how many steps you will be doing this in — say 40 steps.
    Having the start coordinates (call them startX and startY) and end coordinates (endX and endY) then it’s easy to compute the movement at each step (this code will happen in your Runnable class which will run in the thread):

    ...
    private static final int STEPS = 40;
    private static final long INTERVAL = 500;//msec
    private static final long STEP_INTERVAL = INTERVAL / STEPS;
    ...
    private int stepX, stepY;
    ...
    public void computeMovement() {
     stepX = (endX - startX) / STEPS;
     stepY = (endY - startY) / STEPS;
    }
    ...
    public void run() {
     int currX = startX;
     int currY = startY;
     computeMovement();
     for( int i = 0; i < STEPS; i++ ) {
       currX += stepX;
       currY += stepY;
       //move image to (currX, currY)
       TimeUnit.MILISECONDS.sleep( STEP_INTERVAL );
     }
    }
    

    The above is a very rough and ready example but it’s meant to show you how you can move the image in steps within a separate thread.

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

Sidebar

Related Questions

I have a little iOS5 app that shows images. I click on the image
I have this little code to add a watermark image in a doc file,
I have a little image that is showing up as a dead link on
I have this little mixin set up: .linear-gradient(@direction:top, @color1:#fff, @color2:#000) { background-image: -webkit-linear-gradient(@direction, @color1,
I have a little query, when I click on the image to toggle the
I have a little web app that uses the Facebook like widget. This page
I have a little image, with some decoration, that I need to stick to
So on my site right now, I just have a little image that a
I have a website with a fixed background and another little image that is
I have a little CSS code as below: input{ background-image:url(css_img/input_bg.gif); padding:6px; margin:1px; width:250px; font-family:Trebuchet

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.