Seudonym
Seudonym

Reputation: 562

Stopping loop after condition is met Java

I have some code that searches a predetermined area for a specific color. However, I want to take a screenshot of the area (done), search the entirety of it for the color (done), and if the color appears at all, do the search again for the same area. If the color is never found, exit the script. I can't seem to figure out where to put the if statements and brackets. What I have now doesn't quite work.

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

Upvotes: 0

Views: 128

Answers (3)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

As a general rule, once you've got three levels of nested loops, it's time to break your method into pieces. This code will look much simpler if you refactor searching for the colour out from the code that repeats the search. This could look like this.

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;
    } 
}

Here, the second method checks an image for the presence of a colour. If it finds the right colour, it returns true immediately and doesn't check any further. It only returns false once it's checked the whole image, if the colour wasn't found.

The main method just captures the required area over and over, and uses the second method to check for yellow. Once yellow isn't found, it will exit.

Upvotes: 2

SebastianK
SebastianK

Reputation: 782

I changed the while loop with an infinite loop, because the author wanted to loop until the colour was not found in the screenshot. If the colour is not found (isFound == false), the application is exited with the System.exit(1) command.

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

Upvotes: 1

Zigzagoon
Zigzagoon

Reputation: 825

Several things that might stop you from getting your expected behavior:

  1. You need to use && instead of & in your if statement.
  2. I don't think you need to set isFound = false before the loop finishes.
  3. You should break out of your loop once you have found your match.
  4. You don't need to use the second while loop, just let it terminate.

Try this:

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

Upvotes: 1

Related Questions