Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse coordinates relative to Frame

I'm trying to draw Polygons and would like to be able to click on my Frame to get MouseCoordinates so as to turn a mental Image into x/y values more quickly.

I'm using

System.out.println("("+ MouseInfo.getPointerInfo().getLocation().x +",
    "+ MouseInfo.getPointerInfo().getLocation().y +")"); 

but this give me coordinates relative to my actual screen, and not my java window.

How can I make the coordinates show up relative to the Java Window?

like image 283
Nyst Avatar asked Nov 04 '11 19:11

Nyst


1 Answers

You can convert between screen and component coordinates using the SwingUtilties class

The method convertPointFromScreen will take a screen coordinate and convert it to be relative to the component you provide.

like image 161
Aaron Avatar answered Sep 20 '22 17:09

Aaron