如何通过使用Java的平稳移动鼠标整个屏幕? [英] How to move a mouse smoothly throughout the screen by using java?

查看:225
本文介绍了如何通过使用Java的平稳移动鼠标整个屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个的mouseMove()方法,使指针跳转到该位置。我希望能够使小鼠在整个屏幕顺利地移动。我需要编写一个名为mouseGLide()方法,它需要一个启动X,启动Y,最终的x,结束Y,总时间的下滑应该采取,和步数下滑期间进行。它应该由移动(启动x,启动Y)以动画鼠标指针(完X,开始Y)n的步骤。总滑翔应采取T毫秒。

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.

推荐答案

要开始了,让我们只写出其中的参数,你在你的问题中定义一个空方法。

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) {

}

接下来,让我们创建一个机器人对象,也算3块那会帮助你的未来计算的信息。不要忘了从实例机器人捕捉异常。

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 重新presents在鼠标的x差分坐标每次它同时滑翔运动。基本上它的总移动距离分为 N 步骤。与同样的事情DY 除了与y坐标。 DT 是总滑行时间分为 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.

最后,构建一个循环,执行 N 次,每次移动鼠标接近最终位置(以中(DX,DY)步)。使每个执行期间 DT 毫秒为单位的线程休眠。越大你的 N 是,平滑滑行的外观。

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.

最终结果:

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();
    }
}

这篇关于如何通过使用Java的平稳移动鼠标整个屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆