Java Reference
In-Depth Information
// once finished clear path
pathTransition.onFinishedProperty().set((EventHandler<ActionEvent>)
(ActionEvent event) -> {
onePath.getElements().clear();
});
With the shape and transition all set up, the application needs to respond to mouse
events that will update the instance variable mentioned earlier. To do so, listen to
mouse events occurring on the Scene object. Here, you will once again rely on creat-
ing event handlers to be set on the scene's onMouseXXXProperty methods, where
the XXX denotes the actual mouse event name such as pressed, dragged, and released.
When a user draws a path, he or she will perform a mouse-press event to begin the
start of the path. To listen to a mouse-press event, create an event handler with a formal
type parameter of MouseEvent . In the example, a lambda expression is used. As a
mouse-press event occurs, clear the instance variable onePath of any prior drawn
path information. Next, simply set the stroke width and color of the path so the users
can see the path being drawn. Finally, add the starting point to the path using an in-
stance of a MoveTo object. Shown here is the handler code that responds when the
user performs a mouse press:
// starting initial path
scene.onMousePressedProperty().set((EventHandler<MouseEvent>)
(MouseEvent event) -> {
onePath.getElements().clear();
// start point in path
anchorPt = new Point2D(event.getX(),
event.getY());
onePath.setStrokeWidth(3);
onePath.setStroke(Color.BLACK);
onePath.getElements().add(new
MoveTo(anchorPt.getX(), anchorPt.getY()));
});
Once the mouse-press event handler is in place, you create another handler for
mouse-drag events. Again, look for the scene's onMouseXXXProperty() methods
that correspond to the proper mouse event that you care about. In this case, the
onMouseDraggedProperty() will be set. Inside the lambda expression, obtain
mouse coordinates that will be converted to LineTo objects to be added to the path
Search WWH ::




Custom Search