• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Any way to get a Pixel's Color of a Graphics object?

This topic is 6357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What i''m looking for is simply for a way to be able to get the color of a pixel that i choose. i.e. Graphics gfx; ... load some .gif ... Color clr=gfx.GetPixel(x,y); Any ideas? thanks.

Share this post


Link to post
Share on other sites
Advertisement
Hello, here is one idea;(not simple but it''s one way) use the pixel grabber class.
Here is how,in the this code segment I''m assuming your using double buffering (if you don''t know what that is goto Gamelan and search for Double Buffering):

    
// pseudo-ish code

Image offScn; // image used for double buffering

int data[]; // used to store the pixel values.

//initialize data[] as

//data = new int[offScn.getWidth(this)*offScn.getHeight(this)];

....
//Here is how to grab the pixels from the screen using the

//offScn image (which represents what''s on the screen):
private void grabPixels() {
PixelGrabber pxgb;
pxgb = new PixelGrabber(offScn,0,0,
offScn.getWidth(this),offScn.getHeigh(this),
data,0,offScn.getWidth(this));
try {
pxgb.grabPixels();
}catch(InterruptedException e) {}
}

public Color getPixel(int x,int y) {
return new Color(data[y*offScn.getWidth(this) + x]);
}

...
//since the double buffer image (offScn) is not the same each
//frame because where painting to it we must grab the pixels

//each time when we want to know what color is at a given

//location


grabPixels();
Color c = getPixel(x,y);



So that''s ONE way of doing it. Keep in mind if you grab the pixels each frame it will slow the program down, so only grab them when you really need to know the color value of a given pixel.

Hope this helped, a little ().


=============================
silly programmer vb is for kids.
============================
www.thejpsystem.com

Share this post


Link to post
Share on other sites

  • Advertisement