1

I have code

public static void program() throws Exception{
    BufferedImage input = null;

    long start = System.currentTimeMillis();
    while((System.currentTimeMillis() - start)/1000 < 220){
        for (int i = 1; i < 13; i++){
            for (int j = 1; j < 7; j++){
                input = robot.createScreenCapture(new Rectangle(3+i*40, 127+j*40, 40, 40));
                if ((input.getRGB(6, 3) > -7000000) && (input.getRGB(6, 3)<-5000000)){
                    robot.mouseMove(10+i*40, 137+j*40);
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
                } 
            }
        }
    }
}

On a webpage there's a matrix (12*6) and there will randomly spawn some images. Some are bad, some are good.

I'm looking for a better way to check for good images. At the moment, on good images on location (6,3) the RGB color is different from bad images. I'm making screenshot from every box (40 * 40) and looking at pixel in location (6,3)

Don't know how to explain my code any better

EDIT: Picture of the webpage. External links ok? https://i.stack.imgur.com/YAZkF.png

eltabo
  • 3,749
  • 1
  • 21
  • 33
Hans
  • 752
  • 1
  • 15
  • 29
  • 1
    *"Don't know how to explain my code any better"* I do. 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jan 10 '14 at 19:11
  • *"On a webpage"* Does that translate to 'this is an applet'? – Andrew Thompson Jan 10 '14 at 19:12
  • 1
    Okay, understood your question... How about downloading these images and using BufferedImage to check? You could use [JSoup](http://jsoup.org/) to get the URL of the image and then open a connection (`.openStream()`), load the bytes, create a [`BufferedImage`](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html) from it and inspect the pixels. – tilpner Jan 10 '14 at 19:13
  • For interaction you could still use the Robot. For more specific help we need to know what page it is, to look at the html code ourselves. – tilpner Jan 10 '14 at 19:15
  • OK so in the order i see these 1. what ? 2. No this is not an applet. I'm having the specified window open on a specific location on my screen. 3. Well i cant download them. and checking bufferedimages is even more time consuming, than checking at 1 pixel per box. 4. Wait. Making the picture – Hans Jan 10 '14 at 19:21
  • You could have told us it's a flash game! – tilpner Jan 10 '14 at 19:32
  • Well yea... I guess i forgot :) – Hans Jan 10 '14 at 19:33
  • *"External links ok?"* External links tend to go stale. Most people won't follow them, anyway. Do you realize that `imgur` is actually the site that hosts SO images? I often 'cheat' with them and instead of bothering to use the site facility to embed images, I just prefix the URL with `` as the suffix. Hey presto! Image in question.. ;) – Andrew Thompson Jan 10 '14 at 21:03

1 Answers1

1

I'm not sure what exactly the bottleneck is in your code, but I have a hunch it might be the repeated calls to robot.createScreenCapture.

You could try calling robot.createScreenCapture on the entire matrix (i.e. a large rectangle that covers all the smaller rectangles you are interested in) outside your nested loops, and then look up the pixel values at the points you are interested in using offsets for the x and y coordinates for the sub rectangles you are inspecting.

MAK
  • 26,140
  • 11
  • 55
  • 86
  • You mean i take the image of the whole play area, and from there look at the places i need to click. Click on them and repeat? – Hans Jan 10 '14 at 19:39