How to move a mouse smoothly throughout the screen by using java?
Asked Answered
G

3

5

There is a mouseMove()method that makes the pointer jump to that location. I want to be able to make the mouse move smoothly throughout the screen. I need to write a method named mouseGLide() which takes a start x, start y, end x, end y, the total time the gliding should take, and the number of steps to make during the glide. It should animate the mouse pointer by moving from (start x, start y) to (end x, start y) in n steps. The total glide should take t milliseconds.

I don't know how to get started can anyone help me get started on this? Can anyone just tell me what steps I need to do in order to make this problem work.

Graeco answered 22/2, 2012 at 0:30 Comment(0)
A
29

To start off, let's just write out an empty method where the parameters are as you defined in your question.

public void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) {

}

Next, let's create a Robot object and also calculate 3 pieces of information that'll help your future calculations. Don't forget to catch the exception from instantiating Robot.

Robot r = new Robot();
double dx = (x2 - x1) / ((double) n);
double dy = (y2 - y1) / ((double) n);
double dt = t / ((double) n);

dx represents the difference in your mouse's x coordinate everytime it moves while gliding. Basically it's the total move distance divided into n steps. Same thing with dy except with the y coordinate. dt is the total glide time divided into n steps.

Finally, construct a loop that executes n times, each time moving the mouse closer to the final location (taking steps of (dx, dy)). Make the thread sleep for dt milliseconds during each execution. The larger your n is, the smoother the glide will look.


Final result:

public void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) {
    try {
        Robot r = new Robot();
        double dx = (x2 - x1) / ((double) n);
        double dy = (y2 - y1) / ((double) n);
        double dt = t / ((double) n);
        for (int step = 1; step <= n; step++) {
            Thread.sleep((int) dt);
            r.mouseMove((int) (x1 + dx * step), (int) (y1 + dy * step));
        }
    } catch (AWTException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Anjaanjali answered 22/2, 2012 at 1:7 Comment(3)
thanks for your answer! although its not accepted by the asker, it really helped me :-) but you can basically get rid of the sleep delay, because it gets really slow if there are a lot of steps anyways.Wakeless
a refinement would be to measure how long the waking-up-and moving takes, and then sleeping for (dt - timeSinceLastMove) instead of always using dt (this would require doing the move before the sleep, and probably starting from step 0 instead of step 1).Sutherlan
Works like charm! :)Rabbi
E
5

For all those who have been struggling with the programming as I did before, this is how you implement this method and use it correctly:

    public class gliding_the_mouse {

        public void mouseGlide(int x1, int y1, int x2, int y2, int t, int n) {
            try {
                Robot r = new Robot();
                double dx = (x2 - x1) / ((double) n);
                double dy = (y2 - y1) / ((double) n);
                double dt = t / ((double) n);
                for (int step = 1; step <= n; step++) {
                    Thread.sleep((int) dt);
                    r.mouseMove((int) (x1 + dx * step), (int) (y1 + dy * step));
                }
            } catch (AWTException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
        }    
        public static void main (String[] args) { // program initialization
            gliding_the_mouse x = new gliding_the_mouse(); // we declare what are we are going to use
    
            x.mouseGlide(400,500,700,700,5000,10000); // the smaller the time value, the faster the mouse glides. 

        } 
    } 
Esculent answered 11/1, 2019 at 19:3 Comment(0)
G
1

Using no. of steps may not be ideal in every situation. Below is the code which moves the mouse by some given pixel amount.

The idea of this logic, is we provide the start point and end point and:

  • we get the straight line equation for the two points
  • we identify the points (pixel jump) along the line
  • we move the mouse to each idenfied point on the line using a very minimal delay to replicate the general mouse movement.
/**
 * Moves mouse from (x1,y1) to (x2, y2).
 *
 * @param x1 initial x
 * @param y1 initial y
 * @param x2 x
 * @param y2 y
 */
private static void moveTo(final int x1, final int y1, final int x2, final int y2) {
    int pixelJump = 10;
    final double xSqu = (x2 - x1) * (x2 - x1);
    final double ySqu = (y2 - y1) * (y2 - y1);
    final double lineLength = Math.sqrt(xSqu + ySqu);
    double dt = 0;
    while (dt < lineLength) {
        dt += pixelJump;
        final double t = dt / lineLength;
        final int dx = (int) ((1 - t) * x1 + t * x2);
        final int dy = (int) ((1 - t) * y1 + t * y2);
        r.mouseMove(dx, dy);
        r.delay(1); // Increase this number if you need to delay the mouse movement
    }
    r.mouseMove(x2, y2);
}
Gudrun answered 8/12, 2021 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.