当给定像素为白色时,机器人的行为与预测的一样。但是当我用不同的颜色测试它然后变成白色时它仍然卡在循环中:
(...)
stuck in while->else
false
(...)
可能是什么问题?这是代码:
public class test {
public static boolean ifFinished = false;
public static void main(String[] args) throws AWTException, IOException,
InterruptedException {
// Create a Robot object
Robot myRobot = new Robot();
// pick a color of given pixels
Color color = myRobot.getPixelColor(912, 487);
System.out.println("Red color of pixel = " + color.getRed());
System.out.println("Green color of pixel = " + color.getGreen());
System.out.println("Blue color of pixel = " + color.getBlue());
while (ifFinished == false) {
//Checks if color is white
if (color.getRed() == 255 && color.getGreen() == 255
&& color.getBlue() == 255) {
ifFinished = true; //if it is set ifFinished to true
System.out.println("stuck in while->if");
} else
Thread.sleep(1000);
ifFinished = false;
System.out.println("stuck in while->else");
System.out.println(ifFinished);
}
}
}
答案 0 :(得分:3)
您需要在循环内使用Robot移动获取颜色的线,否则永远不会更新。
while (ifFinished == false) {
Color color = myRobot.getPixelColor(912, 487); // <==== move inside loop
System.out.println("Red color of pixel = " + color.getRed());
System.out.println("Green color of pixel = " + color.getGreen());
System.out.println("Blue color of pixel = " + color.getBlue());
//Checks if color is white
if (color.getRed() == 255 && color.getGreen() == 255
&& color.getBlue() == 255) {
ifFinished = true; //if it is set ifFinished to true
System.out.println("stuck in while->if");
} else
Thread.sleep(1000);
ifFinished = false;
System.out.println("stuck in while->else");
System.out.println(ifFinished);
}