From d6bbc2c4e5b20610d07078904e4f0328864d0cf8 Mon Sep 17 00:00:00 2001 From: Xuitty Date: Tue, 2 May 2023 16:49:48 +0800 Subject: [PATCH] change to use the system resolution instead of fixed resolution(100x100) --- src/com/test/MyThread.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/com/test/MyThread.java b/src/com/test/MyThread.java index feb2b53..f73fed7 100644 --- a/src/com/test/MyThread.java +++ b/src/com/test/MyThread.java @@ -1,6 +1,8 @@ package com.test; import java.awt.AWTException; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; import java.awt.MouseInfo; import java.awt.Robot; @@ -18,6 +20,9 @@ public MyThread(Long time) { @Override public void run() { + GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + int maxWidth = gd.getDisplayMode().getWidth(); + int maxHeight = gd.getDisplayMode().getHeight(); Robot robot = null; try { robot = new Robot(); @@ -32,8 +37,8 @@ public void run() { int x1 = MouseInfo.getPointerInfo().getLocation().x; int y1 = MouseInfo.getPointerInfo().getLocation().y; if (x == x1 && y == y1) { - robot.mouseMove((int) Math.round(Math.random() * 100 + 0.1D), - (int) Math.round(Math.random() * 100 + 0.1D)); + robot.mouseMove((int) Math.floor(Math.random() * maxWidth) + 1, + (int) Math.floor(Math.random() * maxHeight) + 1); x1 = MouseInfo.getPointerInfo().getLocation().x; y1 = MouseInfo.getPointerInfo().getLocation().y; System.out.println(++times);