开发者

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 th开发者_开发百科is,

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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜