- java.lang.Object
-
- javafx.scene.canvas.GraphicsContext
-
public final class GraphicsContext extends Object
This class is used to issue draw calls to aCanvasusing a buffer.Each call pushes the necessary parameters onto the buffer where they will be later rendered onto the image of the
Canvasnode by the rendering thread at the end of a pulse.A
Canvasonly contains oneGraphicsContext, 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 aCanvasnode is attached to a scene, it must be modified on the JavaFX Application Thread.Calling any method on the
GraphicsContextis considered modifying its correspondingCanvasand is subject to the same threading rules.A
GraphicsContextalso manages a stack of state objects that can be saved or restored at anytime.The
GraphicsContextmaintains 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 ClipYes No clipping An anti-aliased intersection of various clip paths to which rendering is restricted. Global AlphaYes 1.0An opacity value that controls the visibility or fading of each rendering operation. Global Blend ModeYes SRC_OVERA BlendModeenum value that controls how pixels from each rendering operation are composited into the existing image.TransformYes IdentityA 3x2 2D affine transformation matrix that controls how coordinates are mapped onto the logical pixels of the canvas image. EffectYes nullAn Effectapplied individually to each rendering operation.Fill Attributes Fill PaintYes BLACKThe Paintto be applied to the interior of shapes in a fill operation.Stroke Attributes Stroke PaintYes BLACKThe Paintto be applied to the boundary of shapes in a stroke operation.Line WidthYes 1.0The width of the stroke applied to the boundary of shapes in a stroke operation. Line CapYes SQUAREThe style of the end caps applied to the beginnings and ends of each dash and/or subpath in a stroke operation. Line JoinYes MITERThe style of the joins applied between individual segments in the boundary paths of shapes in a stroke operation. Miter LimitYes 10.0The ratio limit of how far a MITERline 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 aBEVELjoin in a stroke operation.DashesYes nullThe array of dash lengths to be applied to the segments in the boundary of shapes in a stroke operation. Dash OffsetYes 0.0The 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 FontYes Default FontThe font used for all fill and stroke text operations. Text AlignYes LEFTThe horizontal alignment of text with respect to the Xcoordinate specified in the text operation.Text BaselineYes BASELINEThe vertical position of the text relative to the Ycoordinate specified in the text operation.Font SmoothingYes GRAYThe type of smoothing (antialiasing) applied to the glyphs in the font for all fill text operations. Path Attributes Current PathNo Empty path The path constructed using various path construction methods to be used in various path filling, stroking, or clipping operations. Fill RuleYes NON_ZEROThe method used to determine the interior of paths for a path fill or clip operation. The various rendering methods on the
GraphicsContextuse the following sets of rendering attributes:Rendering Attributes Table Method Common Rendering Attributes Fill Attributes Stroke Attributes Text Attributes Path Attributes Basic Shape Rendering fillRect(),fillRoundRect(),fillOval(),fillArc()Yes Yes No No No strokeLine(),strokeRect(),strokeRoundRect(),strokeOval(),strokeArc()Yes No Yes No No clearRect()Yes [1] No No No No fillPolygon()Yes Yes No No Yes [2] strokePolygon(),strokePolyline()Yes No Yes 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 unchangedText Rendering fillText(),fillText(with maxWidth)Yes Yes No Yes [3] No strokeText(),strokeText(with maxWidth)Yes No Yes Yes [3] 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 fill()Yes [4] Yes No No Yes stroke()Yes [4] No Yes No Yes [5] clip()No No No No Yes [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 Miscellaneous applyEffect(),PixelWriter methodsNo 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:
Canvas
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappendSVGPath(String svgpath)Appends an SVG Path string to the current path.voidapplyEffect(Effect e)Applies the given effect to the entire bounds of the canvas and stores the result back into the same canvas.voidarc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)Adds path elements to the current path to make an arc that uses Euclidean degrees.voidarcTo(double x1, double y1, double x2, double y2, double radius)Adds segments to the current path to make an arc.voidbeginPath()Resets the current path to empty.voidbezierCurveTo(double xc1, double yc1, double xc2, double yc2, double x1, double y1)Adds segments to the current path to make a cubic Bezier curve.voidclearRect(double x, double y, double w, double h)Clears a portion of the canvas with a transparent color value.voidclip()Intersects the current clip with the current path and applies it to subsequent rendering operation as an anti-aliased mask.voidclosePath()Closes the path.voiddrawImage(Image img, double x, double y)Draws an image at the given x, y position using the width and height of the given image.voiddrawImage(Image img, double x, double y, double w, double h)Draws an image into the given destination rectangle of the canvas.voiddrawImage(Image img, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)Draws the specified source rectangle of the given image to the given destination rectangle of the Canvas.voidfill()Fills the path with the current fill paint.voidfillArc(double x, double y, double w, double h, double startAngle, double arcExtent, ArcType closure)Fills an arc using the current fill paint.voidfillOval(double x, double y, double w, double h)Fills an oval using the current fill paint.voidfillPolygon(double[] xPoints, double[] yPoints, int nPoints)Fills a polygon with the given points using the currently set fill paint.voidfillRect(double x, double y, double w, double h)Fills a rectangle using the current fill paint.voidfillRoundRect(double x, double y, double w, double h, double arcWidth, double arcHeight)Fills a rounded rectangle using the current fill paint.voidfillText(String text, double x, double y)Fills the given string of text at position x, y with the current fill paint attribute.voidfillText(String text, double x, double y, double maxWidth)Fills text and includes a maximum width of the string.CanvasgetCanvas()Gets theCanvasthat theGraphicsContextis issuing draw commands to.EffectgetEffect(Effect e)Gets a copy of the effect to be applied after the next draw call.PaintgetFill()Gets the current fill paint attribute.FillRulegetFillRule()Get the filling rule attribute for determining the interior of paths in fill and clip operations.FontgetFont()Gets the current Font.FontSmoothingTypegetFontSmoothingType()Gets the current Font Smoothing Type.doublegetGlobalAlpha()Gets the current global alpha.BlendModegetGlobalBlendMode()Gets the global blend mode.StrokeLineCapgetLineCap()Gets the current stroke line cap.double[]getLineDashes()Gets a copy of the current line dash array.doublegetLineDashOffset()Gets the current line dash offset.StrokeLineJoingetLineJoin()Gets the current stroke line join.doublegetLineWidth()Gets the current line width.doublegetMiterLimit()Gets the current miter limit.PixelWritergetPixelWriter()Returns aPixelWriterobject that can be used to modify the pixels of theCanvasassociated with thisGraphicsContext.PaintgetStroke()Gets the current stroke.TextAlignmentgetTextAlign()Gets the currentTextAlignment.VPosgetTextBaseline()Gets the current Text Baseline.AffinegetTransform()Returns a copy of the current transform.AffinegetTransform(Affine xform)Copies the current transform into the supplied object, creating a newAffineobject if it is null, and returns the object containing the copy.booleanisPointInPath(double x, double y)Returns true if the the given x,y point is inside the path.voidlineTo(double x1, double y1)Adds segments to the current path to make a line to the given x,y coordinate.voidmoveTo(double x0, double y0)Issues a move command for the current path to the given x,y coordinate.voidquadraticCurveTo(double xc, double yc, double x1, double y1)Adds segments to the current path to make a quadratic Bezier curve.voidrect(double x, double y, double w, double h)Adds path elements to the current path to make a rectangle.voidrestore()Pops the state off of the stack, setting the following attributes to their value at the time when that state was pushed onto the stack.voidrotate(double degrees)Rotates the current transform in degrees.voidsave()Saves the following attributes onto a stack.voidscale(double x, double y)Scales the current transform by x, y.voidsetEffect(Effect e)Sets the effect to be applied after the next draw call, or null to disable effects.voidsetFill(Paint p)Sets the current fill paint attribute.voidsetFillRule(FillRule fillRule)Set the filling rule attribute for determining the interior of paths in fill or clip operations.voidsetFont(Font f)Sets the current Font.voidsetFontSmoothingType(FontSmoothingType fontsmoothing)Sets the current Font Smoothing Type.voidsetGlobalAlpha(double alpha)Sets the global alpha of the current state.voidsetGlobalBlendMode(BlendMode op)Sets the global blend mode.voidsetLineCap(StrokeLineCap cap)Sets the current stroke line cap.voidsetLineDashes(double... dashes)Sets the current stroke line dash pattern to a normalized copy of the argument.voidsetLineDashOffset(double dashOffset)Sets the line dash offset.voidsetLineJoin(StrokeLineJoin join)Sets the current stroke line join.voidsetLineWidth(double lw)Sets the current line width.voidsetMiterLimit(double ml)Sets the current miter limit.voidsetStroke(Paint p)Sets the current stroke paint attribute.voidsetTextAlign(TextAlignment align)Defines horizontal text alignment, relative to the textxorigin.voidsetTextBaseline(VPos baseline)Sets the current Text Baseline.voidsetTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt)Sets the current transform.voidsetTransform(Affine xform)Sets the current transform.voidstroke()Strokes the path with the current stroke paint.voidstrokeArc(double x, double y, double w, double h, double startAngle, double arcExtent, ArcType closure)Strokes an Arc using the current stroke paint.voidstrokeLine(double x1, double y1, double x2, double y2)Strokes a line using the current stroke paint.voidstrokeOval(double x, double y, double w, double h)Strokes an oval using the current stroke paint.voidstrokePolygon(double[] xPoints, double[] yPoints, int nPoints)Strokes a polygon with the given points using the currently set stroke paint.voidstrokePolyline(double[] xPoints, double[] yPoints, int nPoints)Strokes a polyline with the given points using the currently set stroke paint attribute.voidstrokeRect(double x, double y, double w, double h)Strokes a rectangle using the current stroke paint.voidstrokeRoundRect(double x, double y, double w, double h, double arcWidth, double arcHeight)Strokes a rounded rectangle using the current stroke paint.voidstrokeText(String text, double x, double y)Draws the given string of text at position x, y with the current stroke paint attribute.voidstrokeText(String text, double x, double y, double maxWidth)Draws text with stroke paint and includes a maximum width of the string.voidtransform(double mxx, double myx, double mxy, double myy, double mxt, double myt)Concatenates the input with the current transform.voidtransform(Affine xform)Concatenates the input with the current transform.voidtranslate(double x, double y)Translates the current transform by x, y.
-
-
-
Method Detail
-
getCanvas
public Canvas getCanvas()
Gets theCanvasthat theGraphicsContextis issuing draw commands to. There is only ever oneCanvasfor aGraphicsContext.- Returns:
- Canvas the canvas that this
GraphicsContextis issuing draw commands to.
-
save
public void save()
Saves the following attributes onto a stack.- Global Alpha
- Global Blend Operation
- Transform
- Fill Paint
- Stroke Paint
- Line Width
- Line Cap
- Line Join
- Miter Limit
- Clip
- Font
- Text Align
- Text Baseline
- Effect
- Fill Rule
-
restore
public void restore()
Pops the state off of the stack, setting the following attributes to their value at the time when that state was pushed onto the stack. If the stack is empty then nothing is changed.- Global Alpha
- Global Blend Operation
- Transform
- Fill Paint
- Stroke Paint
- Line Width
- Line Cap
- Line Join
- Miter Limit
- Clip
- Font
- Text Align
- Text Baseline
- Effect
- Fill Rule
-
translate
public void translate(double x, double y)Translates the current transform by x, y.- Parameters:
x- value to translate along the x axis.y- value to translate along the y axis.
-
scale
public void scale(double x, double y)Scales the current transform by x, y.- Parameters:
x- value to scale in the x axis.y- value to scale in the y axis.
-
rotate
public void rotate(double degrees)
Rotates the current transform in degrees.- Parameters:
degrees- value in degrees to rotate the current transform.
-
transform
public void transform(double mxx, double myx, double mxy, double myy, double mxt, double myt)Concatenates the input with the current transform.- Parameters:
mxx- - the X coordinate scaling element of the 3x4 matrixmyx- - the Y coordinate shearing element of the 3x4 matrixmxy- - the X coordinate shearing element of the 3x4 matrixmyy- - the Y coordinate scaling element of the 3x4 matrixmxt- - the X coordinate translation element of the 3x4 matrixmyt- - the Y coordinate translation element of the 3x4 matrix
-
transform
public void transform(Affine xform)
Concatenates the input with the current transform. Only 2D transforms are supported. The only values used are the X and Y scaling, translation, and shearing components of a transform. Anullvalue is treated as identity.- Parameters:
xform- The affine to be concatenated with the current transform or null.
-
setTransform
public void setTransform(double mxx, double myx, double mxy, double myy, double mxt, double myt)Sets the current transform.- Parameters:
mxx- - the X coordinate scaling element of the 3x4 matrixmyx- - the Y coordinate shearing element of the 3x4 matrixmxy- - the X coordinate shearing element of the 3x4 matrixmyy- - the Y coordinate scaling element of the 3x4 matrixmxt- - the X coordinate translation element of the 3x4 matrixmyt- - the Y coordinate translation element of the 3x4 matrix
-
setTransform
public void setTransform(Affine xform)
Sets the current transform. Only 2D transforms are supported. The only values used are the X and Y scaling, translation, and shearing components of a transform.- Parameters:
xform- The affine to be copied and used as the current transform.
-
getTransform
public Affine getTransform(Affine xform)
Copies the current transform into the supplied object, creating a newAffineobject if it is null, and returns the object containing the copy.- Parameters:
xform- A transform object that will be used to hold the result. If xform is non null, then this method will copy the current transform into that object. If xform is null a new transform object will be constructed. In either case, the return value is a copy of the current transform.- Returns:
- A copy of the current transform.
-
getTransform
public Affine getTransform()
Returns a copy of the current transform.- Returns:
- a copy of the transform of the current state.
-
setGlobalAlpha
public void setGlobalAlpha(double alpha)
Sets the global alpha of the current state. The default value is1.0. Any valid double can be set, but only values in the range[0.0, 1.0]are valid and the nearest value in that range will be used for rendering. The global alpha is a common attribute used for nearly all rendering methods as specified in the Rendering Attributes Table.- Parameters:
alpha- the new alpha value, clamped to[0.0, 1.0]during actual use.
-
getGlobalAlpha
public double getGlobalAlpha()
Gets the current global alpha. The default value is1.0. The global alpha is a common attribute used for nearly all rendering methods as specified in the Rendering Attributes Table.- Returns:
- the current global alpha.
-
setGlobalBlendMode
public void setGlobalBlendMode(BlendMode op)
Sets the global blend mode. The default value isSRC_OVER. Anullvalue will be ignored and the current value will remain unchanged. The blend mode is a common attribute used for nearly all rendering methods as specified in the Rendering Attributes Table.- Parameters:
op- theBlendModethat will be set or null.
-
getGlobalBlendMode
public BlendMode getGlobalBlendMode()
Gets the global blend mode. The default value isSRC_OVER. The blend mode is a common attribute used for nearly all rendering methods as specified in the Rendering Attributes Table.- Returns:
- the global
BlendModeof the current state.
-
setFill
public void setFill(Paint p)
Sets the current fill paint attribute. The default value isBLACK. The fill paint is a fill attribute used for any of the fill methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.- Parameters:
p- ThePaintto be used as the fillPaintor null.
-
getFill
public Paint getFill()
Gets the current fill paint attribute. The default value isBLACK. The fill paint is a fill attribute used for any of the fill methods as specified in the Rendering Attributes Table.- Returns:
- p The
Paintto be used as the fillPaint.
-
setStroke
public void setStroke(Paint p)
Sets the current stroke paint attribute. The default value isBLACK. The stroke paint is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.- Parameters:
p- The Paint to be used as the stroke Paint or null.
-
getStroke
public Paint getStroke()
Gets the current stroke. The default value isBLACK. The stroke paint is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
- the
Paintto be used as the strokePaint.
-
setLineWidth
public void setLineWidth(double lw)
Sets the current line width. The default value is1.0. The line width is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. An infinite or non-positive value outside of the range(0, +inf)will be ignored and the current value will remain unchanged.- Parameters:
lw- value in the range {0-positive infinity}, with any other value being ignored and leaving the value unchanged.
-
getLineWidth
public double getLineWidth()
Gets the current line width. The default value is1.0. The line width is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
- value between 0 and infinity.
-
setLineCap
public void setLineCap(StrokeLineCap cap)
Sets the current stroke line cap. The default value isSQUARE. The line cap is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.- Parameters:
cap-StrokeLineCapwith a value of Butt, Round, or Square or null.
-
getLineCap
public StrokeLineCap getLineCap()
Gets the current stroke line cap. The default value isSQUARE. The line cap is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
StrokeLineCapwith a value of Butt, Round, or Square.
-
setLineJoin
public void setLineJoin(StrokeLineJoin join)
Sets the current stroke line join. The default value isStrokeLineJoin.MITER. The line join is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.- Parameters:
join-StrokeLineJoinwith a value of Miter, Bevel, or Round or null.
-
getLineJoin
public StrokeLineJoin getLineJoin()
Gets the current stroke line join. The default value isStrokeLineJoin.MITER. The line join is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
StrokeLineJoinwith a value of Miter, Bevel, or Round.
-
setMiterLimit
public void setMiterLimit(double ml)
Sets the current miter limit. The default value is10.0. The miter limit is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. An infinite or non-positive value outside of the range(0, +inf)will be ignored and the current value will remain unchanged.- Parameters:
ml- miter limit value between 0 and positive infinity with any other value being ignored and leaving the value unchanged.
-
getMiterLimit
public double getMiterLimit()
Gets the current miter limit. The default value is10.0. The miter limit is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
- the miter limit value in the range
0.0-positive infinity
-
setLineDashes
public void setLineDashes(double... dashes)
Sets the current stroke line dash pattern to a normalized copy of the argument. The default value isnull. The line dash array is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. If the array isnullor empty or contains all0elements then dashing will be disabled and the current dash array will be set tonull. If any of the elements of the array are a negative, infinite, or NaN value outside the range[0, +inf)then the entire array will be ignored and the current dash array will remain unchanged. If the array is an odd length then it will be treated as if it were two copies of the array appended to each other.- Parameters:
dashes- the array of finite non-negative dash lengths- Since:
- JavaFX 8u40
-
getLineDashes
public double[] getLineDashes()
Gets a copy of the current line dash array. The default value isnull. The array may be normalized by the validation tests in thesetLineDashes(double...)method. The line dash array is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
- a copy of the current line dash array.
- Since:
- JavaFX 8u40
-
setLineDashOffset
public void setLineDashOffset(double dashOffset)
Sets the line dash offset. The default value is0.0. The line dash offset is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table. An infinite or NaN value outside of the range(-inf, +inf)will be ignored and the current value will remain unchanged.- Parameters:
dashOffset- the line dash offset in the range(-inf, +inf)- Since:
- JavaFX 8u40
-
getLineDashOffset
public double getLineDashOffset()
Gets the current line dash offset. The default value is0.0. The line dash offset is a stroke attribute used for any of the stroke methods as specified in the Rendering Attributes Table.- Returns:
- the line dash offset in the range
(-inf, +inf) - Since:
- JavaFX 8u40
-
setFont
public void setFont(Font f)
Sets the current Font. The default value is specified byFont.getDefault(). The font is a text attribute used for any of the text methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.- Parameters:
f- the Font or null.
-
getFont
public Font getFont()
Gets the current Font. The default value is specified byFont.getDefault(). The font is a text attribute used for any of the text methods as specified in the Rendering Attributes Table.- Returns:
- the Font
-
setFontSmoothingType
public void setFontSmoothingType(FontSmoothingType fontsmoothing)
Sets the current Font Smoothing Type. The default value isGRAY. The font smoothing type is a text attribute used for any of the text methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.Note that the
FontSmoothingTypevalue ofLCDis only supported over an opaque background.LCDtext will generally appear asGRAYtext over transparent or partially transparent pixels, and in some implementations it may not be supported at all on aCanvasbecause the required support does not exist for surfaces which contain an alpha channel as allCanvasobjects do.- Parameters:
fontsmoothing- theFontSmoothingTypeor null- Since:
- JavaFX 8u40
-
getFontSmoothingType
public FontSmoothingType getFontSmoothingType()
Gets the current Font Smoothing Type. The default value isGRAY. The font smoothing type is a text attribute used for any of the text methods as specified in the Rendering Attributes Table.- Returns:
- the
FontSmoothingType - Since:
- JavaFX 8u40
-
setTextAlign
public void setTextAlign(TextAlignment align)
Defines horizontal text alignment, relative to the textxorigin. The default value isLEFT. The text alignment is a text attribute used for any of the text methods as specified in the Rendering Attributes Table.Let horizontal bounds represent the logical width of a single line of text. Where each line of text has a separate horizontal bounds.
Then TextAlignment is specified as:
- Left: the left edge of the horizontal bounds will be at
x. - Center: the center, halfway between left and right edge, of the
horizontal bounds will be at
x. - Right: the right edge of the horizontal bounds will be at
x.
Note: Canvas does not support line wrapping, therefore the text alignment Justify is identical to left aligned text.
A
nullvalue will be ignored and the current value will remain unchanged.- Parameters:
align-TextAlignmentwith values of Left, Center, Right or null.
- Left: the left edge of the horizontal bounds will be at
-
getTextAlign
public TextAlignment getTextAlign()
Gets the currentTextAlignment. The default value isLEFT. The text alignment is a text attribute used for any of the text methods as specified in the Rendering Attributes Table.- Returns:
TextAlignmentwith values of Left, Center, Right, or Justify.
-
setTextBaseline
public void setTextBaseline(VPos baseline)
Sets the current Text Baseline. The default value isBASELINE. The text baseline is a text attribute used for any of the text methods as specified in the Rendering Attributes Table. Anullvalue will be ignored and the current value will remain unchanged.- Parameters:
baseline-VPoswith values of Top, Center, Baseline, or Bottom or null.
-
getTextBaseline
public VPos getTextBaseline()
Gets the current Text Baseline. The default value isBASELINE. The text baseline is a text attribute used for any of the text methods as specified in the Rendering Attributes Table.- Returns:
VPoswith values of Top, Center, Baseline, or Bottom
-
fillText
public void fillText(String text, double x, double y)
Fills the given string of text at position x, y with the current fill paint attribute. Anulltext value will be ignored.This method will be affected by any of the global common, fill, or text attributes as specified in the Rendering Attributes Table.
- Parameters:
text- the string of text or null.x- position on the x axis.y- position on the y axis.
-
strokeText
public void strokeText(String text, double x, double y)
Draws the given string of text at position x, y with the current stroke paint attribute. Anulltext value will be ignored.This method will be affected by any of the global common, stroke, or text attributes as specified in the Rendering Attributes Table.
- Parameters:
text- the string of text or null.x- position on the x axis.y- position on the y axis.
-
fillText
public void fillText(String text, double x, double y, double maxWidth)
Fills text and includes a maximum width of the string. If the width of the text extends past max width, then it will be sized to fit. Anulltext value will be ignored.This method will be affected by any of the global common, fill, or text attributes as specified in the Rendering Attributes Table.
- Parameters:
text- the string of text or null.x- position on the x axis.y- position on the y axis.maxWidth- maximum width the text string can have.
-
strokeText
public void strokeText(String text, double x, double y, double maxWidth)
Draws text with stroke paint and includes a maximum width of the string. If the width of the text extends past max width, then it will be sized to fit. Anulltext value will be ignored.This method will be affected by any of the global common, stroke, or text attributes as specified in the Rendering Attributes Table.
- Parameters:
text- the string of text or null.x- position on the x axis.y- position on the y axis.maxWidth- maximum width the text string can have.
-
setFillRule
public void setFillRule(FillRule fillRule)
Set the filling rule attribute for determining the interior of paths in fill or clip operations. The default value isFillRule.NON_ZERO. Anullvalue will be ignored and the current value will remain unchanged. The fill rule is a path attribute used for any of the fill or clip path methods as specified in the Rendering Attributes Table.- Parameters:
fillRule-FillRulewith a value of Even_odd or Non_zero or null.
-
getFillRule
public FillRule getFillRule()
Get the filling rule attribute for determining the interior of paths in fill and clip operations. The default value isFillRule.NON_ZERO. The fill rule is a path attribute used for any of the fill or clip path methods as specified in the Rendering Attributes Table.- Returns:
- current fill rule.
-
beginPath
public void beginPath()
Resets the current path to empty. The default path is empty. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.
-
moveTo
public void moveTo(double x0, double y0)Issues a move command for the current path to the given x,y coordinate. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
x0- the X position for the move to command.y0- the Y position for the move to command.
-
lineTo
public void lineTo(double x1, double y1)Adds segments to the current path to make a line to the given x,y coordinate. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
x1- the X coordinate of the ending point of the line.y1- the Y coordinate of the ending point of the line.
-
quadraticCurveTo
public void quadraticCurveTo(double xc, double yc, double x1, double y1)Adds segments to the current path to make a quadratic Bezier curve. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
xc- the X coordinate of the control pointyc- the Y coordinate of the control pointx1- the X coordinate of the end pointy1- the Y coordinate of the end point
-
bezierCurveTo
public void bezierCurveTo(double xc1, double yc1, double xc2, double yc2, double x1, double y1)Adds segments to the current path to make a cubic Bezier curve. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
xc1- the X coordinate of first Bezier control point.yc1- the Y coordinate of the first Bezier control point.xc2- the X coordinate of the second Bezier control point.yc2- the Y coordinate of the second Bezier control point.x1- the X coordinate of the end point.y1- the Y coordinate of the end point.
-
arcTo
public void arcTo(double x1, double y1, double x2, double y2, double radius)Adds segments to the current path to make an arc. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.If
p0is the current point in the path andp1is the point specified by(x1, y1)andp2is the point specified by(x2, y2), then the arc segments appended will be segments along the circumference of a circle of the specified radius touching and inscribed into the convex (interior) side ofp0->p1->p2. The path will contain a line segment (if needed) to the tangent point between that circle andp0->p1followed by circular arc segments to reach the tangent point between the circle andp1->p2and will end with the current point at that tangent point (not atp2). Note that the radius and circularity of the arc segments will be measured or considered relative to the current transform, but the resulting segments that are computed from those untransformed points will then be transformed when they are added to the path. Since all computation is done in untransformed space, but the pre-existing path segments are all transformed, the ability to correctly perform the computation may implicitly depend on being able to inverse transform the current end of the current path back into untransformed coordinates.If there is no way to compute and inscribe the indicated circle for any reason then the entire operation will simply append segments to force a line to point
p1. Possible reasons that the computation may fail include:- The current path is empty.
- The segments
p0->p1->p2are colinear. - the current transform is non-invertible so that the current end point of the current path cannot be untransformed for computation.
- Parameters:
x1- the X coordinate of the first point of the arc.y1- the Y coordinate of the first point of the arc.x2- the X coordinate of the second point of the arc.y2- the Y coordinate of the second point of the arc.radius- the radius of the arc in the range {0.0-positive infinity}.
-
arc
public void arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)Adds path elements to the current path to make an arc that uses Euclidean degrees. This Euclidean orientation sweeps from East to North, then West, then South, then back to East. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
centerX- the center x position of the arc.centerY- the center y position of the arc.radiusX- the x radius of the arc.radiusY- the y radius of the arc.startAngle- the starting angle of the arc in the range0-360.0length- the length of the baseline of the arc.
-
rect
public void rect(double x, double y, double w, double h)Adds path elements to the current path to make a rectangle. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
x- x position of the upper left corner of the rectangle.y- y position of the upper left corner of the rectangle.w- width of the rectangle.h- height of the rectangle.
-
appendSVGPath
public void appendSVGPath(String svgpath)
Appends an SVG Path string to the current path. If there is no current path the string must then start with either type of move command. Anullvalue or incorrect SVG path will be ignored. The coordinates are transformed by the current transform as they are added to the path and unaffected by subsequent changes to the transform. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.- Parameters:
svgpath- the SVG Path string.
-
closePath
public void closePath()
Closes the path. The current path is a path attribute used for any of the path methods as specified in the Rendering Attributes Table and is not affected by thesave()andrestore()operations.
-
fill
public void fill()
Fills the path with the current fill paint.This method will be affected by any of the global common, fill, or path attributes as specified in the Rendering Attributes Table. Note that the path segments were transformed as they were originally added to the current path so the current transform will not affect those path segments again, but it may affect other attributes in affect at the time of the
fill()operation.
-
stroke
public void stroke()
Strokes the path with the current stroke paint.This method will be affected by any of the global common, stroke, or path attributes as specified in the Rendering Attributes Table. Note that the path segments were transformed as they were originally added to the current path so the current transform will not affect those path segments again, but it may affect other attributes in affect at the time of the
stroke()operation.
-
clip
public void clip()
Intersects the current clip with the current path and applies it to subsequent rendering operation as an anti-aliased mask. The current clip is a common attribute used for nearly all rendering operations as specified in the Rendering Attributes Table.This method will itself be affected only by the path attributes as specified in the Rendering Attributes Table. Note that the path segments were transformed as they were originally added to the current path so the current transform will not affect those path segments again, but it may affect other attributes in affect at the time of the
stroke()operation.
-
isPointInPath
public boolean isPointInPath(double x, double y)Returns true if the the given x,y point is inside the path.- Parameters:
x- the X coordinate to use for the check.y- the Y coordinate to use for the check.- Returns:
- true if the point given is inside the path, false otherwise.
-
clearRect
public void clearRect(double x, double y, double w, double h)Clears a portion of the canvas with a transparent color value.This method will be affected only by the current transform, clip, and effect.
- Parameters:
x- X position of the upper left corner of the rectangle.y- Y position of the upper left corner of the rectangle.w- width of the rectangle.h- height of the rectangle.
-
fillRect
public void fillRect(double x, double y, double w, double h)Fills a rectangle using the current fill paint.This method will be affected by any of the global common or fill attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X position of the upper left corner of the rectangle.y- the Y position of the upper left corner of the rectangle.w- the width of the rectangle.h- the height of the rectangle.
-
strokeRect
public void strokeRect(double x, double y, double w, double h)Strokes a rectangle using the current stroke paint.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X position of the upper left corner of the rectangle.y- the Y position of the upper left corner of the rectangle.w- the width of the rectangle.h- the height of the rectangle.
-
fillOval
public void fillOval(double x, double y, double w, double h)Fills an oval using the current fill paint.This method will be affected by any of the global common or fill attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X coordinate of the upper left bound of the oval.y- the Y coordinate of the upper left bound of the oval.w- the width at the center of the oval.h- the height at the center of the oval.
-
strokeOval
public void strokeOval(double x, double y, double w, double h)Strokes an oval using the current stroke paint.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X coordinate of the upper left bound of the oval.y- the Y coordinate of the upper left bound of the oval.w- the width at the center of the oval.h- the height at the center of the oval.
-
fillArc
public void fillArc(double x, double y, double w, double h, double startAngle, double arcExtent, ArcType closure)Fills an arc using the current fill paint. AnullArcType or non positive width or height will cause the render command to be ignored.This method will be affected by any of the global common or fill attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X coordinate of the arc.y- the Y coordinate of the arc.w- the width of the arc.h- the height of the arc.startAngle- the starting angle of the arc in degrees.arcExtent- the angular extent of the arc in degrees.closure- closure type (Round, Chord, Open) or null.
-
strokeArc
public void strokeArc(double x, double y, double w, double h, double startAngle, double arcExtent, ArcType closure)Strokes an Arc using the current stroke paint. AnullArcType or non positive width or height will cause the render command to be ignored.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X coordinate of the arc.y- the Y coordinate of the arc.w- the width of the arc.h- the height of the arc.startAngle- the starting angle of the arc in degrees.arcExtent- arcExtent the angular extent of the arc in degrees.closure- closure type (Round, Chord, Open) or null
-
fillRoundRect
public void fillRoundRect(double x, double y, double w, double h, double arcWidth, double arcHeight)Fills a rounded rectangle using the current fill paint.This method will be affected by any of the global common or fill attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X coordinate of the upper left bound of the oval.y- the Y coordinate of the upper left bound of the oval.w- the width at the center of the oval.h- the height at the center of the oval.arcWidth- the arc width of the rectangle corners.arcHeight- the arc height of the rectangle corners.
-
strokeRoundRect
public void strokeRoundRect(double x, double y, double w, double h, double arcWidth, double arcHeight)Strokes a rounded rectangle using the current stroke paint.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
x- the X coordinate of the upper left bound of the oval.y- the Y coordinate of the upper left bound of the oval.w- the width at the center of the oval.h- the height at the center of the oval.arcWidth- the arc width of the rectangle corners.arcHeight- the arc height of the rectangle corners.
-
strokeLine
public void strokeLine(double x1, double y1, double x2, double y2)Strokes a line using the current stroke paint.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
x1- the X coordinate of the starting point of the line.y1- the Y coordinate of the starting point of the line.x2- the X coordinate of the ending point of the line.y2- the Y coordinate of the ending point of the line.
-
fillPolygon
public void fillPolygon(double[] xPoints, double[] yPoints, int nPoints)Fills a polygon with the given points using the currently set fill paint. Anullvalue for any of the arrays will be ignored and nothing will be drawn.This method will be affected by any of the global common, fill, or Fill Rule attributes as specified in the Rendering Attributes Table.
- Parameters:
xPoints- array containing the x coordinates of the polygon's points or null.yPoints- array containing the y coordinates of the polygon's points or null.nPoints- the number of points that make the polygon.
-
strokePolygon
public void strokePolygon(double[] xPoints, double[] yPoints, int nPoints)Strokes a polygon with the given points using the currently set stroke paint. Anullvalue for any of the arrays will be ignored and nothing will be drawn.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
xPoints- array containing the x coordinates of the polygon's points or null.yPoints- array containing the y coordinates of the polygon's points or null.nPoints- the number of points that make the polygon.
-
strokePolyline
public void strokePolyline(double[] xPoints, double[] yPoints, int nPoints)Strokes a polyline with the given points using the currently set stroke paint attribute. Anullvalue for any of the arrays will be ignored and nothing will be drawn.This method will be affected by any of the global common or stroke attributes as specified in the Rendering Attributes Table.
- Parameters:
xPoints- array containing the x coordinates of the polyline's points or null.yPoints- array containing the y coordinates of the polyline's points or null.nPoints- the number of points that make the polyline.
-
drawImage
public void drawImage(Image img, double x, double y)
Draws an image at the given x, y position using the width and height of the given image. Anullimage value or an image still in progress will be ignored.This method will be affected by any of the global common attributes as specified in the Rendering Attributes Table.
- Parameters:
img- the image to be drawn or null.x- the X coordinate on the destination for the upper left of the image.y- the Y coordinate on the destination for the upper left of the image.
-
drawImage
public void drawImage(Image img, double x, double y, double w, double h)
Draws an image into the given destination rectangle of the canvas. The Image is scaled to fit into the destination rectangle. Anullimage value or an image still in progress will be ignored.This method will be affected by any of the global common attributes as specified in the Rendering Attributes Table.
- Parameters:
img- the image to be drawn or null.x- the X coordinate on the destination for the upper left of the image.y- the Y coordinate on the destination for the upper left of the image.w- the width of the destination rectangle.h- the height of the destination rectangle.
-
drawImage
public void drawImage(Image img, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)
Draws the specified source rectangle of the given image to the given destination rectangle of the Canvas. Anullimage value or an image still in progress will be ignored.This method will be affected by any of the global common attributes as specified in the Rendering Attributes Table.
- Parameters:
img- the image to be drawn or null.sx- the source rectangle's X coordinate position.sy- the source rectangle's Y coordinate position.sw- the source rectangle's width.sh- the source rectangle's height.dx- the destination rectangle's X coordinate position.dy- the destination rectangle's Y coordinate position.dw- the destination rectangle's width.dh- the destination rectangle's height.
-
getPixelWriter
public PixelWriter getPixelWriter()
Returns aPixelWriterobject that can be used to modify the pixels of theCanvasassociated with thisGraphicsContext. All coordinates in thePixelWritermethods on the returned object will be in device space since they refer directly to pixels and no other rendering attributes will be applied when modifying pixels using this object.- Returns:
- the
PixelWriterfor modifying the pixels of thisCanvas
-
setEffect
public void setEffect(Effect e)
Sets the effect to be applied after the next draw call, or null to disable effects. The current effect is a common attribute used for nearly all rendering operations as specified in the Rendering Attributes Table.- Parameters:
e- the effect to use, or null to disable effects
-
getEffect
public Effect getEffect(Effect e)
Gets a copy of the effect to be applied after the next draw call. A null return value means that no effect will be applied after subsequent rendering calls. The current effect is a common attribute used for nearly all rendering operations as specified in the Rendering Attributes Table.- Parameters:
e- anEffectobject that may be used to store the copy of the current effect, if it is of a compatible type- Returns:
- the current effect used for all rendering calls, or null if there is no current effect
-
applyEffect
public void applyEffect(Effect e)
Applies the given effect to the entire bounds of the canvas and stores the result back into the same canvas. Anullvalue will be ignored. The effect will be applied without any other rendering attributes and under an Identity coordinate transform. Since the effect is applied to the entire bounds of the canvas, some effects may have a confusing result, such as a Reflection effect that will apply its reflection off of the bottom of the canvas even if only a portion of the canvas has been rendered to and will not be visible unless a negative offset is used to bring the reflection back into view.- Parameters:
e- the effect to apply onto the entire destination or null.
-
-