public final class SVGGraphics2D extends Graphics2D
A Graphics2D
implementation that creates SVG output. After
rendering the graphics via the SVGGraphics2D
, you can retrieve
an SVG element (see getSVGElement()
) or an SVG document (see
getSVGDocument()
) containing your content.
Using the SVGGraphics2D
class is straightforward. First,
create an instance specifying the height and width of the SVG element that
will be created. Then, use standard Java2D API calls to draw content
into the element. Finally, retrieve the SVG element that has been
accumulated. For example:
SVGGraphics2D g2 = new SVGGraphics2D(300, 200);
g2.setPaint(Color.RED);
g2.draw(new Rectangle(10, 10, 280, 180));
String svgElement = g2.getSVGElement();
For the content generation step, you can make use of third party libraries, such as JFreeChart and Orson Charts, that render output using standard Java2D API calls.
Rendering Hints
The SVGGraphics2D
supports a couple of custom rendering hints -
for details, refer to the SVGHints
class documentation. Also see
the examples in this blog post:
Orson Charts 3D / Enhanced SVG Export.
Some additional notes:
- by default, JFreeSVG uses a fast conversion of numerical values to
strings for the SVG output (the 'RyuDouble' implementation). If you
prefer a different approach (for example, controlling the number of
decimal places in the output to reduce the file size) you can set your
own functions for converting numerical values - see the
setGeomDoubleConverter(DoubleFunction)
andsetTransformDoubleConverter(DoubleFunction)
methods. - the
getFontMetrics(java.awt.Font)
andgetFontRenderContext()
methods return values that come from an internalBufferedImage
, this is a short-cut and we don't know if there are any negative consequences (if you know of any, please let us know and we'll add the info here or find a way to fix it); - Images are supported, but for methods with an
ImageObserver
parameter note that the observer is ignored completely. In any case, using images that are not fully loaded already would not be a good idea in the context of generating SVG data/files; - when an HTML page contains multiple SVG elements, the items within
the DEFS element for each SVG element must have IDs that are unique across
all SVG elements in the page. JFreeSVG auto-populates the
defsKeyPrefix
attribute to help ensure that unique IDs are generated.
For some demos showing how to use this class, look at the JFree-Demos project at GitHub: https://github.com/jfree/jfree-demos.
-
Constructor Summary
Constructors Constructor Description SVGGraphics2D(double width, double height)
Creates a new instance with the specified width and height.SVGGraphics2D(double width, double height, SVGUnits units)
Creates a new instance with the specified width and height in the given units.SVGGraphics2D(double width, double height, SVGUnits units, StringBuilder sb)
Creates a new instance with the specified width and height that will populate the suppliedStringBuilder
instance. -
Method Summary
Modifier and Type Method Description void
addRenderingHints(Map<?,?> hints)
Adds all the supplied rendering hints.void
clearRect(int x, int y, int width, int height)
Clears the specified rectangle by filling it with the current background color.void
clip(Shape s)
Clips to the intersection of the current clipping region and the specified shape.void
clipRect(int x, int y, int width, int height)
Clips to the intersection of the current clipping region and the specified rectangle.void
copyArea(int x, int y, int width, int height, int dx, int dy)
This method does nothing.Graphics
create()
Creates a new graphics object that is a copy of this graphics object (except that it has not accumulated the drawing operations).void
dispose()
This method does nothing, there are no resources to dispose.void
draw(Shape s)
Draws the specified shape with the currentpaint
andstroke
.void
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Draws an arc contained within the rectangle(x, y, width, height)
, starting atstartAngle
and continuing througharcAngle
degrees using the currentpaint
andstroke
.void
drawGlyphVector(GlyphVector g, float x, float y)
Draws the specified glyph vector at the location(x, y)
.void
drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
Draws the image resulting from applying theBufferedImageOp
to the specified image at the location(x, y)
.boolean
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)
) into the destination rectangle(dx1, dy1, dx2, dy2)
.boolean
drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)
) into the destination rectangle(dx1, dy1, dx2, dy2)
.boolean
drawImage(Image img, int x, int y, int w, int h, Color bgcolor, ImageObserver observer)
Draws an image to the rectangle(x, y, w, h)
(scaling it if required), first filling the background with the specified color.boolean
drawImage(Image img, int x, int y, int w, int h, ImageObserver observer)
Draws the image into the rectangle defined by(x, y, w, h)
.boolean
drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
Draws an image at the location(x, y)
.boolean
drawImage(Image img, int x, int y, ImageObserver observer)
Draws an image at the location(x, y)
.boolean
drawImage(Image img, AffineTransform xform, ImageObserver obs)
Draws an image with the specified transform.void
drawLine(int x1, int y1, int x2, int y2)
Draws a line from(x1, y1)
to(x2, y2)
using the currentpaint
andstroke
.void
drawOval(int x, int y, int width, int height)
Draws an oval framed by the rectangle(x, y, width, height)
using the currentpaint
andstroke
.void
drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
Draws the specified polygon using the currentpaint
andstroke
.void
drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
Draws the specified multi-segment line using the currentpaint
andstroke
.void
drawRenderableImage(RenderableImage img, AffineTransform xform)
Draws the renderable image.void
drawRenderedImage(RenderedImage img, AffineTransform xform)
Draws the rendered image.void
drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Draws a rectangle with rounded corners using the currentpaint
andstroke
.void
drawString(String str, float x, float y)
Draws a string at(x, y)
.void
drawString(String str, int x, int y)
Draws a string at(x, y)
.void
drawString(AttributedCharacterIterator iterator, float x, float y)
Draws a string of attributed characters at(x, y)
.void
drawString(AttributedCharacterIterator iterator, int x, int y)
Draws a string of attributed characters at(x, y)
.void
fill(Shape s)
Fills the specified shape with the currentpaint
.void
fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Fills an arc contained within the rectangle(x, y, width, height)
, starting atstartAngle
and continuing througharcAngle
degrees, using the currentpaint
.void
fillOval(int x, int y, int width, int height)
Fills an oval framed by the rectangle(x, y, width, height)
.void
fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills the specified polygon using the currentpaint
.void
fillRect(int x, int y, int width, int height)
Fills the specified rectangle with the currentpaint
.void
fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Fills a rectangle with rounded corners using the currentpaint
.Color
getBackground()
Returns the background color.boolean
getCheckStrokeControlHint()
Returns the flag that controls whether or not this object will observe theKEY_STROKE_CONTROL
rendering hint.Shape
getClip()
Returns the user clipping region.Rectangle
getClipBounds()
Returns the bounds of the user clipping region.Color
getColor()
Returns the foreground color.Composite
getComposite()
Returns the current composite.String
getDefsKeyPrefix()
Returns the prefix used for all keys in the DEFS element.GraphicsConfiguration
getDeviceConfiguration()
Returns the device configuration associated with thisGraphics2D
.Set<String>
getElementIDs()
Returns a new set containing the element IDs that have been used in output so far.String
getFilePrefix()
Returns the prefix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.String
getFileSuffix()
Returns the suffix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.Font
getFont()
Returns the current font used for drawing text.Function<String,String>
getFontFunction()
Returns the function that generates SVG font references from a supplied Java font family name.FontMetrics
getFontMetrics(Font f)
Returns the font metrics for the specified font.FontRenderContext
getFontRenderContext()
Returns the font render context.SVGUnits
getFontSizeUnits()
Returns the font size units.DoubleFunction<String>
getGeomDoubleConverter()
Returns the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output.double
getHeight()
Returns the height for the SVG element, specified in the constructor.Paint
getPaint()
Returns the paint used to draw or fill shapes (or text).Object
getRenderingHint(RenderingHints.Key hintKey)
Returns the current value for the specified hint.RenderingHints
getRenderingHints()
Returns a copy of the rendering hints.Stroke
getStroke()
Returns the current stroke (used when drawing shapes).String
getSVGDocument()
Returns an SVG document (this contains the content returned by thegetSVGElement()
method, prepended with the required document header).String
getSVGElement()
Returns the SVG element that has been generated by calls to thisGraphics2D
implementation.String
getSVGElement(String id)
Returns the SVG element that has been generated by calls to thisGraphics2D
implementation, giving it the specifiedid
.String
getSVGElement(String id, boolean includeDimensions, ViewBox viewBox, PreserveAspectRatio preserveAspectRatio, MeetOrSlice meetOrSlice)
Returns the SVG element that has been generated by calls to thisGraphics2D
implementation, giving it the specifiedid
.List<ImageElement>
getSVGImages()
Returns the list of image elements that have been referenced in the SVG output but not embedded.AffineTransform
getTransform()
Returns a copy of the current transform.DoubleFunction<String>
getTransformDoubleConverter()
Returns the double-to-string function that is used when writing values for matrix transformations in the SVG output.SVGUnits
getUnits()
Returns the units for the width and height of the SVG element's viewport, as specified in the constructor.double
getWidth()
Returns the width for the SVG element, specified in the constructor.double
getZeroStrokeWidth()
Returns the width to use for the SVG stroke when the AWT stroke specified has a zero width (the default value is0.1
).boolean
hit(Rectangle rect, Shape s, boolean onStroke)
Returnstrue
if the rectangle (in device space) intersects with the shape (the interior, ifonStroke
isfalse
, otherwise the stroked outline of the shape).void
rotate(double theta)
Applies a rotation (anti-clockwise) about(0, 0)
.void
rotate(double theta, double x, double y)
Applies a rotation (anti-clockwise) about(x, y)
.void
scale(double sx, double sy)
Applies a scale transformation.void
setBackground(Color color)
Sets the background color.void
setCheckStrokeControlHint(boolean check)
Sets the flag that controls whether or not this object will observe theKEY_STROKE_CONTROL
rendering hint.void
setClip(int x, int y, int width, int height)
Sets the user clipping region to the specified rectangle.void
setClip(Shape shape)
Sets the user clipping region.void
setColor(Color c)
Sets the foreground color.void
setComposite(Composite comp)
Sets the composite (onlyAlphaComposite
is handled).void
setDefsKeyPrefix(String prefix)
Sets the prefix that will be used for all keys in the DEFS element.void
setFilePrefix(String prefix)
Sets the prefix used to generate a filename for any image that is referenced from the SVG element.void
setFileSuffix(String suffix)
Sets the suffix used to generate a filename for any image that is referenced from the SVG element.void
setFont(Font font)
Sets the font to be used for drawing text.void
setFontFunction(Function<String,String> fontFunction)
Sets the font function that is used to generate SVG font references from Java font family names.void
setFontSizeUnits(SVGUnits fontSizeUnits)
Sets the font size units.void
setGeomDoubleConverter(DoubleFunction<String> converter)
Sets the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output.void
setPaint(Paint paint)
Sets the paint used to draw or fill shapes (or text).void
setPaintMode()
Does nothing in thisSVGGraphics2D
implementation.void
setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
Sets the value for a hint.void
setRenderingHints(Map<?,?> hints)
Sets the rendering hints to the specified collection.void
setStroke(Stroke s)
Sets the stroke that will be used to draw shapes.void
setTransform(AffineTransform t)
Sets the transform.void
setTransformDoubleConverter(DoubleFunction<String> converter)
Sets the double-to-string function that is used when writing coordinates for matrix transformations in the SVG output.void
setXORMode(Color c)
Does nothing in thisSVGGraphics2D
implementation.void
setZeroStrokeWidth(double width)
Sets the width to use for the SVG stroke when the current AWT stroke has a width of 0.0.void
shear(double shx, double shy)
Applies a shear transformation.void
transform(AffineTransform t)
Applies this transform to the existing transform by concatenating it.void
translate(double tx, double ty)
Applies the translation(tx, ty)
.void
translate(int tx, int ty)
Applies the translation(tx, ty)
.Methods inherited from class java.awt.Graphics2D
draw3DRect, fill3DRect
Methods inherited from class java.awt.Graphics
create, drawBytes, drawChars, drawPolygon, drawRect, fillPolygon, finalize, getClipBounds, getClipRect, getFontMetrics, hitClip, toString
-
Constructor Details
-
SVGGraphics2D
Creates a new instance with the specified width and height.- Parameters:
width
- the width of the SVG element.height
- the height of the SVG element.
-
SVGGraphics2D
Creates a new instance with the specified width and height in the given units.- Parameters:
width
- the width of the SVG element.height
- the height of the SVG element.units
- the units for the width and height (null
permitted).- Since:
- 3.2
-
SVGGraphics2D
Creates a new instance with the specified width and height that will populate the suppliedStringBuilder
instance.- Parameters:
width
- the width of the SVG element.height
- the height of the SVG element.units
- the units for the width and height (null
permitted).sb
- the string builder (null
not permitted).- Since:
- 3.2
-
-
Method Details
-
getWidth
Returns the width for the SVG element, specified in the constructor. This value will be written to the SVG element returned by thegetSVGElement()
method.- Returns:
- The width for the SVG element.
-
getHeight
Returns the height for the SVG element, specified in the constructor. This value will be written to the SVG element returned by thegetSVGElement()
method.- Returns:
- The height for the SVG element.
-
getUnits
Returns the units for the width and height of the SVG element's viewport, as specified in the constructor. The default value isnull
).- Returns:
- The units (possibly
null
). - Since:
- 3.2
-
getCheckStrokeControlHint
Returns the flag that controls whether or not this object will observe theKEY_STROKE_CONTROL
rendering hint. The default value istrue
.- Returns:
- A boolean.
- Since:
- 2.0
- See Also:
setCheckStrokeControlHint(boolean)
-
setCheckStrokeControlHint
Sets the flag that controls whether or not this object will observe theKEY_STROKE_CONTROL
rendering hint. When enabled (the default), a hint to normalise strokes will write astroke-style
attribute with the valuecrispEdges
.- Parameters:
check
- the new flag value.- Since:
- 2.0
- See Also:
getCheckStrokeControlHint()
-
getDefsKeyPrefix
Returns the prefix used for all keys in the DEFS element. The default value is"_"+ String.valueOf(System.nanoTime())
.- Returns:
- The prefix string (never
null
). - Since:
- 1.9
-
setDefsKeyPrefix
Sets the prefix that will be used for all keys in the DEFS element. If required, this must be set immediately after construction (before any content generation methods have been called).- Parameters:
prefix
- the prefix (null
not permitted).- Since:
- 1.9
-
getGeomDoubleConverter
Returns the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output. The default function uses the Ryu algorithm for speed (see class description for more details).- Returns:
- The double-to-string function (never
null
). - Since:
- 5.0
-
setGeomDoubleConverter
Sets the double-to-string function that is used when writing coordinates for geometrical shapes in the SVG output. The default converter optimises for speed when generating the SVG and should cover normal usage. However this method provides the ability to substitute an alternative function (for example, one that favours output size over speed of generation).- Parameters:
converter
- the convertor function (null
not permitted).- Since:
- 5.0
- See Also:
setTransformDoubleConverter(java.util.function.DoubleFunction)
-
getTransformDoubleConverter
Returns the double-to-string function that is used when writing values for matrix transformations in the SVG output.- Returns:
- The double-to-string function (never
null
). - Since:
- 5.0
-
setTransformDoubleConverter
Sets the double-to-string function that is used when writing coordinates for matrix transformations in the SVG output. The default converter optimises for speed when generating the SVG and should cover normal usage. However this method provides the ability to substitute an alternative function (for example, one that favours output size over speed of generation).- Parameters:
converter
- the convertor function (null
not permitted).- Since:
- 5.0
- See Also:
setGeomDoubleConverter(java.util.function.DoubleFunction)
-
getFilePrefix
Returns the prefix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.- Returns:
- The file prefix (never
null
). - Since:
- 1.5
-
setFilePrefix
Sets the prefix used to generate a filename for any image that is referenced from the SVG element.- Parameters:
prefix
- the new prefix (null
not permitted).- Since:
- 1.5
-
getFileSuffix
Returns the suffix used to generate a filename for an image that is referenced from, rather than embedded in, the SVG element.- Returns:
- The file suffix (never
null
). - Since:
- 1.5
-
setFileSuffix
Sets the suffix used to generate a filename for any image that is referenced from the SVG element.- Parameters:
suffix
- the new prefix (null
not permitted).- Since:
- 1.5
-
getZeroStrokeWidth
Returns the width to use for the SVG stroke when the AWT stroke specified has a zero width (the default value is0.1
). In the Java specification forBasicStroke
it states "If width is set to 0.0f, the stroke is rendered as the thinnest possible line for the target device and the antialias hint setting." We don't have a means to implement that accurately since we must specify a fixed width.- Returns:
- The width.
- Since:
- 1.9
-
setZeroStrokeWidth
Sets the width to use for the SVG stroke when the current AWT stroke has a width of 0.0.- Parameters:
width
- the new width (must be 0 or greater).- Since:
- 1.9
-
getDeviceConfiguration
Returns the device configuration associated with thisGraphics2D
.- Specified by:
getDeviceConfiguration
in classGraphics2D
- Returns:
- The graphics configuration.
-
create
Creates a new graphics object that is a copy of this graphics object (except that it has not accumulated the drawing operations). Not sure yet when or why this would be useful when creating SVG output. Note that thefontFunction
object (getFontFunction()
) is shared between the existing instance and the new one. -
getPaint
Returns the paint used to draw or fill shapes (or text). The default value isColor.BLACK
.- Specified by:
getPaint
in classGraphics2D
- Returns:
- The paint (never
null
). - See Also:
setPaint(java.awt.Paint)
-
setPaint
Sets the paint used to draw or fill shapes (or text). Ifpaint
is an instance ofColor
, this method will also update the current color attribute (seegetColor()
). If you passnull
to this method, it does nothing (in accordance with the JDK specification).- Specified by:
setPaint
in classGraphics2D
- Parameters:
paint
- the paint (null
is permitted but ignored).- See Also:
getPaint()
-
getColor
Returns the foreground color. This method exists for backwards compatibility in AWT, you should use thegetPaint()
method.- Specified by:
getColor
in classGraphics
- Returns:
- The foreground color (never
null
). - See Also:
getPaint()
-
setColor
Sets the foreground color. This method exists for backwards compatibility in AWT, you should use thesetPaint(java.awt.Paint)
method.- Specified by:
setColor
in classGraphics
- Parameters:
c
- the color (null
permitted but ignored).- See Also:
setPaint(java.awt.Paint)
-
getBackground
Returns the background color. The default value isColor.BLACK
. This is used by theclearRect(int, int, int, int)
method.- Specified by:
getBackground
in classGraphics2D
- Returns:
- The background color (possibly
null
). - See Also:
setBackground(java.awt.Color)
-
setBackground
Sets the background color. This is used by theclearRect(int, int, int, int)
method. The reference implementation allowsnull
for the background color so we allow that too (but for that case, the clearRect method will do nothing).- Specified by:
setBackground
in classGraphics2D
- Parameters:
color
- the color (null
permitted).- See Also:
getBackground()
-
getComposite
Returns the current composite.- Specified by:
getComposite
in classGraphics2D
- Returns:
- The current composite (never
null
). - See Also:
setComposite(java.awt.Composite)
-
setComposite
Sets the composite (onlyAlphaComposite
is handled).- Specified by:
setComposite
in classGraphics2D
- Parameters:
comp
- the composite (null
not permitted).- See Also:
getComposite()
-
getStroke
Returns the current stroke (used when drawing shapes).- Specified by:
getStroke
in classGraphics2D
- Returns:
- The current stroke (never
null
). - See Also:
setStroke(java.awt.Stroke)
-
setStroke
Sets the stroke that will be used to draw shapes.- Specified by:
setStroke
in classGraphics2D
- Parameters:
s
- the stroke (null
not permitted).- See Also:
getStroke()
-
getRenderingHint
Returns the current value for the specified hint. See theSVGHints
class for information about the hints that can be used withSVGGraphics2D
.- Specified by:
getRenderingHint
in classGraphics2D
- Parameters:
hintKey
- the hint key (null
permitted, but the result will benull
also).- Returns:
- The current value for the specified hint
(possibly
null
). - See Also:
setRenderingHint(java.awt.RenderingHints.Key, java.lang.Object)
-
setRenderingHint
Sets the value for a hint. See theSVGHints
class for information about the hints that can be used with this implementation.- Specified by:
setRenderingHint
in classGraphics2D
- Parameters:
hintKey
- the hint key (null
not permitted).hintValue
- the hint value.- See Also:
getRenderingHint(java.awt.RenderingHints.Key)
-
getRenderingHints
Returns a copy of the rendering hints. Modifying the returned copy will have no impact on the state of thisGraphics2D
instance.- Specified by:
getRenderingHints
in classGraphics2D
- Returns:
- The rendering hints (never
null
). - See Also:
setRenderingHints(java.util.Map)
-
setRenderingHints
Sets the rendering hints to the specified collection.- Specified by:
setRenderingHints
in classGraphics2D
- Parameters:
hints
- the new set of hints (null
not permitted).- See Also:
getRenderingHints()
-
addRenderingHints
Adds all the supplied rendering hints.- Specified by:
addRenderingHints
in classGraphics2D
- Parameters:
hints
- the hints (null
not permitted).
-
draw
Draws the specified shape with the currentpaint
andstroke
. There is direct handling forLine2D
,Rectangle2D
,Ellipse2D
andPath2D
. All other shapes are mapped to aGeneralPath
and then drawn (effectively asPath2D
objects).- Specified by:
draw
in classGraphics2D
- Parameters:
s
- the shape (null
not permitted).- See Also:
fill(java.awt.Shape)
-
fill
Fills the specified shape with the currentpaint
. There is direct handling forRectangle2D
,Ellipse2D
andPath2D
. All other shapes are mapped to aGeneralPath
and then filled.- Specified by:
fill
in classGraphics2D
- Parameters:
s
- the shape (null
not permitted).- See Also:
draw(java.awt.Shape)
-
getFont
Returns the current font used for drawing text.- Specified by:
getFont
in classGraphics
- Returns:
- The current font (never
null
). - See Also:
setFont(java.awt.Font)
-
setFont
Sets the font to be used for drawing text. -
getFontFunction
Returns the function that generates SVG font references from a supplied Java font family name. The default function will convert Java logical font names to the equivalent SVG generic font name, pass-through all other font names unchanged, and surround the result in single quotes.- Returns:
- The font mapper (never
null
). - Since:
- 5.0
- See Also:
setFontFunction(java.util.function.Function)
-
setFontFunction
Sets the font function that is used to generate SVG font references from Java font family names.- Parameters:
fontFunction
- the font mapper (null
not permitted).- Since:
- 5.0
-
getFontSizeUnits
Returns the font size units. The default value isSVGUnits.PX
.- Returns:
- The font size units.
- Since:
- 3.4
-
setFontSizeUnits
Sets the font size units. In general, if this method is used it should be called immediately after theSVGGraphics2D
instance is created and before any content is generated.- Parameters:
fontSizeUnits
- the font size units (null
not permitted).- Since:
- 3.4
-
getFontMetrics
Returns the font metrics for the specified font.- Specified by:
getFontMetrics
in classGraphics
- Parameters:
f
- the font.- Returns:
- The font metrics.
-
getFontRenderContext
Returns the font render context.- Specified by:
getFontRenderContext
in classGraphics2D
- Returns:
- The font render context (never
null
).
-
drawString
Draws a string at(x, y)
. The start of the text at the baseline level will be aligned with the(x, y)
point.
Note that you can make use of theSVGHints.KEY_TEXT_RENDERING
hint when drawing strings (this is completely optional though).- Specified by:
drawString
in classGraphics2D
- Parameters:
str
- the string (null
not permitted).x
- the x-coordinate.y
- the y-coordinate.- See Also:
drawString(java.lang.String, float, float)
-
drawString
Draws a string at(x, y)
. The start of the text at the baseline level will be aligned with the(x, y)
point.
Note that you can make use of theSVGHints.KEY_TEXT_RENDERING
hint when drawing strings (this is completely optional though).- Specified by:
drawString
in classGraphics2D
- Parameters:
str
- the string (null
not permitted).x
- the x-coordinate.y
- the y-coordinate.
-
drawString
Draws a string of attributed characters at(x, y)
. The call is delegated todrawString(AttributedCharacterIterator, float, float)
.- Specified by:
drawString
in classGraphics2D
- Parameters:
iterator
- an iterator for the characters.x
- the x-coordinate.y
- the x-coordinate.
-
drawString
Draws a string of attributed characters at(x, y)
.- Specified by:
drawString
in classGraphics2D
- Parameters:
iterator
- an iterator over the characters (null
not permitted).x
- the x-coordinate.y
- the y-coordinate.
-
drawGlyphVector
Draws the specified glyph vector at the location(x, y)
.- Specified by:
drawGlyphVector
in classGraphics2D
- Parameters:
g
- the glyph vector (null
not permitted).x
- the x-coordinate.y
- the y-coordinate.
-
translate
Applies the translation(tx, ty)
. This call is delegated totranslate(double, double)
.- Specified by:
translate
in classGraphics2D
- Parameters:
tx
- the x-translation.ty
- the y-translation.- See Also:
translate(double, double)
-
translate
Applies the translation(tx, ty)
.- Specified by:
translate
in classGraphics2D
- Parameters:
tx
- the x-translation.ty
- the y-translation.
-
rotate
Applies a rotation (anti-clockwise) about(0, 0)
.- Specified by:
rotate
in classGraphics2D
- Parameters:
theta
- the rotation angle (in radians).
-
rotate
Applies a rotation (anti-clockwise) about(x, y)
.- Specified by:
rotate
in classGraphics2D
- Parameters:
theta
- the rotation angle (in radians).x
- the x-coordinate.y
- the y-coordinate.
-
scale
Applies a scale transformation.- Specified by:
scale
in classGraphics2D
- Parameters:
sx
- the x-scaling factor.sy
- the y-scaling factor.
-
shear
Applies a shear transformation. This is equivalent to the following call to thetransform
method:
-
transform(AffineTransform.getShearInstance(shx, shy));
- Specified by:
shear
in classGraphics2D
- Parameters:
shx
- the x-shear factor.shy
- the y-shear factor.
-
-
transform
Applies this transform to the existing transform by concatenating it.- Specified by:
transform
in classGraphics2D
- Parameters:
t
- the transform (null
not permitted).
-
getTransform
Returns a copy of the current transform.- Specified by:
getTransform
in classGraphics2D
- Returns:
- A copy of the current transform (never
null
). - See Also:
setTransform(java.awt.geom.AffineTransform)
-
setTransform
Sets the transform.- Specified by:
setTransform
in classGraphics2D
- Parameters:
t
- the new transform (null
permitted, resets to the identity transform).- See Also:
getTransform()
-
hit
Returnstrue
if the rectangle (in device space) intersects with the shape (the interior, ifonStroke
isfalse
, otherwise the stroked outline of the shape).- Specified by:
hit
in classGraphics2D
- Parameters:
rect
- a rectangle (in device space).s
- the shape.onStroke
- test the stroked outline only?- Returns:
- A boolean.
-
setPaintMode
Does nothing in thisSVGGraphics2D
implementation.- Specified by:
setPaintMode
in classGraphics
-
setXORMode
Does nothing in thisSVGGraphics2D
implementation.- Specified by:
setXORMode
in classGraphics
- Parameters:
c
- ignored
-
getClipBounds
Returns the bounds of the user clipping region.- Specified by:
getClipBounds
in classGraphics
- Returns:
- The clip bounds (possibly
null
). - See Also:
getClip()
-
getClip
Returns the user clipping region. The initial default value isnull
.- Specified by:
getClip
in classGraphics
- Returns:
- The user clipping region (possibly
null
). - See Also:
setClip(java.awt.Shape)
-
setClip
Sets the user clipping region. -
clip
Clips to the intersection of the current clipping region and the specified shape. According to the Oracle API specification, this method will accept anull
argument, however there is a bug report (opened in 2004 and fixed in 2021) that describes the passing ofnull
as "not recommended":- Specified by:
clip
in classGraphics2D
- Parameters:
s
- the clip shape (null
not recommended).
-
clipRect
Clips to the intersection of the current clipping region and the specified rectangle. -
setClip
Sets the user clipping region to the specified rectangle. -
drawLine
Draws a line from(x1, y1)
to(x2, y2)
using the currentpaint
andstroke
. -
fillRect
Fills the specified rectangle with the currentpaint
. -
clearRect
Clears the specified rectangle by filling it with the current background color. If the background color isnull
, this method will do nothing.- Specified by:
clearRect
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.- See Also:
getBackground()
-
drawRoundRect
Draws a rectangle with rounded corners using the currentpaint
andstroke
.- Specified by:
drawRoundRect
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.arcWidth
- the arc-width.arcHeight
- the arc-height.- See Also:
fillRoundRect(int, int, int, int, int, int)
-
fillRoundRect
Fills a rectangle with rounded corners using the currentpaint
.- Specified by:
fillRoundRect
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.arcWidth
- the arc-width.arcHeight
- the arc-height.- See Also:
drawRoundRect(int, int, int, int, int, int)
-
drawOval
Draws an oval framed by the rectangle(x, y, width, height)
using the currentpaint
andstroke
.- Specified by:
drawOval
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.- See Also:
fillOval(int, int, int, int)
-
fillOval
Fills an oval framed by the rectangle(x, y, width, height)
.- Specified by:
fillOval
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.- See Also:
drawOval(int, int, int, int)
-
drawArc
Draws an arc contained within the rectangle(x, y, width, height)
, starting atstartAngle
and continuing througharcAngle
degrees using the currentpaint
andstroke
.- Specified by:
drawArc
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.startAngle
- the start angle in degrees, 0 = 3 o'clock.arcAngle
- the angle (anticlockwise) in degrees.- See Also:
fillArc(int, int, int, int, int, int)
-
fillArc
Fills an arc contained within the rectangle(x, y, width, height)
, starting atstartAngle
and continuing througharcAngle
degrees, using the currentpaint
.- Specified by:
fillArc
in classGraphics
- Parameters:
x
- the x-coordinate.y
- the y-coordinate.width
- the width.height
- the height.startAngle
- the start angle in degrees, 0 = 3 o'clock.arcAngle
- the angle (anticlockwise) in degrees.- See Also:
drawArc(int, int, int, int, int, int)
-
drawPolyline
Draws the specified multi-segment line using the currentpaint
andstroke
.- Specified by:
drawPolyline
in classGraphics
- Parameters:
xPoints
- the x-points.yPoints
- the y-points.nPoints
- the number of points to use for the polyline.
-
drawPolygon
Draws the specified polygon using the currentpaint
andstroke
.- Specified by:
drawPolygon
in classGraphics
- Parameters:
xPoints
- the x-points.yPoints
- the y-points.nPoints
- the number of points to use for the polygon.- See Also:
fillPolygon(int[], int[], int)
-
fillPolygon
Fills the specified polygon using the currentpaint
.- Specified by:
fillPolygon
in classGraphics
- Parameters:
xPoints
- the x-points.yPoints
- the y-points.nPoints
- the number of points to use for the polygon.- See Also:
drawPolygon(int[], int[], int)
-
drawImage
Draws an image at the location(x, y)
. Note that theobserver
is ignored. -
drawImage
Draws the image into the rectangle defined by(x, y, w, h)
. Note that theobserver
is ignored (it is not useful in this context). -
drawImage
Draws an image at the location(x, y)
. Note that theobserver
is ignored. -
drawImage
public boolean drawImage(Image img, int x, int y, int w, int h, Color bgcolor, ImageObserver observer)Draws an image to the rectangle(x, y, w, h)
(scaling it if required), first filling the background with the specified color. Note that theobserver
is ignored. -
drawImage
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)
) into the destination rectangle(dx1, dy1, dx2, dy2)
. Note that theobserver
is ignored.- Specified by:
drawImage
in classGraphics
- Parameters:
img
- the image.dx1
- the x-coordinate for the top left of the destination.dy1
- the y-coordinate for the top left of the destination.dx2
- the x-coordinate for the bottom right of the destination.dy2
- the y-coordinate for the bottom right of the destination.sx1
- the x-coordinate for the top left of the source.sy1
- the y-coordinate for the top left of the source.sx2
- the x-coordinate for the bottom right of the source.sy2
- the y-coordinate for the bottom right of the source.- Returns:
true
if the image is drawn.
-
drawImage
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)Draws part of an image (defined by the source rectangle(sx1, sy1, sx2, sy2)
) into the destination rectangle(dx1, dy1, dx2, dy2)
. The destination rectangle is first cleared by filling it with the specifiedbgcolor
. Note that theobserver
is ignored.- Specified by:
drawImage
in classGraphics
- Parameters:
img
- the image.dx1
- the x-coordinate for the top left of the destination.dy1
- the y-coordinate for the top left of the destination.dx2
- the x-coordinate for the bottom right of the destination.dy2
- the y-coordinate for the bottom right of the destination.sx1
- the x-coordinate for the top left of the source.sy1
- the y-coordinate for the top left of the source.sx2
- the x-coordinate for the bottom right of the source.sy2
- the y-coordinate for the bottom right of the source.bgcolor
- the background color (null
permitted).observer
- ignored.- Returns:
true
if the image is drawn.
-
drawRenderedImage
Draws the rendered image. Ifimg
isnull
this method does nothing.- Specified by:
drawRenderedImage
in classGraphics2D
- Parameters:
img
- the image (null
permitted).xform
- the transform.
-
drawRenderableImage
Draws the renderable image.- Specified by:
drawRenderableImage
in classGraphics2D
- Parameters:
img
- the renderable image.xform
- the transform.
-
drawImage
Draws an image with the specified transform. Note that theobserver
is ignored.- Specified by:
drawImage
in classGraphics2D
- Parameters:
img
- the image.xform
- the transform (null
permitted).obs
- the image observer (ignored).- Returns:
true
if the image is drawn.
-
drawImage
Draws the image resulting from applying theBufferedImageOp
to the specified image at the location(x, y)
.- Specified by:
drawImage
in classGraphics2D
- Parameters:
img
- the image.op
- the operation (null
permitted).x
- the x-coordinate.y
- the y-coordinate.
-
copyArea
This method does nothing. The operation assumes that the output is in bitmap form, which is not the case for SVG, so we silently ignore this method call. -
dispose
This method does nothing, there are no resources to dispose. -
getSVGElement
Returns the SVG element that has been generated by calls to thisGraphics2D
implementation.- Returns:
- The SVG element.
-
getSVGElement
Returns the SVG element that has been generated by calls to thisGraphics2D
implementation, giving it the specifiedid
. Ifid
isnull
, the element will have noid
attribute.- Parameters:
id
- the element id (null
permitted).- Returns:
- A string containing the SVG element.
- Since:
- 1.8
-
getSVGElement
public String getSVGElement(String id, boolean includeDimensions, ViewBox viewBox, PreserveAspectRatio preserveAspectRatio, MeetOrSlice meetOrSlice)Returns the SVG element that has been generated by calls to thisGraphics2D
implementation, giving it the specifiedid
. Ifid
isnull
, the element will have noid
attribute. This method also allows for aviewBox
to be defined, along with the settings that handle scaling.- Parameters:
id
- the element id (null
permitted).includeDimensions
- include the width and height attributes?viewBox
- the view box specification (ifnull
then noviewBox
attribute will be defined).preserveAspectRatio
- the value of thepreserveAspectRatio
attribute (ifnull
then not attribute will be defined).meetOrSlice
- the value of the meetOrSlice attribute.- Returns:
- A string containing the SVG element.
- Since:
- 3.2
-
getSVGDocument
Returns an SVG document (this contains the content returned by thegetSVGElement()
method, prepended with the required document header).- Returns:
- An SVG document.
-
getSVGImages
Returns the list of image elements that have been referenced in the SVG output but not embedded. If the image files don't already exist, you can use this list as the basis for creating the image files.- Returns:
- The list of image elements.
- See Also:
SVGHints.KEY_IMAGE_HANDLING
-
getElementIDs
Returns a new set containing the element IDs that have been used in output so far.- Returns:
- The element IDs.
- Since:
- 1.5
-