满足条件后停止循环Java

时间:2019-05-22 18:45:28

标签: java for-loop while-loop

我有一些代码可以在预定区域中搜索特定颜色。但是,我想截取该区域(完成)的屏幕快照,在整个区域中搜索颜色(完成),如果颜色完全显示再次对同一区域进行搜索。如果找不到颜色,请退出脚本。我似乎无法弄清楚将if语句和方括号放在哪里。我现在所拥有的还不够用。

import java.awt.*;
import java.awt.image.BufferedImage;



public class Main {
    public static void main(String[] args) throws AWTException {
        boolean isFound = true;
        while (isFound == true){
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            BufferedImage image2 = new Robot().createScreenCapture(new Rectangle(70, 102,200,222));
            isFound = false;
            for (int y = 0; y < image2.getHeight(); y++) {
                for (int x = 0; x < image2.getWidth(); x++) {
                    Color pixcolor = new Color(image2.getRGB(x, y));
                    int red = pixcolor.getRed();
                    int green = pixcolor.getGreen();
                    int blue = pixcolor.getBlue();
                    System.out.println("Red  = " + red);
                    System.out.println("Green  = " + green);
                    System.out.println("Blue  = " + blue);

                    if (red == 255 & green == 255 & blue == 0){
                        isFound = true;
                    }
                    else{
                        isFound = false;
                    }

                }

            }
        }
        while (isFound == false){
            System.exit(1);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我用无限循环更改了while循环,因为作者想循环直到在屏幕快照中找不到颜色为止。如果找不到颜色(isFound == false),则使用System.exit(1)命令退出应用程序。

import java.awt.image.BufferedImage;

public class Main {
    public static void main(String[] args) throws AWTException {
        while (true){
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            BufferedImage image2 = new Robot().createScreenCapture(new Rectangle(70, 102,200,222));
            boolean isFound = false;
            for (int y = 0; y < image2.getHeight(); y++) {
                for (int x = 0; x < image2.getWidth(); x++) {
                    Color pixcolor = new Color(image2.getRGB(x, y));
                    int red = pixcolor.getRed();
                    int green = pixcolor.getGreen();
                    int blue = pixcolor.getBlue();
                    System.out.println("Red  = " + red);
                    System.out.println("Green  = " + green);
                    System.out.println("Blue  = " + blue);

                    if (red == 255 && green == 255 && blue == 0){
                        isFound = true;
                        break;
                    }
                }
            }
            if(!isFound) {
               System.exit(1);
            }
        }
    }
}

答案 1 :(得分:1)

可能阻止您获得预期行为的几件事:

  1. 您需要在if语句中使用&&而不是&
  2. 我认为您不需要在循环结束之前设置isFound = false
  3. 找到匹配项后,您应该退出循环。
  4. 您不需要使用第二个while循环,只需终止它即可。

尝试一下:

public class Main {
    public static void main(String[] args) throws AWTException {
        boolean isFound = false;
        while (!isFound){
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            BufferedImage image2 = new Robot().createScreenCapture(new Rectangle(70, 102,200,222));
            for (int y = 0; y < image2.getHeight(); y++) {
                for (int x = 0; x < image2.getWidth(); x++) {
                    Color pixcolor = new Color(image2.getRGB(x, y));
                    int red = pixcolor.getRed();
                    int green = pixcolor.getGreen();
                    int blue = pixcolor.getBlue();
                    System.out.println("Red  = " + red);
                    System.out.println("Green  = " + green);
                    System.out.println("Blue  = " + blue);

                    if (red == 255 && green == 255 && blue == 0){
                        isFound = true;
                        break;
                    }
                }
            }
        }
        if (!isFound){
            System.exit(1);
        }
    }
}

答案 2 :(得分:1)

作为一般规则,一旦获得了三个级别的嵌套循环,就该将您的方法分解为碎片了。如果您从重复搜索的代码中重构出用于搜索颜色的代码,则该代码将看起来更加简单。看起来可能像这样。

public class BreakWhenNoYellow {

    public static void main(String[] args) {
        BufferedImage capture = null;
        Rectangle screenRegion = new Rectangle(70, 102, 200, 220);
        do {
            capture = new Robot().createScreenCapture(screenRegion);
        } while (containsColor(capture, Color.YELLOW));
    }

    private boolean containsColor(BufferedImage image, Color toFind) {
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                if (toFind.equals(new Color(image.getRGB(x, y)))) {
                   return true;
                }
            }
        }
        return false;
    } 
}

在这里,第二种方法检查图像是否存在颜色。如果找到正确的颜色,它将立即返回true,并且不再检查。如果未找到颜色,则仅在检查了整个图像后才返回false

main方法只是一遍又一遍地捕获所需区域,并使用第二种方法检查黄色。一旦找不到黄色,它将退出。