Java Reference
In-Depth Information
canvas.setOnMousePressed(event -> {
graphicsContext.clearRect(0, 0, 640, 480);
p0 = new Point2D(event.getX(), event.getY());
});
canvas.setOnMouseDragged(event -> {
Point2D p1 = new Point2D(event.getX(), event.getY());
graphicsContext.clearRect(0, 0, 640, 480);
double x = min(p0.getX(), p1.getX());
double y = min(p0.getY(), p1.getY());
double width = abs(p1.getX() - p0.getX());
double height = abs(p1.getY() - p0.getY());
graphicsContext.fillOval(x, y, width, height);
graphicsContext.strokeText("Oval(" + x + ", " + y + ", " +
width + ", " + height + ")", x, y - 10);
});
Group group = new Group(canvas);
return new Scene(group, 640, 480);
}
}
When the program in Listing 10-12 is run, and after some user interaction, the Canvas Example window in
Figure 10-15 is displayed.
Figure 10-15. The CanvasExample program
 
Search WWH ::




Custom Search