【问题标题】:How to move a mouse smoothly throughout the screen by using java?如何使用java在整个屏幕上平滑移动鼠标?
【发布时间】:2012-03-12 08:22:51
【问题描述】:

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

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

【问题讨论】:

    标签: java eclipse awt awtrobot


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答!虽然它不被提问者接受,但它确实帮助了我:-) 但你基本上可以摆脱睡眠延迟,因为如果有很多步骤,它会变得非常慢。
    • 一种改进是测量唤醒和移动需要多长时间,然后睡眠 (dt - timeSinceLastMove) 而不是总是使用 dt(这需要在睡眠之前进行移动,并且可能从第 0 步而不是第 1 步开始)。
    • 像魅力一样工作! :)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-18
        • 1970-01-01
        • 2018-09-27
        • 2021-08-21
        相关资源
        最近更新 更多