[英]Get coordinates of one mouse click
I am using this class from Sedgewick and Wayne and I need to get the coordinates from a mouse click when it is pressed at the standard draw window. 我正在使用Sedgewick和Wayne的此类 ,当在标准绘制窗口中单击鼠标时,需要单击鼠标来获取坐标。 This class have a method boolean mousePressed that identifies when mouse is pressed and we can get the coordinates from a point when the mouse is pressed at that point.
此类具有boolean mousePressed方法,该方法标识何时按下鼠标,并且我们可以从在该点按下鼠标时的某个点获取坐标。 I've tried to modify the java code to do something similar for a mouse click:
我试图修改Java代码以使鼠标单击执行类似的操作:
public static boolean mouseClicked() {
synchronized (mouseLock) {
return mouseClicked;
}
}
. 。 .
。 .
。
public void mouseClicked(MouseEvent e) {
synchronized (mouseLock) {
mouseX = StdDraw.userX(e.getX());
mouseY = StdDraw.userY(e.getY());
mouseClicked = true;
}
}
... ...
public void mouseReleased(MouseEvent e) {
synchronized (mouseLock) {
mousePressed = false;
mouseClicked = false;
}
}
If I run the following program 如果我运行以下程序
public class test {
public static void main(String[] args) {
while(true){
if (StdDraw.mouseClicked()) {
System.out.println("hello");
}
}
}
} }
It keeps printing hello (so it means that mouseClicked is returning true. How can I make it stop? I've tried to set mouseClicked=false on released event, but didnt work. What am I doing wrong? First, I've tried to use mousePressed to get the point, but I couldn't use it to get two points. 它一直在打个招呼(因此,它意味着mouseClicked返回的是true。我如何使其停止?我试图在发布的事件上设置mouseClicked = false,但是没有起作用。我在做什么错?首先,我尝试了使用mousePressed来获得要点,但是我无法使用它来获得两点。
Thanks! 谢谢!
You have to set the mouseClicked
flag false after printing the value 打印值后,必须将
mouseClicked
标志设置为false
while (true) {
if (StdDraw.mouseClicked()) {
System.out.println("hello");
StdDraw.mouseClicked=false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.