views:

260

answers:

3

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.*

+1  A: 

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.

danben
I have an animation with some objects, whose speeds, accelerations are not fixed, and I move those objects in the panel according to the displacement they have. Objects sometimes needs to move, lets say 100,2 in x coordinate, and I need this exact position.
EEE
@EEE: and how exactly is the user supposed to move the mouse with sub-pixel precision? First of all he gets no feedback that tells him if he's at 10.1 or 10.2 and then there's the tiny problem that most people can't do that kind of precision-movement anyway. It's meaningless to have that value in a precision that's higher than the users actual capability of precision.
Joachim Sauer
+1  A: 

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.

Daniel Schneller
Thanks for your answer but I need exact resolution. Please see my comment to danben 's answer.
EEE
A: 

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?

Stefan