【问题标题】:Java robot not reading color runningJava机器人不读取颜色运行
【发布时间】:2015-01-06 15:32:39
【问题描述】:

当给定像素为白色时,机器人的行为与预期一致。但是当我用不同的颜色测试它然后变成白色时,它仍然卡在循环中:

(...)
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);
    }
}

}

【问题讨论】:

    标签: java colors awtrobot


    【解决方案1】:

    您需要在循环内使用 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多