Question

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.

Was it helpful?

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.

OTHER TIPS

From int you should be able to cast to double or float without any problems:

double x = e.getPoint().x;
double y = e.getPoint().y;

There are however methods that already do that:

double x = e.getPoint().getX();
double y = e.getPoint().getY();

Am I missing something here?

Please note, that the precision will not be higher - the mouse can only snap to full pixels, hence usually integer is fine. But if you need to calculate based on these values, the floating point representations might be useful.

You cannot get a more precise point then getPoint(), wich returns a Point object that only holds two integers. What would like to achieve with this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top