javacoordinatesmouseeventpoint

How to get mouse position in float or double with exact resolution?


I need to take the mouse click position in float or double, how can I do that?In mouse listener, I take the point like this,

e.getPoint();

but Point object's x and y values are integer, I need position in float or double. Any help will be appreciated.

Edit I need exact resolution.


Solution

  • getPoint() gives you integer values because that is the precision in which coordinates are specified. See http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Point.html.

    Why do you need floating-point values?

    Edit: in response to your comment, you will need to map your absolute positions to onscreen pixels, using a function like floor (always round down) or round (round to nearest int). The system has no notion of 0.2 pixels or anything like that. You can either continually truncate the decimal part of your calculations, or maintain the exact coordinates at all times and map them to pixels as needed.