Canvas
class in JavaFX is a very flexible platform for drawing whatever one would like. It is not that difficult to use, but there are a few elements that are a bit confusing.To take advantage of this example, start by creating an FXML file containing a
Canvas
and a Button
. Then connect it to the CanvasDemoController class below:package canvas; import javafx.fxml.FXML; import javafx.scene.canvas.Canvas; public class CanvasDemoController { @FXML Canvas canvas; @FXML void move() { d.move(10, 10); refresh(); } void refresh() { canvas.getGraphicsContext2D() .clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); d.draw(canvas); } Drawing d = new Drawing(); @FXML void initialize() { canvas.setOnMouseClicked(event -> { d.teleport(event.getX(), event.getY()); refresh(); }); } }
The class above references the
Drawing
class, shown below:package canvas; import javafx.scene.canvas.Canvas; public class Drawing { private double x, y, radius; public Drawing() { x = y = 0.0; radius = 10.0; } public void move(double dx, double dy) { x += dx; y += dy; } public void teleport(double x, double y) { this.x = x; this.y = y; } public void draw(Canvas canvas) { canvas.getGraphicsContext2D() .fillOval(x - radius, y - radius, radius*2, radius*2); } }
The combined example illustrates several useful ideas:
- Creating graphics on a
Canvas
is not that difficult, but theGraphics2DContext
object must be acquired from it in order to draw anything. - Drawings on a
Canvas
persist until erased. Erase and redraw as necessary to animate. - Mouse click events are pretty easy to handle, as the coordinates of the mouse click are easily obtained from the
event
parameter. However, we need to code the event handler explicitly in the controller class.
No comments:
Post a Comment