将光标发送到像素正方形
Send cursor to square of pixels
我正在尝试找到一种方法将光标发送到屏幕上的像素正方形。在这里,我有一些代码可以将它发送到特定位置:
package JavaObjects;
import java.awt.AWTException;
import java.awt.Robot;
public class MCur {
public static void main(String args[]) {
try {
// The cursor goes to these coordinates
int xCoord = 500;
int yCoord = 500;
// This moves the cursor
Robot robot = new Robot();
robot.mouseMove(xCoord, yCoord);
} catch (AWTException e) {}
}
}
有没有什么方法可以使用类似的代码建立一个范围而不是一个特定的点,这样光标就可以随机移动到已建立的正方形的某个部分?
由于您使用的是 "Square",您可能希望使用 java.awt.Rectangle class,如果您正在单击按钮,这将特别有用,因为您可以定义按钮边界而不是点。
至于随机半径,这很容易用 java.util.Random
完成
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.util.Random;
public class MoveMouse {
private static final Robot ROBOT;
private static final Random RNG;
public static void main(String[] args) {
// grab the screen size
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
// Equivalent to 'new Rectangle(0, 0, screen.width, screen.height)'
Rectangle boundary = new Rectangle(screen);
// move anywhere on screen
moveMouse(boundary);
}
public static void moveMouse(int x, int y, int radiusX, int radiusY) {
Rectangle boundary = new Rectangle();
// this will be our center
boundary.setLocation(x, y);
// grow the boundary from the center
boundary.grow(radiusX, radiusY);
moveMouse(boundary);
}
public static void moveMouse(Rectangle boundary) {
// add 1 to the width/height, nextInt returns an exclusive random number (0 to (argument - 1))
int x = boundary.x + RNG.nextInt(boundary.width + 1);
int y = boundary.y + RNG.nextInt(boundary.height + 1);
ROBOT.mouseMove(x, y);
}
// initialize the robot/random instance once when the class is loaded
// and throw an exception in the unlikely scenario when it can't
static {
try {
ROBOT = new Robot();
RNG = new Random();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
这是一个基本的演示。
您可能需要添加 negative/out-of-range 值检查等,这样它就不会尝试点击离开屏幕。
我正在尝试找到一种方法将光标发送到屏幕上的像素正方形。在这里,我有一些代码可以将它发送到特定位置:
package JavaObjects;
import java.awt.AWTException;
import java.awt.Robot;
public class MCur {
public static void main(String args[]) {
try {
// The cursor goes to these coordinates
int xCoord = 500;
int yCoord = 500;
// This moves the cursor
Robot robot = new Robot();
robot.mouseMove(xCoord, yCoord);
} catch (AWTException e) {}
}
}
有没有什么方法可以使用类似的代码建立一个范围而不是一个特定的点,这样光标就可以随机移动到已建立的正方形的某个部分?
由于您使用的是 "Square",您可能希望使用 java.awt.Rectangle class,如果您正在单击按钮,这将特别有用,因为您可以定义按钮边界而不是点。
至于随机半径,这很容易用 java.util.Random
完成import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.util.Random;
public class MoveMouse {
private static final Robot ROBOT;
private static final Random RNG;
public static void main(String[] args) {
// grab the screen size
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
// Equivalent to 'new Rectangle(0, 0, screen.width, screen.height)'
Rectangle boundary = new Rectangle(screen);
// move anywhere on screen
moveMouse(boundary);
}
public static void moveMouse(int x, int y, int radiusX, int radiusY) {
Rectangle boundary = new Rectangle();
// this will be our center
boundary.setLocation(x, y);
// grow the boundary from the center
boundary.grow(radiusX, radiusY);
moveMouse(boundary);
}
public static void moveMouse(Rectangle boundary) {
// add 1 to the width/height, nextInt returns an exclusive random number (0 to (argument - 1))
int x = boundary.x + RNG.nextInt(boundary.width + 1);
int y = boundary.y + RNG.nextInt(boundary.height + 1);
ROBOT.mouseMove(x, y);
}
// initialize the robot/random instance once when the class is loaded
// and throw an exception in the unlikely scenario when it can't
static {
try {
ROBOT = new Robot();
RNG = new Random();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
这是一个基本的演示。
您可能需要添加 negative/out-of-range 值检查等,这样它就不会尝试点击离开屏幕。