1

Ok, so I have:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle)


int w = image.getWidth(null);
int h = image.getHeight(null);
int[] rgbs = new int[w*h];
image.getRGB(0, 0, w, h, rgbs, 0, w);
// find your pixel in the rgbs array
for(int y=0;y<h;y++) {
    for(int x=0;x<w;x++) {
        if(rgbs[(y*w) + x] == mypixel) { // mypixel
            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        }
    }
}

Could someone please explain to me and perhaps give me an example of what mypixel could be? The answer is probably obvious but I'm just a bit confused. Thanks in advance

Zarkonnen
  • 22,200
  • 14
  • 65
  • 81
user1304765
  • 247
  • 3
  • 19
  • Could you clarify this question? Start by assuming that we don't know all the details of your problem that you do. – Hovercraft Full Of Eels May 04 '12 at 04:52
  • @Hovercraft Full Of Eels I need an example of what mypixel could be. What this does is capture an image the size of your entire screen, it then scans every pixel and if it matches mypixel the code below is executed. I'm confused of what to replace mypixel with. I don't know – user1304765 May 04 '12 at 04:56
  • mypixel is supposed to be a Color in the RGB format...I think. my original question: http://stackoverflow.com/questions/10441972 – user1304765 May 04 '12 at 05:02
  • So am I right in thinking that this program is meant to find a pixel of a particular color (mypixel) on the screen and click on it? – Zarkonnen May 04 '12 at 06:02

2 Answers2

2

The easiest way of getting the color value you want is to use

mypixel = new Color(red, green, blue).getRGB();
Zarkonnen
  • 22,200
  • 14
  • 65
  • 81
1

rbgs is an array of color values for the entire screen shot. This line below compares the color values of the current pixel in the picture against the color you wanted clicked. The variable is presumably a "packed" integer.
if(rgbs[(y*w) + x] == mypixel)

Community
  • 1
  • 1
HulkingUnicorn
  • 599
  • 5
  • 9