Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Getting pixel information for mouse movement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting pixel information for mouse movement" Watch "Getting pixel information for mouse movement" New topic
Author

Getting pixel information for mouse movement

Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29
Hi all

I m interested to know if it is possible to get pixel information about wherever mouse hovers on a button or Panel inside a JFrame. Any help is highly appreciated.


Thanks

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, it is.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It just occurred to me that, although you didn't ask, you might want to know how to do that. If you are interested, you could look at How to Write a Mouse-Motion Listener.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

It also depends on what you mean by pixel information.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

A MouseListener or MouseMotionListener will return the location in pixels inside the component itself. That means that the top-left corner of the component is (0,0). To translate this to the screen itself you can check out which method of SwingUtilities to use for that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Rob Prime wrote:A MouseListener or MouseMotionListener will return the location in pixels inside the component itself. That means that the top-left corner of the component is (0,0). To translate this to the screen itself you can check out which method of SwingUtilities to use for that.

The MouseEvent can provide that information without use of SwingUtilities.

I'm still not clear as to whether the OP wants to detect the location or the color of the pixel under the mouse.
Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29

@paul : Yes... what I meant was how to do that . Thanks for giving link !

@ Burke: At this time, I had pixel location in my mind. But is it possible to get pixel color information ? If we can do that... it would be worth paying attention for. Can we do that ?

@ Rob: I will check package mentioned by you !

Thanks !!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Darryl Burke wrote:
Rob Prime wrote:A MouseListener or MouseMotionListener will return the location in pixels inside the component itself. That means that the top-left corner of the component is (0,0). To translate this to the screen itself you can check out which method of SwingUtilities to use for that.

The MouseEvent can provide that information without use of SwingUtilities.

Ah yes, I forgot, they added this in Java 6.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Jatin Dhingra wrote:@ Burke: At this time, I had pixel location in my mind. But is it possible to get pixel color information ? If we can do that... it would be worth paying attention for. Can we do that ?

The only way I know to get the pixel colour is from an Image. There are a few ways to get an Image for a specific location. I think the easiest way is to use java.awt.Robot and its createScreenCapture method. Note that the rectangle to take the screen shot of is relative to the screen. With the returned BufferedImage you can then call getRGB(int x, int y). So to get the colour of exactly one pixel, at (x, y) in screen coordinates:
Jatin Dhingra
Greenhorn

Joined: Jun 28, 2010
Posts: 29
@ Rob: Thanks a ton !!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting pixel information for mouse movement
 
Similar Threads
Pushing A Button on a Webpage
grab pixel in java Swing
How can i find the color of the pixel
curves and the html5 canvas
How to show TooltipText with different text