Check if color is displayed anywhere on the screen

I can successfully check to see if a color exist at an exact location ie 500,500 pixels successfully by:

import java.awt.*;

import java.awt.event.*;

import java.lang.Object;

import java.awt.Robot;

publicstaticvoid main(String[] args)throws AWTException{

Robot robot =new Robot();

Color DaColor;

Color greenn =new Color(0, 204, 51);

DaColor = robot.getPixelColor(500, 500);

if (DaColor.equals(greenn){ System.out.print("green is displayed at 500,500")}

}

But I really want to take it a step further and be able to detect if a certain exact RBG value color is displayed anywhere on the screen and its pixel coordinates.

Thanks in advance.

[1229 byte] By [dfasdfsdafsadfasdfa] at [2007-11-27 2:30:57]
# 1
I'll bite: why the devil do you need to do this? What are you trying to do? What is your goal?
DrLaszloJamfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 2
why can't you just get the screensize from Toolkit, create a double for loop and check all pixels?
tjacobs01a at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 3

Without getting too unneccisarely in depth im trying to automate keeping display computers at a store from popping up this annoying screen that you always have to click out of. I have implemented, with the code above, successfully, clicking the close button whenever it popes up, the problem is they have many different resolutions and clicking the button based off of pixel coordinates wont work for must of them

dfasdfsdafsadfasdfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 4
So you're looking for the close button based on its color? What if the mutually-assured-thermonuclear-destruction button is the same color?
DrLaszloJamfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 5

public class PixelTest

{

public static void main(String[] args)

{

try

{

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

Robot r = new Robot();

Color c ;

for (int x = 0; x < (int) d.getWidth(); x++)

{

for (int y=0; y<(int) d.getHeight(); y++)

{

c = r.getPixelColor(x, y) ;

System.out.println("red=" + c.getRed() + " : green=" + c.getGreen() + " : blue=" + c.getBlue()) ;

}

}

}

catch (AWTException e)

{

e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

}

}

}

This spins through all of the pixels on the screen and displays the rgb values for each pixel. Using this as a starting point you should be able to do something similar for your implementation.

PS.

puckstopper31a at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 6
Then we would all be in a lot of troubleGreat thanks puckstopper
dfasdfsdafsadfasdfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 7

> public class PixelTest

Ouch -- that's taking about 14 seconds to run on my machine, and it only

checked out one monitor of my dualies :-)

Getting a BufferedImage screen capture and trawling through that is

bound to be faster. Left as an exercise for the interested student.

DrLaszloJamfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 8

I would suggest taking a screen capture and work from that rather than keep checking the screen.

Robot r = new Robot();

Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

BufferedImage img = r.createScreenCapture(new Rectangle(size));

for (int y = 0; y < size.height; y++)

{

for (int x = 0; x < size.width; x++)

{

Color c = new Color(img.getRGB(x,y));

// Do what you will with this color!

}

}

This took < 0.5 second rather than > 20 seconds for getPixelColor on a 1280x1024 screen. You'll probably be even more efficient leaving img.getRGB(x,y) as an int and comparing that.

HTH

OK, I came in second but did give an example.

Message was edited by:

ipooley

ipooleya at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 9
No disagreement doc I didn't even let it finish on my display ... But it does work and it demonstrates moving through the pixels of an area for him. That was all I was really after.PS.
puckstopper31a at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 10
That works great, you guys really know your stuff, thanks a bunch.
dfasdfsdafsadfasdfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 11
Careful with that deadman switch!
DrLaszloJamfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 12
oh and one last basic questionI successfully find the color but how do i get it to break out of both for loops? Is there a goto command in Java?
dfasdfsdafsadfasdfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 13
> oh and one last basic question> > I successfully find the color but how do i get it to> break out of both for loops? Is there a goto command> in Java?
hunter9000a at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 14

well when i'm in the inner for loop i cant say

break;

break;

to instantly get out of both..

edit: looks like using return to get back to the calling method is most effective.

Message was edited by:

dfasdfsdafsadfasdf

dfasdfsdafsadfasdfa at 2007-7-12 2:45:17 > top of Java-index,Java Essentials,Java Programming...
# 15
Also, if you are trying to optimize this, if the colored area you are looking for is 25 wide and 15 high, you only have to check every 25th pixel in the x direction and every 15th pixel in the y. That's 1/375 of the pixels -- you get the idea.
DrLaszloJamfa at 2007-7-21 20:27:28 > top of Java-index,Java Essentials,Java Programming...
# 16
By the way as Doc pointed out, this is a VERY slow way to sample the screen, and what you're trying to do with it is hideous in the worst (well almost) possible way. PS.
puckstopper31a at 2007-7-21 20:27:28 > top of Java-index,Java Essentials,Java Programming...