• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting pixel information for mouse movement

 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also depends on what you mean by pixel information.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Rob: Thanks a ton !!
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic