This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Getting pixel information for mouse movement  RSS feed

 
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

 
Sheriff
Posts: 23647
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is.
 
Paul Clapham
Sheriff
Posts: 23647
49
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.
 
Bartender
Posts: 5167
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.
 
Sheriff
Posts: 21424
94
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: 5167
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
Posts: 21424
94
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
Posts: 21424
94
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
Posts: 21424
94
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!