1

I have created an program that automates certain functions at my place of business but I'm trying to dummy proof it so it cannot go wrong and I have encountered an error that should not be occurring.

Basically what my method is doing is waiting until the spreadsheet has stopped being populated and auto formatted so I can do some other stuff with it. All this method has to do is take a screen shot, wait, take another screen shot and if they are the same then continue, if not wait some more.

here are the methods.

private boolean WaitTillDone() {
    BufferedImage image1;
    BufferedImage image2;
    image1 = siri.createScreenCapture(new Rectangle(0,0,width,height-80));
    wait(4000);
    image2 = siri.createScreenCapture(new Rectangle(0,0,width,height-80));
    boolean same = bufferedImagesEqual(image1,image2);
    return same;
}

public boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight() ) {
        for (int x = 0; x < img1.getWidth(); x++) {
            for (int y = 0; y < img1.getHeight(); y++) {
                if (img1.getRGB(x, y) != img2.getRGB(x, y) ) return false;
            }
            }
    }
    else {
        return false;
    }
return true;
}

Here is the loop that this is being called in.

do{
    running = WaitTillDone();
    wait(800);
}while(running);

The program loops ok but sometimes when the images are not the same and it has to wait it will enter an "infinite" loop. I say "infinite" because without any user input it will not continue. However if I press any arrow button or enter just to move the selected box in excel so that the images will be different it will continue along with no problems. So i was wondering if there was anything that I was doing wrong (besides calling a wait (a Thread.sleep) in a loop) that would cause my program to have this logic error.

EDIT: This problem is not occurring every time, only about 1/4th of the time. This problem is resolved, the mistake was the do-while condidtion. It was supposed to be

do{
    running = WaitTillDone();
    wait(800);
}while(!running);

Thank you all for your help.

  • what is the reason behind adding **wait(4000)** in function **WaitTillDone()** between **image1** and **image2** initialization? – AvmnuSng Jun 11 '13 at 15:41
  • to allow for the spreadsheet to continue being populated and formatted. If I did not have the wait there then it would take the images way too fast and give a false positive. Think of it this way, you are taking snapshots at a "regular" interval just to see when all the movement stops. – Joel Newman Jun 11 '13 at 15:42
  • I think you need to debug the differences with the images. I doubt the error is in the logic above and more likely there is something going on with the images since only one pixel has to be different. You might consider a percentage threshold like 0.1% or 100 px. Is it possible that when Excel is in some waiting state it is editing the color of the cursor or something like that. – John B Jun 11 '13 at 16:00
  • @JohnB I had originally gone with a concept like that, I had it where I would have allowances so that up to 50px could be different but as it turned out it was a critical 50px of the scroll bar getting smaller and showing movement. In theory yes that is what I would like to do but in practice, on a small screen the px value is too important. Also why would it work when I press a button to change the image? once a different cell is highlighted and it rescans it works. – Joel Newman Jun 11 '13 at 16:04
  • I don't know. Something seems to be modifying the image on a periodic basis like a cursor blinking, etc. Maybe when you move to a new cell the cursor is no longer present / blinking. I would suggest you save the two images are compare them visually. – John B Jun 11 '13 at 16:09
  • After checking them over manually I cannot find anything that is different between the two images, so I'm back to step one with my problem then. – Joel Newman Jun 13 '13 at 17:30

1 Answers1

1

Looking at this code, it seems you could rename WaitTillDone to WaitTillScreenShotDifferentIn4SecountSnapshots

As long as the image is the same as it was 4 seconds ago, it will re-enter the loop.

So if for example you enter your loop, after all of the changes have already been made, you will never exit it.

I think that what is going on here is that in a quarter of the cases Excel is too quick for you and finishes making changes before you enter your loop.

For example if your initial image is y, and you enter your do-loop at time 0:

This will be loop forever until the screenshot changes again:

time (s): Image
-1        y
0         x
1         x
2         x
3         x
4         x

This is likely what is occur most of the time:

time (s): Image
-1        y
0         y
1         y or x
2         y or x
3         y or x
4         x
Kyle Shanafelt
  • 959
  • 6
  • 5
  • thank you! I cant believe that I had made such a stupid mistake. I just changed the while to !running and it works perfectly. Thank you for spotting my mistake. – Joel Newman Jun 14 '13 at 14:05