Archived

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

vlovich

[java] getting pixel color

Recommended Posts

Is there a way to get the pixel color (RGB or otherwise) of a specified pixel? I tried using Robot class but I can''t access it because the compiler says it can''t find the class. I''m trying to use DirectModelColor, but I don''t understand what the pixel represents in DirectModelColor.getBlue (int pixel). How does 1 integer number represent a pixel? How would I get the integer number if i new the pixel location? Any help would be appreciated.

Share this post


Link to post
Share on other sites
In order to use the Robot class you need to import it as in :
import java.awt.Robot;
If the compiler still can''t find it, then you might have an old version of the JDK.

At any rate, I think getBlue(int), just tells you what the blue component of the color represented in the int you give it (under that model). I don''t think you can get actual image information from a ColorModel - the model only tells you the format of an image''s colors, not the actual coloring.

I think to get pixel colors you will need to use the Robot class unless you are getting them from your own application, in which case there may be other similarly easy ways.

Share this post


Link to post
Share on other sites
I''m using Holsoft Ready to Program (don''t know if you heard of it... made by a Canadian based company for educational purposes). Anyways, the zip file called src.zip contains all the libraries that I can import. Inside of it is the Robot.java file, but when I try to import it, it says it''s unable to.

Share this post


Link to post
Share on other sites
The src.zip file is just something that comes with the JDK. If you have the JDK installed you should be able to import all of the standard classes at least, one of which is java.awt.Robot.

If you can''t you should probably download the JDK from java.sun.com and learn a bit more about java first.

Share this post


Link to post
Share on other sites
I did try to import the files, but it says that it can''t find them. I also tried extracting the folders to the folder where the compiler also looks. It does recognize the robot class then, but it freezes during compilation on some class. Same thing happens when I add the src.zip file from a later version of java in the alternate class library.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
java.awt.Robot was introduced in version 1.3 so you''ll need at least that AND the jre plug-in if this is for an applet.

The source code (src.zip) is for reference only and cannot be re-compiled (as a whole anyway) according to Sun. But certainly bits and pieces are compilable...

One solution to your problem would be to create an image from your Component and do all of your drawing on that. Then, use PixelGrabber to ''grab'' a particular pixel or maybe the whole thing if you like. A sample is provided in the API for PixelGrabber. To transfer your image back onto the Component, you just call drawImage(...) on the component with the image.

I wouldn''t use Robot for this task personally...

Hope this helps,
-Ron

Share this post


Link to post
Share on other sites