如何找到正在变化坐标且识别后需要点击的颜色的位置。
该程序的目的是完成游戏中的任务,需要点击不同的颜色,而这些颜色并不总是位于同一位置。
代码当前在执行程序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 ();
}
}
最佳答案
如果颜色采用连续路径而不是跳跃 read Kevin Mangold answer否则,如果它只是出现在您任何地方的颜色,我认为您有两个选择(如果背景是恒定颜色):
第一个:您可以拍摄屏幕截图,对其进行迭代并获取任何出现的颜色(或特定颜色)的坐标,然后使用 Robot lib this may help 按下它。 and this for taking screenshots
第二个:如果您不想截屏,您可以使用机器人库通过 2 个嵌套 for 循环迭代所有屏幕像素 THis may help 来迭代屏幕。
如果背景图像不恒定,您可以截取屏幕截图并将其与前一个进行比较,使用机器人库来按下差异。
额外我之前读过某处,当使用机器人库按下按钮时,最好这样做
robot.mousePress(InputEvent.BUTTON1_MASK);
机器人.延迟(1);
robots.mouseRelease(InputEvent.BUTTON1_MASK);
而不是这个
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
额外的事情您可以从软件中读取游戏内存并获取您想要的颜色坐标
关于Java Robot类鼠标移动到特定像素的位置(鼠标单击颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15687313/