4

有一个 mouseMove() 方法可以使指针跳转到那个位置。我希望能够使鼠标在整个屏幕上平滑移动。我需要编写一个名为 mouseGLide() 的方法,它需要一个开始 x、开始 y、结束 x、结束 y、滑翔应该花费的总时间,以及滑翔过程中的步数。它应该通过在 n 步内从 (start x, start y) 移动到 (end x, start y) 来动画鼠标指针。总滑行时间应该是 t 毫秒。

我不知道如何开始,有人可以帮助我开始吗?谁能告诉我为了解决这个问题我需要做哪些步骤。

4

3 回答 3

28

首先,让我们写出一个空方法,其中的参数与您在问题中定义的一样。

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

}

接下来,让我们创建一个 Robot 对象并计算 3 条信息,这将有助于您未来的计算。不要忘记从实例化 Robot 中捕获异常。

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

dx表示每次滑行时鼠标 x 坐标的差异。基本上它是分为n步骤的总移动距离。dy除了 y 坐标外,情况相同。dt是分为多个n步骤的总滑行时间。

最后,构造一个执行n次数的循环,每次将鼠标移近最终位置(采取 (dx, dy) 的步骤)。在每次执行期间使线程休眠dt几毫秒。你的越大n,滑行看起来越平滑。


最后结果:

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();
    }
}
于 2012-02-22T01:07:53.367 回答
4

对于所有像我以前一样在编程中苦苦挣扎的人,这就是您实现此方法并正确使用它的方式:

    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. 

        } 
    } 
于 2019-01-11T19:03:28.310 回答
0

使用没有。的步骤可能并非在每种情况下都是理想的。下面是将鼠标移动某个给定像素量的代码。

这个逻辑的想法是,我们提供起点和终点,并且:

  • 我们得到两点的直线方程
  • 我们确定沿线的点(像素跳跃)
  • 我们使用非常小的延迟将鼠标移动到线上的每个识别点,以复制一般的鼠标移动。
/**
 * 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);
}
于 2021-12-08T05:15:44.377 回答