respond to color events with java

851 views Asked by At

I am building a java application to solve puzzles. The way i am coding it basically the program will take a screen shot, find a pixel in the screen shot, and move the mouse though the Robot function to that position on the desktop. I understand the theory behind taking a screen shot, storing it in an array, exploring the array until a stored pixel with the right color combo pops up, and moving mouse to that position on screen, however i cannot for the life of me get the code down. If anyone knows, or could knock together a sample code that takes a screen shot, stores it in an array (or and storage device i don't know if array is the best for this particular use) finds a pixel from that array moves mouse to pixel position and then clears array, I would be amazingly great-full because this is driving me nuts!

so far i have:

public static void main(String[] args) throws Exception{

Robot robot = new Robot();

{
private static Rectangle rectangle = new Rectangle(0, 0, 1075, 700);

{
    BufferedImage image = r.createScreenCapture(rectangle);
    search: for(int x = 0; x < rectangle.getWidth(); x++)
    {
        for(int y = 0; y < rectangle.getHeight(); y++)
        {
            if(image.getRGB(x, y) == Color.getRGB(195, 174, 196))
            {
                Robot.mouseMove(x, y);
                break search;
            }
        }
    }
}

}

i am getting three errors:

  1. illegal start of expression, the indicator pointing at the get in code segment below

    private static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

  2. illegal start of expression, the indicator pointing at the Size in code segment below

    private static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

  3. ; expected indicator pointing at Rectangle rectangle

    private static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

1

There are 1 answers

3
pimaster On

Creating the screen shot and looping though it is not that hard. The Javadoc for the GraphicsDevice will tell you how to get the right screen size.

The only thing I don't think you can do is respond to "color events". You can poll the screen to see when the color has changed though.

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

public class FindColor
{
    private static Rectangle rectangle = new Rectangle(800, 600);

    public static void main(String[] args) throws Exception
    {
        Robot r = new Robot();
        BufferedImage image = r.createScreenCapture(rectangle);
        search: for(int x = 0; x < rectangle.getWidth(); x++)
        {
            for(int y = 0; y < rectangle.getHeight(); y++)
            {
                if(image.getRGB(x, y) == Color.BLACK.getRGB())
                {
                    r.mouseMove(x, y);
                    System.out.println("Found!");
                    break search;
                }
            }
        }
    }
}

-edit since the question was expanded- You don't need to write the image out to disk if you are going to check it there and then. The BufferedImage already has a way to access the individual pixels so I don't think there is a need to translate the pixel data into an array.