aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How do i find out what color a certain pixel is? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do i find out what color a certain pixel is?" Watch "How do i find out what color a certain pixel is?" New topic
Author

How do i find out what color a certain pixel is?

Mike Miller
Greenhorn

Joined: Mar 11, 2002
Posts: 5
I'm writing a little applet using awt that draws a number of line of the screen. The lines are drawn as single points and move around the screen using velocity vectors. When they hit the edge they bounce at random. I need to detect when a line hits another line. I thought that if i look ahead of the line to see if the color is not the default background then the line could bounce. So what i need is a method that accepts x,y co-ords and returns the color at those co-ords.
With thanks.
Mike Miller
Greenhorn

Joined: Mar 11, 2002
Posts: 5
Could i use pixelGrabber? The screen is being drawn as an image.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
This idea is perhaps not as slick as being able to test a pixel for its color...
What about maintaining an array of the pixels that you have already drawn to? Then just test the upcoming pixel with the current pixels in your array to determine if you should bounce or not.
Just an idea.
Good Luck.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Just looking at the JavaDocs:
java.awt.Robot rb = new java.awt.Robot();
Color aPixelColor = rb.getPixelColor(0,0);//gets the color of the pixel at screen coordinate 0,0.
Hope this helps!


Rob
SCJP 1.4
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Very nice. Thanks Rob.
Mike Miller
Greenhorn

Joined: Mar 11, 2002
Posts: 5
in the end i used this...
class mikes extends Object {
public static boolean handlesinglepixel(int x, int y, int pixel,Color c) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
int blue2,red2,green2;
//System.out.println(red+"|"+green+"|"+blue);
red2 = c.getRed();
green2 =c.getGreen();
blue2 = c.getBlue();
if (((red==255)&&(green==255)&&(blue==255)) || ((red==0)&&(green==0)&&(blue==0)) || ((red==red2)&&(green==green2)&&(blue==blue2)) )
return true;
else return false;
}
public static boolean handlepixels(Image img, int x, int y, int w, int h,Color c) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return false;
}/*
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}*/
boolean white = true;
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
if (white) white = handlesinglepixel(x+i, y+j, pixels[j * w + i],c);
}
}
return white;
}
}

thanks to all for replying.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i find out what color a certain pixel is?
 
Similar Threads
V.New and in need of assistance
DIV Popup Location
get the word by position
Multiline JLabel?
maze solve