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

被刻印的时光 ゝ 提交于 2019-12-25 01:46:04

问题


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:


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.




回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/2417425/how-to-get-mouse-position-in-float-or-double-with-exact-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!