Java 如何测量鼠标';旅行距离是多少?
我想为一个宠物项目测量鼠标的移动距离。所以我想做一个实验:假设我能得到1米的旅行距离有多精确 我试图编写测试程序,但遇到了一些困难:屏幕边框 我使用Robot类在鼠标即将到达边缘时设置鼠标位置,但我不确定这是否正确。你知道如何解除屏幕或任何其他解决方案的绑定的技术吗 以下是我的测试代码:Java 如何测量鼠标';旅行距离是多少?,java,mouse,awtrobot,measurement,Java,Mouse,Awtrobot,Measurement,我想为一个宠物项目测量鼠标的移动距离。所以我想做一个实验:假设我能得到1米的旅行距离有多精确 我试图编写测试程序,但遇到了一些困难:屏幕边框 我使用Robot类在鼠标即将到达边缘时设置鼠标位置,但我不确定这是否正确。你知道如何解除屏幕或任何其他解决方案的绑定的技术吗 以下是我的测试代码: public class App { public static void main( String[] args ) throws AWTException { Toolkit too
public class App {
public static void main( String[] args ) throws AWTException {
Toolkit toolkit = new WToolkit();
int screenResolution = toolkit.getScreenResolution();
Robot robot = new Robot();
List<Point> list = new ArrayList<Point>();
int distance = 0;
while (true) {
if(MouseInfo.getPointerInfo().getLocation().y <= 50)
{
robot.mouseMove(910, 1080);
}
list.add(MouseInfo.getPointerInfo().getLocation().getLocation());
if(list.size() >= 2) {
Point p2 = list.get(list.size()-1);
Point p1 = list.get(list.size()-2);
if(p2.getY() <= p1.getY()) {
distance += (int) Math.sqrt(0 + Math.pow(p2.y - p1.y, 2)); // sqrt(pow(x2-x1) + pow(y2-y1)) sqrt(pow(x2-x1) = 0 for just measure on Y
System.out.println(distance / screenResolution * 0.0254);
}
}
try {
Thread.sleep(200);
} catch ( Exception e) {
System.out.println("something");
}
}
}
}
公共类应用程序{
公共静态void main(字符串[]args)引发AWTException{
工具箱=新的WToolkit();
int screensolution=toolkit.getScreensolution();
机器人=新机器人();
列表=新的ArrayList();
整数距离=0;
while(true){
if(MouseInfo.getPointerInfo().getLocation().y=2){
点p2=list.get(list.size()-1);
点p1=list.get(list.size()-2);
如果(p2.getY()在该解决方案中,您将不断更新点p1和p2,以查找更新之间的总移动距离
//I think this code should work for what you are doing
double distance = Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2));
是的,我想在机器人上使用鼠标的光学传感器。使用传感器我想测量机器人移动的距离。这就是为什么我想知道使用鼠标传感器是否可行的原因。我认为在Java中无法做到这一点。aMouseInfo
只能提供鼠标指针的当前位置-它无法测量超出edg的距离你正在使用的图形设备(哎哟,我一开始打错了框)。为什么你要平方然后平方根p2.y-p1.y
?这是因为距离公式:我省略了x,因为首先我只想在直线移动时检查它,所以x不会干扰结果。那么一定要检查Math.hypot
。