public void mousePressed(MouseEvent e) {
//Invoked when a mouse button has been pressed on a component.
if (e.getButton() == MouseEvent.BUTTON1) {
isDown = true;
System.out.println("isDown is now true");
}
if (e.getButton() == MouseEvent.BUTTON3) {
isDown2 = true;
System.out.println("isDown2 is now true");
}
do {
Point location = MouseInfo.getPointerInfo().getLocation();
int x = location.x - (drawingPanel.getLocationOnScreen()).x;
int y = location.y - (drawingPanel.getLocationOnScreen()).y;
drawingPanel.paint(drawingPanel.getGraphics(), (x - (x % 20) - 1), (y - (y % 20) - 1), 19, 19);
} while (isDown);
System.out.println("Mouse has been pressed down.");
}
public void mouseReleased(MouseEvent e) {
//Invoked when a mouse button has been released on a component.
if (e.getButton() == MouseEvent.BUTTON1) {
isDown = false;
System.out.println("isDown is now false");
}
if (e.getButton() == MouseEvent.BUTTON3) {
isDown2 = false;
System.out.println("isDown2 is now false");
}
System.out.println("Mouse has been released.");
}
This is what I have so far. My original intentions were to design the code so that the boolean isDown would be set to true when the mouse was pressed down and then I would have the while loop run while isDown is true. If the mouse button is released, I would set isDown to false in order to terminate the while loop.
What I am messing up here? Is it not possible for two MouseEvent methods to be running at the same time? The change in the isDown boolean variable is not being registered and I have an infinite while loop on my hands.
This is a classic violation of the Event Dispatching Thread.
All UI code is run from within a single thread. All events are dispatched the UI from this same thread, meaning that should you block this thread (using a loop or other blocking operation), no events will be dispatched. This will make your program look like it’s hung.
Take a look at Concurrency in Swing for more details.
What you really should be doing is using a
MouseMoitionListenerto track drag events instead. Check out How to use Mouse Listeners for more details.This
drawingPanel.paint(drawingPanel.getGraphics(), (x - (x % 20) - 1), (y - (y % 20) - 1), 19, 19);also worries me to no end.You should never be using
getGraphicsto perform custom painting.getGraphicscan return null and is only a snap shot of the last paint cycle. Any painting done using this method will be removed/cleaned when another repaint occurs.You should be creating a custom component (such as a
JPanel) and overriding it’spaintComponentmethod and performing any painting you need in it. Check out Performing Custom Painting for more detailsExample