Java Robot 类鼠标移动到特定像素的位置(鼠标单击颜色)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15687313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Robot class mouse move to position of a specific pixel (Mouse click a color)
提问by user2220444
How to find the position of a color that is changing coordinates and needs to be click after being identified.
如何找到一个正在改变坐标的颜色的位置,需要在识别后点击。
Purpose of the program complete tasks in a game, requiring the clicking of different colors which aren't always in the same position.
该程序的目的是在游戏中完成任务,需要点击不同颜色但并不总是在同一位置。
Code currently gets color of mouse's coordinates after 5 seconds of executing program
代码当前在执行程序 5 秒后获取鼠标坐标的颜色
public class RobotColorClick
{
public RobotColorClick () throws AWTException, IOException, InterruptedException
{
Robot robot = new Robot();
//Delay 5 seconds
robot.delay(5000);
//Gets color (value of red,green,blue) from the mouse position after 5 seconds
Color color = robot.getPixelColor( MouseInfo.getPointerInfo().getLocation().x
, MouseInfo.getPointerInfo().getLocation().y);
//Delay 3 seconds
robot.delay(3000);
//Mouse moves to X and Y then right click
//Problem! How to set X and Y to position color coordinates, position will change
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
public static void main(String[] args) throws AWTException, IOException,
InterruptedException
{
new RobotColorClick ();
}
}
回答by Kevin Mangold
You'll most likely need to take a screen shot image then spiral out (assuming the "color" is taking a continuous path and not jumping around) from the original location comparing the color of that pixel with the color you are looking for. Once that has been identified, do mouseMove(newX, newY)
and then the mousePress()
/mouseRelease()
methods.
您很可能需要拍摄屏幕截图图像,然后从原始位置螺旋出来(假设“颜色”采用连续路径而不是跳跃),比较该像素的颜色与您正在寻找的颜色。一旦确定,mouseMove(newX, newY)
然后执行mousePress()
/mouseRelease()
方法。
回答by Mahmoud Hossam
if the color is taking continues path and not jumping around read Kevin Mangold answerotherwise if it is just a color appearing anywhere you I think you have 2 options(in case of the background being constant color):
如果颜色是继续路径而不是跳来跳去 阅读 Kevin Mangold 的答案,否则如果它只是出现在任何地方的颜色,我认为你有 2 个选项(如果背景颜色不变):
The First one:you can take screen shot iterate over it and get coordinates of any appearing color (or a specific color)then press on it using Robot lib,this may helpand this for taking screenshots
第一个:您可以对它进行屏幕截图迭代并获取任何出现的颜色(或特定颜色)的坐标,然后使用 Robot lib 按下它,这可能会有所帮助,这对于截取屏幕截图
The second one:if you don't want to take screenshots you can use the robot lib to iterate over your screen with a 2 nested for loops iterating over all screen pixels THis may help
第二个:如果您不想截取屏幕截图,您可以使用机器人库通过 2 个嵌套的 for 循环遍历您的屏幕来遍历所有屏幕像素,这可能会有所帮助
In case ofbackground image is not constant you can take screenshot of the screen and compare it with the previous one ,use robot lib to press on the difference.
如果背景图像不是恒定的,您可以截取屏幕截图并与前一个进行比较,使用机器人库按差异。
ExtraI have read somewhere before that when pressing on a button using robot lib ,it is better to do this
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(1);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
instead of this
额外的我之前在某个地方读过,当使用机器人库按下按钮时,最好这样做robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(1);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
而不是这样做
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Extra thingyou can read the game memory from you software and get the color coordinates you want
额外的东西你可以从你的软件中读取游戏内存并获得你想要的颜色坐标