From 5cce2364efe177c063bf46b8e2912ae5241e2066 Mon Sep 17 00:00:00 2001 From: wangbing Date: Tue, 28 Oct 2025 17:43:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/xyz/wbsite/jmacro/JMacro.java | 61 ++++++++++++--------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main/java/xyz/wbsite/jmacro/JMacro.java b/src/main/java/xyz/wbsite/jmacro/JMacro.java index b20b1c6..6199ad5 100644 --- a/src/main/java/xyz/wbsite/jmacro/JMacro.java +++ b/src/main/java/xyz/wbsite/jmacro/JMacro.java @@ -187,7 +187,7 @@ public abstract class JMacro { * @param region 区域 */ public void mouseMove(Region region) { - mouseMove(randomCenter(region)); + mouseMove(rand(region)); } /** @@ -203,7 +203,7 @@ public abstract class JMacro { * @param region 区域 */ public void mouseLeftClick(Region region) { - mouseMove(randomCenter(region)); + mouseMove(rand(region)); mouseLeftClick(); } @@ -228,7 +228,7 @@ public abstract class JMacro { * 鼠标左键双击 */ public void mouseLeftDoubleClick(Region region) { - mouseMove(randomCenter(region)); + mouseMove(rand(region)); mouseLeftDoubleClick(); } @@ -253,7 +253,7 @@ public abstract class JMacro { * @param region 区域 */ public void mouseRightClick(Region region) { - mouseMove(randomCenter(region)); + mouseMove(rand(region)); mouseRightClick(); } @@ -556,40 +556,51 @@ public abstract class JMacro { return legends; } + /** - * 获取随机中心坐标 - * - * @param region 区域 - * @return 随机中心坐标 + * @param location 坐标 + * @param radius 半径 + * @return 随机坐标 */ - public Location randomCenter(Region region) { + public Location rand(Location location, int radius) { int x1 = Mouse.at().getX(); int y1 = Mouse.at().getY(); - // 随机移动到区域中心附近(而非直接移动到中心) - Location center = region.getCenter(); - // 得到随机移动到区域中心附近的半径 - int radius = (int) (Math.min(region.getW(), region.getH()) / 2 * 0.9f); - - // 如果半径过小,则直接返回中心点 - if (radius <= 0) { - return center; - } - // 中心坐标 - int cx = center.getX(); - int cy = center.getY(); - + int lx = location.getX(); + int ly = location.getY(); // 判断鼠标是否在区域中心以内 - if (x1 >= cx - radius && x1 <= cx + radius && y1 >= cy - radius && y1 <= cy + radius) { + if (x1 >= lx - radius && x1 <= lx + radius && y1 >= ly - radius && y1 <= ly + radius) { return Mouse.at(); } - int x = center.getX() + RandomUtil.randomInt(-radius, radius); - int y = center.getY() + RandomUtil.randomInt(-radius, radius); + int x = location.getX() + RandomUtil.randomInt(-radius, radius); + int y = location.getY() + RandomUtil.randomInt(-radius, radius); return new Location(x, y); } + /** + * @param x x坐标 + * @param y y坐标 + * @param radius 半径 + * @return 随机坐标 + */ + public Location rand(int x, int y, int radius) { + return rand(new Location(x, y), radius); + } + + /** + * 获取随机中心坐标 + * + * @param region 区域 + * @return 随机中心坐标 + */ + public Location rand(Region region) { + // 得到区域中心随机半径 + int radius = (int) (Math.min(region.getW(), region.getH()) / 2 * 0.9f); + return rand(region.getCenter(), radius); + } + /** * 输入文本 *