Dan r
Dan r

Reputation: 11

Java - Screen Coordinates to Image Coordinates

I am using a Java application to display an image on the screen. I also am using an eye-tracker device which records the absolute pixel X,Y locations where the person is looking on the screen.

However, what I need to do is convert these X,Y coordinates from the screen positions into the X,Y locations of the image. In other words, somehow I need to figure out that (just an example) 482, 458 translates to pixel 1,1 (the upper left pixel) of the image.

How can I determine the image's placement on the screen (not relative to anything)?

I saw a few posts about "getComponentLocation" and some other APIs, but in my experimentation with these, they seem to be giving coordinates relative to the window. I have also had problems with that because the 1,1 coordinate that they give is within the window, and there is actually a bar at the top of the window (that has the title and the close and minimize buttons) whose width I do not know, so I cannot easily translate.

Surely there must be a way to get the absolute pixel location on the screen of a component?

Upvotes: 1

Views: 1987

Answers (1)

If we are talking about Swing/AWT application than class java.awt.Component has method getLocationOnScreen which seemed to do what you want

And yes as @RealSkeptic mentioned in comments to question:

SwingUtilities.html#convertPointFromScreen

will do all this work for you considering components hierarchy

Upvotes: 4

Related Questions