Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

color detection with mouse

 
Dalibor Toth
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic