aspose file tools*
The moose likes Java in General and the fly likes color detection with mouse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "color detection with mouse" Watch "color detection with mouse" New topic
Author

color detection with mouse

Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
Can anybody tell me is it possible to detect the color of the object when passing over it with the mouse?
And how can it be done?
Bye!
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
If you mean object as in button then I don't think so (not without some fancy screen capture, or perhaps getting the location of the mouse, seeing what object it is above and then asking the object what color it is). But if you mean an image, then you can simply grap the pixels using a pixelgrabber (jav.awt.image) and then map the mouse current position to the pixel in the array. Once you have the pixel isolated you can break it up into RGB or whatever you like. I realize this is not too detailed but there are some nice image processing sites and books out there. Hope this helps.
Sean
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
What I mean is this: say you have drawn a circle, or a line, and you want to discover what color it is whith your mouse beeing over it.
Does the answer above applays to this further explanation?
Bye!
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
You should be able to create the functionality you're after. I imagine there's a better way that this, but this is just off the top of my head. Since it not so easy to get an Image object from graphics object, you should create an image, get the graphics object from that and draw in this, then use the
drawImage(Image img, int x, int y, ImageObserver observer)
method to put you graphics on the screen. So far things should work as normal. But now you have an Image object that represents the image on the screen. So use a java.image.PixelGrabber to get the pixels repesenting the onscreen image. With the right mapping of the pixel array to the on-screen image location, you can have the mouse position indicate exactly what pixel it's over. Now you can examine the pixel any way you want. The pixel grabber api shows some sample code of how to get the individual RGB values from and image. I'm sure there are some tutorial out there as well (though with a quick search, I didn't come up with much). As well, if you want a great book, have a look at Digital Image Processing: A Practical Introduction Using Java
.
Sean
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: color detection with mouse