如何使用java在屏幕上平滑地移动鼠标?
有一个 mouseMove() 方法可以使指针跳转到该位置。我希望能够使鼠标在整个屏幕上平滑移动。我需要编写一个名为 mouseGLide() 的方法,该方法需要起始 x、起始 y、结束 x、结束 y、滑行应花费的总时间以及滑行过程中要执行的步数。它应该通过 n 步从 (start x, start y) 移动到 (end x, start y) 来对鼠标指针进行动画处理。总滑行时间应为 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,让我们编写一个空方法,其中参数如您在问题中定义的那样。
接下来,让我们创建一个 Robot 对象并计算 3 条信息,这将有助于您将来的计算。不要忘记捕获实例化 Robot 时的异常。
dx
表示鼠标在滑行时每次移动时 x 坐标的差异。基本上它是分为n
步的总移动距离。除了 y 坐标外,与 dy 相同。dt
是分为n
步的总滑行时间。最后,构建一个执行 n 次的循环,每次将鼠标移近最终位置(采取 (dx, dy) 步长)。让线程在每次执行期间休眠
dt
毫秒。n
越大,滑动看起来越平滑。最终结果:
To start off, let's just write out an empty method where the parameters are as you defined in your question.
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.
dx
represents the difference in your mouse's x coordinate everytime it moves while gliding. Basically it's the total move distance divided inton
steps. Same thing withdy
except with the y coordinate.dt
is the total glide time divided inton
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 fordt
milliseconds during each execution. The larger yourn
is, the smoother the glide will look.Final result:
对于所有像我之前一样一直在编程中苦苦挣扎的人,这就是实现此方法并正确使用它的方法:
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:
使用没有。步骤可能并不适合所有情况。下面的代码将鼠标移动给定的像素量。
这个逻辑的想法是,我们提供起点和终点,并且:
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: