Questions
Considering the Java program in section 6.3.3 “Dragging” of Eck (2019), describe what it does in response to specific operations of the mouse (mousePressed, mouseDragged, and mouseReleased), and how it does it.
You are encouraged to run the SimplePaint program for yourself to test its behavior. Then read through the program carefully to understand how that behavior arises.
1. Overview of the SimplePaint.java
To create a JavaFX program, the SimplePaint class extends the abstract class Application and overrides the abstract method start() in it. In the overridden start() method, the author creates a root node as the container and a canvas node inside it. The scene graph is as follows:
To respond to the mouse operations, including mouse pressed, mouse dragged and mouse released, the author uses three lambda expressions to create three corresponding listeners, namely mouse pressed listener, mouse dragged listener, and the mouse released listener, and then he registers these listeners to the mouse event source. Hence, when the mouse event occurs, the mouse event source will automatically send the event object to the registered listener, and the listener object will call the corresponding method to process the event.
2. Details of the Mouse Event Handling
2.1. Explanation of the Event Handling Methods
Mouse Pressed: The author defines a mousePressed() method to handle the mouse pressed event. In the mousePressed(), the author uses the nested if statement to handle 3 different cases: First, the click is on the white drawing area. If so, the program will save the previous location of the mouse, set the boolean variable dragging to be true, and the width and color of the line for drawing. Second, the click is one the clear button. In this case, the program will clearAndDrawPalette() to fill the canvas with white and redraw the color palette. Third, the click is on the color palette. under this circumstance, the program will call the changeColor() method to change the drawing color.
Mouse Dragged: The author defines a mouseDragged() method to handle the mouse dragged event. In the mouseDragged(), the author uses the boolean variable dragging to determine whether the user is drawing. If the value is true, it means the user is drawing. Then, the author will adjust the coordinate of the mouse to ensure drawing on the drawing area. Finally, the author calls the strokeLine() method to draw the line.
Mouse Released: The author defines a mouseReleased() method to handle the mouse released event. In the mouseReleased(), the author set the boolean variable dragging to be false. This makes sense because the drawing stops if the mouse is released.
2.2. The Processs of How Event Handling Works
The difficulty in understanding SimplePaint.java is how the mouse event handling works. Specifically, it is how the event listener is created and registered. In the program, the author uses the lambda espressions to achieve these operations (Eck, 2019, p. 288):
1 | /* Respond to mouse events on the canvas, by calling methods in this class. */ |
Although the lambda expression is a concise way to specify a listener, it is not conducive to understanding the event handling because lambda expression cannot allow us to see the things that are done behind the code to handle the event. Conversely, a better way to understand event handling is to declare a class to implement the Eventhandler interface and override the handle() method in it. And then use this class to create a listener and register it to the corresponding event source.
For example, to create a mouse pressed listener, the author uses the lambda expression: e -> mousePressed(e). In fact, this expression does the following things. First, declare a class to implement the interface EventHandler. Second, override the handle() method of the interface. Third, use the class to create a listener object. Finally, register the listener to the event source.
Appendix
I rewrote the SimplePaint.java by declaring three classes that implement the interface EventHandler. And I will attach the codes in the end, and I hope this is helpful for understanding event handling.
The Lambda Version:
1 | package discussion_assignment; |
The interface version:
1 | package discussion_assignment; |
References
1.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes