0

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

Community
  • 1
  • 1
user1179522
  • 145
  • 1
  • 3
  • 8
  • 2
    *"knock together a sample code"* SO is not a code factory. Why not show some effort by posting us an SSCCE of your best attempt (before this gets closed)? – Andrew Thompson Jan 31 '12 at 05:09
  • [`Zoom`](http://stackoverflow.com/a/3742841/230513) might be a starting point for constructing your [sscce](http://sscce.org/). – trashgod Jan 31 '12 at 05:15
  • @user1179522 Did you try to compile your code? – Alex K Jan 31 '12 at 07:24

1 Answers1

1

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.

pimaster
  • 1,907
  • 10
  • 11
  • but for the image capture you could search it for a specific color correct say if i wanted the mouse to click on the first bright blue pixel it finds, i could do that using the above code? By respond to color changes what i mean is i would have it keep taking shots looking at the specified pixel and when that pixel no longer = preset color then i would have the mouse click again if you get my meaning. – user1179522 Jan 31 '12 at 05:31
  • Yes, you can search for any color. You just need to modify the line "image.getRGB(x, y) == Color.BLACK.getRGB()" (I'm just looking for black) For the color change, you can either check the past value at the same spot or do complex image diffs (depending on complexity required) – pimaster Jan 31 '12 at 05:38
  • you make it sound so simple let me see how badly i can mess it up thank you – user1179522 Jan 31 '12 at 05:42