Class GraphicsContext

java.lang.Object
javafx.scene.canvas.GraphicsContext

public final class GraphicsContext extends Object
This class is used to issue draw calls to a Canvas using a buffer.

Each call pushes the necessary parameters onto the buffer where they will be later rendered onto the image of the Canvas node by the rendering thread at the end of a pulse.

A Canvas only contains one GraphicsContext, and only one buffer. If it is not attached to any scene, then it can be modified by any thread, as long as it is only used from one thread at a time. Once a Canvas node is attached to a scene, it must be modified on the JavaFX Application Thread.

Calling any method on the GraphicsContext is considered modifying its corresponding Canvas and is subject to the same threading rules.

A GraphicsContext also manages a stack of state objects that can be saved or restored at anytime.

The GraphicsContext maintains the following rendering attributes which affect various subsets of the rendering methods:

List of Rendering Attributes
Attribute Save/Restore? Default value Description
Common Rendering Attributes
Clip Yes No clipping An anti-aliased intersection of various clip paths to which rendering is restricted.
Global Alpha Yes 1.0 An opacity value that controls the visibility or fading of each rendering operation.
Global Blend Mode Yes SRC_OVER A BlendMode enum value that controls how pixels from each rendering operation are composited into the existing image.
Transform Yes Identity A 3x2 2D affine transformation matrix that controls how coordinates are mapped onto the logical pixels of the canvas image.
Effect Yes null An Effect applied individually to each rendering operation.
Fill Attributes
Fill Paint Yes BLACK The Paint to be applied to the interior of shapes in a fill operation.
Stroke Attributes
Stroke Paint Yes BLACK The Paint to be applied to the boundary of shapes in a stroke operation.
Line Width Yes 1.0 The width of the stroke applied to the boundary of shapes in a stroke operation.
Line Cap Yes SQUARE The style of the end caps applied to the beginnings and ends of each dash and/or subpath in a stroke operation.
Line Join Yes MITER The style of the joins applied between individual segments in the boundary paths of shapes in a stroke operation.
Miter Limit Yes 10.0 The ratio limit of how far a MITER line join may extend in the direction of a sharp corner between segments in the boundary path of a shape, relative to the line width, before it is truncated to a BEVEL join in a stroke operation.
Dashes Yes null The array of dash lengths to be applied to the segments in the boundary of shapes in a stroke operation.
Dash Offset Yes 0.0 The distance offset into the array of dash lengths at which to start the dashing of the segments in the boundary of shapes in a stroke operation.
Text Attributes
Font Yes Default Font The font used for all fill and stroke text operations.
Text Align Yes LEFT The horizontal alignment of text with respect to the X coordinate specified in the text operation.
Text Baseline Yes BASELINE The vertical position of the text relative to the Y coordinate specified in the text operation.
Font Smoothing Yes GRAY The type of smoothing (antialiasing) applied to the glyphs in the font for all fill text operations.
Path Attributes
Current Path No Empty path The path constructed using various path construction methods to be used in various path filling, stroking, or clipping operations.
Fill Rule Yes NON_ZERO The method used to determine the interior of paths for a path fill or clip operation.
Image Attributes
Image Smoothing Yes true A boolean state which enables or disables image smoothing for drawImage(all forms).

The various rendering methods on the GraphicsContext use the following sets of rendering attributes:

Rendering Attributes Table
Method Common Rendering Attributes Fill Attributes Stroke Attributes Text Attributes Path Attributes Image Attributes
Basic Shape Rendering
fillRect(), fillRoundRect(), fillOval(), fillArc() Yes Yes No No No No
strokeLine(), strokeRect(), strokeRoundRect(), strokeOval(), strokeArc() Yes No Yes No No No
clearRect() Yes [1] No No No No No
fillPolygon() Yes Yes No No Yes [2] No
strokePolygon(), strokePolyline() Yes No Yes No No No
[1] Only the Transform, Clip, and Effect apply to clearRect()
[2] Only the Fill Rule applies to fillPolygon(), the current path is left unchanged
Text Rendering
fillText(), fillText(with maxWidth) Yes Yes No Yes [3] No No
strokeText(), strokeText(with maxWidth) Yes No Yes Yes [3] No No
[3] The Font Smoothing attribute only applies to filled text
Path Rendering
beginPath(), moveTo(), lineTo(), quadraticCurveTo(), bezierCurveTo(), arc(), arcTo(), appendSVGPath(), closePath(), rect() Yes [4] No No No No No
fill() Yes [4] Yes No No Yes No
stroke() Yes [4] No Yes No Yes [5] No
clip() No No No No Yes No
[4] Transform applied only during path construction
[5] Fill Rule only used for fill() and clip()
Image Rendering
drawImage(all forms) Yes No No No No Yes
Miscellaneous
applyEffect(), PixelWriter methods No No No No No No

Example:

 import javafx.scene.*;
 import javafx.scene.paint.*;
 import javafx.scene.canvas.*;

 Group root = new Group();
 Scene s = new Scene(root, 300, 300, Color.BLACK);

 final Canvas canvas = new Canvas(250,250);
 GraphicsContext gc = canvas.getGraphicsContext2D();

 gc.setFill(Color.BLUE);
 gc.fillRect(75,75,100,100);

 root.getChildren().add(canvas);
 
Since:
JavaFX 2.2
See Also: