【发布时间】:2021-01-08 01:51:51
【问题描述】:
我正在java中制作一个供个人使用的自动点击项目,并使用以下方法从扩展MouseAdapter的类中获取点击坐标。点击是在 JFrame 上完成的。
int[] mouseCoordinates = new int[2]; //The coordinates of the click
mouseCoordinates = mouseListenerExample.getCoordinates();
final int[] baseCoordinates = mouseCoordinates; //The base coordinates (no click) which is this problem//
int[][] totalCoordinates = new int[4][2]; //An array that collects all coordinates of 4 mouse clicks
for (int i = 0; i < 4; i++){ //the goal is to get the coordinates of 4 clicks
while (mouseCoordinates[0] == baseCoordinates[0]){
mouseCoordinates = mouseListenerExample.getCoordinates(); //The problem occurs here: when mouseListenerExample.getCoordinates() changes, mouseCoordinates is changed, baseCoordinates is also changing, which it shouldnt, since there are no statements that say so.
if (mouseCoordinates[0] != baseCoordinates[0]) {
break;
}
}
totalCoordinates[i] = mouseListenerExample.getCoordinates();
mouseListenerExample.setCoordinates(baseCoordinates);
mouseCoordinates = baseCoordinates;
}
是否有一些语句正在改变我缺少的 baseCoordinates?
【问题讨论】:
-
我正在使用调试器来跟踪整个事情的进展,它正在改变两个数组的内容。如果这样可以更容易,我可以发布整个代码,我在这个网站上有点新。
-
没关系,因为我看到了这个问题。请看答案。
-
您可以使用 java.awt.Point 来保存一个 X、Y 坐标。