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. 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. I've tried to modify the java code to do something similar for a mouse click:
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.
Thanks!
You have to set the
mouseClicked
flag false after printing the value