• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting pixel information for mouse movement

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also depends on what you mean by pixel information.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Rob: Thanks a ton !!
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
brevity is the soul of wit - shakepeare. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic