• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could i use pixelGrabber? The screen is being drawn as an image.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice. Thanks Rob.
 
Mike Miller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic