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

后端 未结 2 1057
半阙折子戏
半阙折子戏 2020-12-09 14:00

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

2条回答
  •  抹茶落季
    2020-12-09 14:36

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

提交回复
热议问题