What the Canvas API is
How to create a canvas
How to draw on a canvas such as basic shapes, text, paths, and images
How to clear the canvas area
How to save and restore the drawing states in a GraphicsContext
What Is the Canvas API?
Canvas
GraphicsContext
A canvas is a bitmap image, which is used as a drawing surface. An instance of the Canvas class represents a canvas. It inherits from the Node class. Therefore, a canvas is a node. It can be added to a scene graph, and effects and transformations can be applied to it.
A canvas has a graphics context associated with it that is used to issue drawing commands to the canvas. An instance of the GraphicsContext class represents a graphics context.
Creating a Canvas
Drawing on the Canvas
Basic shapes
Text
Paths
Images
Pixels
Drawing Basic Shapes
fillArc()
fillOval()
fillPolygon()
fillRect()
fillRoundRect()
strokeArc()
strokeLine()
strokeOval()
strokePolygon()
strokePolyline()
strokeRect()
strokeRoundRect()
Drawing Text
void strokeText(String text, double x, double y)
void strokeText(String text, double x, double y, double maxWidth)
void fillText(String text, double x, double y)
void fillText(String text, double x, double y, double maxWidth)
Drawing Paths
beginPath()
lineTo(double x1, double y1)
moveTo(double x0, double y0)
quadraticCurveTo(double xc, double yc, double x1, double y1)
appendSVGPath(String svgpath)
arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)
arcTo(double x1, double y1, double x2, double y2, double radius)
bezierCurveTo(double xc1, double yc1, double xc2, double yc2, double x1, double y1)
closePath()
stroke()
fill()
Drawing Images
void drawImage(Image img, double x, double y)
void drawImage(Image img, double x, double y, double w, double h)
void drawImage(Image img, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)
Writing Pixels
Once you get a PixelWriter, you can write pixels to the canvas. Chapter 21 presented more details on how to write pixels using a PixelWriter.
Clearing the Canvas Area
Saving and Restoring the Drawing States
The current settings for the GraphicsContext are used for all subsequent drawing. For example, if you set the line width to 5px, all subsequent strokes will be 5px in width. Sometimes, you may want to modify the state of the graphics context temporarily and, after some time, restore the state that existed before the modification.
Draw a rectangle without any effects
Draw a string with a reflection effect
Draw a rectangle without any effects
You can fix the problem by setting the Effect to null after you draw the text. You had modified several properties for the GraphicsContext and then had to restore them all manually. Sometimes, a GraphicsContext may be passed to your code, but you do not want to modify its existing state.
A Canvas Drawing Example
Drawing on a Canvas
Summary
Through the javafx.scene.canvas package, JavaFX provides the Canvas API that offers a drawing surface to draw shapes, images, and text using drawing commands. The API also gives pixel-level access to the drawing surface where you can write any pixels on the surface. The API consists of only two classes: Canvas and GraphicsContext. A canvas is a bitmap image, which is used as a drawing surface. An instance of the Canvas class represents a canvas. It inherits from the Node class. Therefore, a canvas is a node. It can be added to a scene graph, and effects and transformations can be applied to it. A canvas has a graphics context associated with it that is used to issue drawing commands to the canvas. An instance of the GraphicsContext class represents a graphics context.
The Canvas class contains a getGraphicsContext2D() method that returns an instance of the GraphicsContext class. After obtaining the GraphicsContext of a canvas, you issue drawing commands to the GraphicsContext that performs the drawing.
Drawings falling outside the bounds of the canvas are clipped. The canvas uses a buffer. The drawing commands push necessary parameters to the buffer. The GraphicsContext of a canvas can be used from any one thread before the canvas is added to the scene graph. Once the canvas is added to the scene graph, the graphics context should be used only on the JavaFX Application Thread. The GraphicsContext class contains methods to draw the following types of objects: basic shapes, text, paths, images, and pixels.
The next chapter will discuss how to use the drag-and-drop gesture to transfer data between nodes in the same JavaFX application, between two different JavaFX applications, and between a JavaFX application and a native application.