Package com.mxgraph.canvas
Class mxGraphicsCanvas2D
- java.lang.Object
-
- com.mxgraph.canvas.mxGraphicsCanvas2D
-
- All Implemented Interfaces:
mxICanvas2D
public class mxGraphicsCanvas2D extends java.lang.Object implements mxICanvas2D
Used for exporting images. To render to an image from a given XML string, graph size and background color, the following code is used:BufferedImage image = mxUtils.createBufferedImage(width, height, background); Graphics2D g2 = image.createGraphics(); mxUtils.setAntiAlias(g2, true, true); XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); reader.setContentHandler(new mxSaxOutputHandler(new mxGraphicsCanvas2D(g2))); reader.parse(new InputSource(new StringReader(xml)));Text rendering is available for plain text and HTML markup, the latter with optional word wrapping. CSS support is limited to the following: http://docs.oracle.com/javase/6/docs/api/index.html?javax/swing/text/html/CSS.html
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classmxGraphicsCanvas2D.CanvasState
-
Field Summary
Fields Modifier and Type Field Description static intCOLOR_CACHE_SIZESpecifies the size of the cache used to store parsed colorsprotected java.util.LinkedHashMap<java.lang.String,java.awt.Color>colorCacheCaches parsed colors.protected java.awt.geom.GeneralPathcurrentPathHolds the current path.protected java.awt.Graphics2DgraphicsReference to the graphics instance for painting.static doubleHTML_SCALEScale for rendering HTML output.static java.lang.StringHTML_UNITUnit to be used for HTML labels.static intIMAGE_SCALINGSpecifies the image scaling quality.static intJAVA_TEXT_WIDTH_DELTASpecifies the additional pixels when computing the text width for HTML labels.protected intlastCapStroke caching.protected booleanlastDashedStroke caching.protected java.lang.ObjectlastDashPatternStroke caching.protected java.awt.FontlastFontFont caching.protected java.lang.StringlastFontFamilyFont caching.protected intlastFontSizeFont caching.protected intlastFontStyleFont caching.protected intlastJoinStroke caching.protected floatlastMiterLimitStroke caching.protected java.awt.StrokelastStrokeStroke caching.protected floatlastStrokeWidthStroke caching.protected javax.swing.CellRendererPanerendererPaneOptional renderer pane to be used for HTML label rendering.protected java.util.Stack<mxGraphicsCanvas2D.CanvasState>stackStack of states for save/restore.protected mxGraphicsCanvas2D.CanvasStatestateRepresents the current state of the canvas.protected booleantextEnabledSpecifies if text output should be rendered.
-
Constructor Summary
Constructors Constructor Description mxGraphicsCanvas2D(java.awt.Graphics2D g)Constructs a new graphics export canvas.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbegin()Begins a new path.protected mxGraphicsCanvas2D.CanvasStatecloneState(mxGraphicsCanvas2D.CanvasState state)Returns a clone of the given state.voidclose()Closes the current path.protected java.awt.FontcreateFont(java.lang.String family, int style, int size)Hook for subclassers to implement font caching.protected java.lang.StringcreateHtmlDocument(java.lang.String text, java.lang.String style)Creates a HTML document for the given text and CSS style.protected java.lang.StringcreateHtmlDocument(java.lang.String text, java.lang.String align, java.lang.String valign, int w, int h, boolean wrap, java.lang.String overflow, boolean clip)Creates a HTML document around the given markup.protected java.awt.Graphics2DcreateImageGraphics(double x, double y, double w, double h, boolean flipH, boolean flipV)Creates a graphic instance for rendering an image.protected java.awt.Graphics2DcreateTextGraphics(double x, double y, double w, double h, double rotation, boolean clip, java.lang.String align, java.lang.String valign)Returns a new graphics instance with the correct color and font for text rendering.voidcurveTo(double x1, double y1, double x2, double y2, double x3, double y3)Draws a bezier curve to the given point.protected voiddrawImage(java.awt.Graphics2D graphics, java.awt.Image image, int x, int y)voidellipse(double x, double y, double w, double h)Next fill or stroke should draw an ellipse.voidfill()Fills the current path.voidfillAndStroke()Fills and paints the outline of the current path.protected java.lang.StringgetFontName(java.lang.String family)Returns a font name for the given CSS values for font-family.java.awt.Graphics2DgetGraphics()Returns the graphics instance.protected java.awt.RectanglegetImageBounds(java.awt.Image img, double x, double y, double w, double h, boolean aspect)protected java.awt.DimensiongetImageSize(java.awt.Image image)Returns the size for the given image.protected java.awt.geom.Point2DgetMargin(java.lang.String align, java.lang.String valign)protected javax.swing.JLabelgetTextRenderer()Hook to return the renderer for HTML formatted text.protected voidhtmlText(double x, double y, double w, double h, java.lang.String str, java.lang.String align, java.lang.String valign, boolean wrap, java.lang.String format, java.lang.String overflow, boolean clip, double rotation)Draws the given HTML text.voidimage(double x, double y, double w, double h, java.lang.String src, boolean aspect, boolean flipH, boolean flipV)Draws the given image.booleanisTextEnabled()Returns true if text should be rendered.voidlineTo(double x, double y)Draws a line to the given path.protected java.awt.ImageloadImage(java.lang.String src)Hook for image caching.voidmoveTo(double x, double y)Moves to the given path.protected voidpaintCurrentPath(boolean filled, boolean stroked)protected voidpaintShadow(boolean filled, boolean stroked)protected java.awt.ColorparseColor(java.lang.String hex)Helper method that usesmxUtils.parseColor(String).protected java.awt.ColorparseColor(java.lang.String hex, double alpha)Helper method that usesmxUtils.parseColor(String).voidplainText(double x, double y, double w, double h, java.lang.String str, java.lang.String align, java.lang.String valign, boolean wrap, java.lang.String format, java.lang.String overflow, boolean clip, double rotation)Draws the given text.voidquadTo(double x1, double y1, double x2, double y2)Draws a quadratic curve to the given point.voidrect(double x, double y, double w, double h)Next fill or stroke should draw a rectangle.voidrestore()Restores the last canvas state.voidrotate(double theta, boolean flipH, boolean flipV, double cx, double cy)Rotates the canvas by the given angle around the given center.voidroundrect(double x, double y, double w, double h, double dx, double dy)Implements a rounded rectangle using a path.voidsave()Saves the current canvas state.voidscale(double value)Uniformaly scales the canvas by the given amount.protected java.awt.ImagescaleImage(java.awt.Image img, int w, int h)UsesIMAGE_SCALINGto scale the given image.voidsetAlpha(double value)Default value 1.voidsetDashed(boolean value)Sets the dashed state.voidsetDashed(boolean value, boolean fixDash)Sets the dashed state.voidsetDashPattern(java.lang.String value)Sets the dash pattern.voidsetFillAlpha(double value)Default value 1.voidsetFillColor(java.lang.String value)Default valuemxConstants.NONE.voidsetFontBackgroundColor(java.lang.String value)Default value "#000000".voidsetFontBorderColor(java.lang.String value)Default value "#000000".voidsetFontColor(java.lang.String value)Default value "#000000".voidsetFontFamily(java.lang.String value)Default valuemxConstants.DEFAULT_FONTFAMILY.voidsetFontSize(double value)Default valuemxConstants.DEFAULT_FONTSIZE.voidsetFontStyle(int value)Default value 0.voidsetGradient(java.lang.String color1, java.lang.String color2, double x, double y, double w, double h, java.lang.String direction, double alpha1, double alpha2)Prepares the canvas to draw a gradient.voidsetGraphics(java.awt.Graphics2D value)Sets the graphics instance.voidsetLineCap(java.lang.String value)Sets the linecap.voidsetLineJoin(java.lang.String value)Sets the linejoin.voidsetMiterLimit(double value)Sets the miterlimit.voidsetShadow(boolean value)Enables or disables the painting of shadows.voidsetShadowAlpha(double value)Default valuemxConstants.NONE.voidsetShadowColor(java.lang.String value)Default valuemxConstants.NONE.voidsetShadowOffset(double dx, double dy)Default valuemxConstants.NONE.voidsetStrokeAlpha(double value)Default value 1.voidsetStrokeColor(java.lang.String value)Caches color conversion as it is expensive.voidsetStrokeWidth(double value)Sets the stroke width.voidsetTextEnabled(boolean value)Disables or enables text rendering.voidstroke()Paints the outline of the current path.voidtext(double x, double y, double w, double h, java.lang.String str, java.lang.String align, java.lang.String valign, boolean wrap, java.lang.String format, java.lang.String overflow, boolean clip, double rotation, java.lang.String textDirection)Draws the given text.voidtranslate(double dx, double dy)Translates the canvas by the given amount.protected voidupdateFont()protected voidupdateStroke()
-
-
-
Field Detail
-
IMAGE_SCALING
public static int IMAGE_SCALING
Specifies the image scaling quality. Default is Image.SCALE_SMOOTH. SeescaleImage(Image, int, int)
-
JAVA_TEXT_WIDTH_DELTA
public static int JAVA_TEXT_WIDTH_DELTA
Specifies the additional pixels when computing the text width for HTML labels. Default is 5.
-
HTML_SCALE
public static double HTML_SCALE
Scale for rendering HTML output. Default is 1.
-
HTML_UNIT
public static java.lang.String HTML_UNIT
Unit to be used for HTML labels. Default is "pt". If you units within HTML labels are used, this should match those units to produce a consistent output. If the value is "px", then HTML_SCALE should be changed the match the ratio between px units for rendering HTML and the units used for rendering other graphics elements. This value is 0.6 on Linux and 0.75 on all other platforms.
-
COLOR_CACHE_SIZE
public static int COLOR_CACHE_SIZE
Specifies the size of the cache used to store parsed colors
-
graphics
protected java.awt.Graphics2D graphics
Reference to the graphics instance for painting.
-
textEnabled
protected boolean textEnabled
Specifies if text output should be rendered. Default is true.
-
state
protected transient mxGraphicsCanvas2D.CanvasState state
Represents the current state of the canvas.
-
stack
protected transient java.util.Stack<mxGraphicsCanvas2D.CanvasState> stack
Stack of states for save/restore.
-
currentPath
protected transient java.awt.geom.GeneralPath currentPath
Holds the current path.
-
rendererPane
protected javax.swing.CellRendererPane rendererPane
Optional renderer pane to be used for HTML label rendering.
-
lastFont
protected transient java.awt.Font lastFont
Font caching.
-
lastFontStyle
protected transient int lastFontStyle
Font caching.
-
lastFontSize
protected transient int lastFontSize
Font caching.
-
lastFontFamily
protected transient java.lang.String lastFontFamily
Font caching.
-
lastStroke
protected transient java.awt.Stroke lastStroke
Stroke caching.
-
lastStrokeWidth
protected transient float lastStrokeWidth
Stroke caching.
-
lastCap
protected transient int lastCap
Stroke caching.
-
lastJoin
protected transient int lastJoin
Stroke caching.
-
lastMiterLimit
protected transient float lastMiterLimit
Stroke caching.
-
lastDashed
protected transient boolean lastDashed
Stroke caching.
-
lastDashPattern
protected transient java.lang.Object lastDashPattern
Stroke caching.
-
colorCache
protected transient java.util.LinkedHashMap<java.lang.String,java.awt.Color> colorCache
Caches parsed colors.
-
-
Method Detail
-
setGraphics
public void setGraphics(java.awt.Graphics2D value)
Sets the graphics instance.
-
getGraphics
public java.awt.Graphics2D getGraphics()
Returns the graphics instance.
-
isTextEnabled
public boolean isTextEnabled()
Returns true if text should be rendered.
-
setTextEnabled
public void setTextEnabled(boolean value)
Disables or enables text rendering.
-
save
public void save()
Saves the current canvas state.- Specified by:
savein interfacemxICanvas2D
-
restore
public void restore()
Restores the last canvas state.- Specified by:
restorein interfacemxICanvas2D
-
cloneState
protected mxGraphicsCanvas2D.CanvasState cloneState(mxGraphicsCanvas2D.CanvasState state)
Returns a clone of the given state.
-
scale
public void scale(double value)
Description copied from interface:mxICanvas2DUniformaly scales the canvas by the given amount.- Specified by:
scalein interfacemxICanvas2D- Parameters:
value- The new scale value.
-
translate
public void translate(double dx, double dy)Description copied from interface:mxICanvas2DTranslates the canvas by the given amount.- Specified by:
translatein interfacemxICanvas2D- Parameters:
dx- X-coordinate of the translation.dy- Y-coordinate of the translation.
-
rotate
public void rotate(double theta, boolean flipH, boolean flipV, double cx, double cy)Description copied from interface:mxICanvas2DRotates the canvas by the given angle around the given center. This method may add rendering overhead and should be used with care.- Specified by:
rotatein interfacemxICanvas2D- Parameters:
theta- Rotation angle in degrees (0 - 360).flipH- Specifies if drawing should be flipped horizontally.flipV- Specifies if drawing should be flipped vertically.cx- X-coordinate of the center point.cy- Y-coordinate of the center point.
-
setStrokeWidth
public void setStrokeWidth(double value)
Description copied from interface:mxICanvas2DSets the stroke width. This should default to 1 if unset.- Specified by:
setStrokeWidthin interfacemxICanvas2D- Parameters:
value- Width of the stroke. The value should be multiplied by the current scale.
-
setStrokeColor
public void setStrokeColor(java.lang.String value)
Caches color conversion as it is expensive.- Specified by:
setStrokeColorin interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setDashed
public void setDashed(boolean value)
Description copied from interface:mxICanvas2DSets the dashed state. This should default to false if unset.- Specified by:
setDashedin interfacemxICanvas2D- Parameters:
value- Boolean representing the dashed state.
-
setDashed
public void setDashed(boolean value, boolean fixDash)Description copied from interface:mxICanvas2DSets the dashed state. This should default to false if unset.- Specified by:
setDashedin interfacemxICanvas2D- Parameters:
value- Boolean representing the dashed state.
-
setDashPattern
public void setDashPattern(java.lang.String value)
Description copied from interface:mxICanvas2DSets the dash pattern. This should default to "3 3" if unset.- Specified by:
setDashPatternin interfacemxICanvas2D- Parameters:
value- Space separated list of floats representing the dash pattern. The value should be multiplied by the current scale.
-
setLineCap
public void setLineCap(java.lang.String value)
Description copied from interface:mxICanvas2DSets the linecap. This should default to "flat" if unset.- Specified by:
setLineCapin interfacemxICanvas2D- Parameters:
value- "flat", "square" or "round".
-
setLineJoin
public void setLineJoin(java.lang.String value)
Description copied from interface:mxICanvas2DSets the linejoin. This should default to "miter" if unset.- Specified by:
setLineJoinin interfacemxICanvas2D- Parameters:
value- "miter", "round" or "bevel".
-
setMiterLimit
public void setMiterLimit(double value)
Description copied from interface:mxICanvas2DSets the miterlimit. This should default to 10 if unset.- Specified by:
setMiterLimitin interfacemxICanvas2D
-
setFontSize
public void setFontSize(double value)
Description copied from interface:mxICanvas2DDefault valuemxConstants.DEFAULT_FONTSIZE.- Specified by:
setFontSizein interfacemxICanvas2D
-
setFontColor
public void setFontColor(java.lang.String value)
Description copied from interface:mxICanvas2DDefault value "#000000".- Specified by:
setFontColorin interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setFontBackgroundColor
public void setFontBackgroundColor(java.lang.String value)
Description copied from interface:mxICanvas2DDefault value "#000000".- Specified by:
setFontBackgroundColorin interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setFontBorderColor
public void setFontBorderColor(java.lang.String value)
Description copied from interface:mxICanvas2DDefault value "#000000".- Specified by:
setFontBorderColorin interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setFontFamily
public void setFontFamily(java.lang.String value)
Description copied from interface:mxICanvas2DDefault valuemxConstants.DEFAULT_FONTFAMILY.- Specified by:
setFontFamilyin interfacemxICanvas2D
-
setFontStyle
public void setFontStyle(int value)
Description copied from interface:mxICanvas2DDefault value 0. SeemxConstants.STYLE_FONTSTYLE.- Specified by:
setFontStylein interfacemxICanvas2D
-
setAlpha
public void setAlpha(double value)
Description copied from interface:mxICanvas2DDefault value 1. This method may add rendering overhead and should be used with care.- Specified by:
setAlphain interfacemxICanvas2D
-
setFillAlpha
public void setFillAlpha(double value)
Description copied from interface:mxICanvas2DDefault value 1. This method may add rendering overhead and should be used with care.- Specified by:
setFillAlphain interfacemxICanvas2D
-
setStrokeAlpha
public void setStrokeAlpha(double value)
Description copied from interface:mxICanvas2DDefault value 1. This method may add rendering overhead and should be used with care.- Specified by:
setStrokeAlphain interfacemxICanvas2D
-
setFillColor
public void setFillColor(java.lang.String value)
Description copied from interface:mxICanvas2DDefault valuemxConstants.NONE.- Specified by:
setFillColorin interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setGradient
public void setGradient(java.lang.String color1, java.lang.String color2, double x, double y, double w, double h, java.lang.String direction, double alpha1, double alpha2)Description copied from interface:mxICanvas2DPrepares the canvas to draw a gradient.- Specified by:
setGradientin interfacemxICanvas2Ddirection- Direction may be null. Use default valuemxConstants.DIRECTION_SOUTH.
-
parseColor
protected java.awt.Color parseColor(java.lang.String hex)
Helper method that usesmxUtils.parseColor(String).
-
parseColor
protected java.awt.Color parseColor(java.lang.String hex, double alpha)Helper method that usesmxUtils.parseColor(String).
-
rect
public void rect(double x, double y, double w, double h)Description copied from interface:mxICanvas2DNext fill or stroke should draw a rectangle.- Specified by:
rectin interfacemxICanvas2D
-
roundrect
public void roundrect(double x, double y, double w, double h, double dx, double dy)Implements a rounded rectangle using a path.- Specified by:
roundrectin interfacemxICanvas2D
-
ellipse
public void ellipse(double x, double y, double w, double h)Description copied from interface:mxICanvas2DNext fill or stroke should draw an ellipse.- Specified by:
ellipsein interfacemxICanvas2D
-
image
public void image(double x, double y, double w, double h, java.lang.String src, boolean aspect, boolean flipH, boolean flipV)Description copied from interface:mxICanvas2DDraws the given image.- Specified by:
imagein interfacemxICanvas2D
-
drawImage
protected void drawImage(java.awt.Graphics2D graphics, java.awt.Image image, int x, int y)
-
loadImage
protected java.awt.Image loadImage(java.lang.String src)
Hook for image caching.
-
getImageBounds
protected final java.awt.Rectangle getImageBounds(java.awt.Image img, double x, double y, double w, double h, boolean aspect)
-
getImageSize
protected java.awt.Dimension getImageSize(java.awt.Image image)
Returns the size for the given image.
-
scaleImage
protected java.awt.Image scaleImage(java.awt.Image img, int w, int h)UsesIMAGE_SCALINGto scale the given image.
-
createImageGraphics
protected final java.awt.Graphics2D createImageGraphics(double x, double y, double w, double h, boolean flipH, boolean flipV)Creates a graphic instance for rendering an image.
-
createHtmlDocument
protected java.lang.String createHtmlDocument(java.lang.String text, java.lang.String align, java.lang.String valign, int w, int h, boolean wrap, java.lang.String overflow, boolean clip)Creates a HTML document around the given markup.
-
createHtmlDocument
protected java.lang.String createHtmlDocument(java.lang.String text, java.lang.String style)Creates a HTML document for the given text and CSS style.
-
getTextRenderer
protected javax.swing.JLabel getTextRenderer()
Hook to return the renderer for HTML formatted text. This implementation returns the shared instance of mxLighweightLabel.
-
getMargin
protected java.awt.geom.Point2D getMargin(java.lang.String align, java.lang.String valign)
-
htmlText
protected void htmlText(double x, double y, double w, double h, java.lang.String str, java.lang.String align, java.lang.String valign, boolean wrap, java.lang.String format, java.lang.String overflow, boolean clip, double rotation)Draws the given HTML text.
-
text
public void text(double x, double y, double w, double h, java.lang.String str, java.lang.String align, java.lang.String valign, boolean wrap, java.lang.String format, java.lang.String overflow, boolean clip, double rotation, java.lang.String textDirection)Draws the given text.- Specified by:
textin interfacemxICanvas2D
-
plainText
public void plainText(double x, double y, double w, double h, java.lang.String str, java.lang.String align, java.lang.String valign, boolean wrap, java.lang.String format, java.lang.String overflow, boolean clip, double rotation)Draws the given text.
-
createTextGraphics
protected final java.awt.Graphics2D createTextGraphics(double x, double y, double w, double h, double rotation, boolean clip, java.lang.String align, java.lang.String valign)Returns a new graphics instance with the correct color and font for text rendering.
-
begin
public void begin()
Description copied from interface:mxICanvas2DBegins a new path.- Specified by:
beginin interfacemxICanvas2D
-
moveTo
public void moveTo(double x, double y)Description copied from interface:mxICanvas2DMoves to the given path.- Specified by:
moveToin interfacemxICanvas2D
-
lineTo
public void lineTo(double x, double y)Description copied from interface:mxICanvas2DDraws a line to the given path.- Specified by:
lineToin interfacemxICanvas2D
-
quadTo
public void quadTo(double x1, double y1, double x2, double y2)Description copied from interface:mxICanvas2DDraws a quadratic curve to the given point.- Specified by:
quadToin interfacemxICanvas2D
-
curveTo
public void curveTo(double x1, double y1, double x2, double y2, double x3, double y3)Description copied from interface:mxICanvas2DDraws a bezier curve to the given point.- Specified by:
curveToin interfacemxICanvas2D
-
close
public void close()
Closes the current path.- Specified by:
closein interfacemxICanvas2D
-
stroke
public void stroke()
Description copied from interface:mxICanvas2DPaints the outline of the current path.- Specified by:
strokein interfacemxICanvas2D
-
fill
public void fill()
Description copied from interface:mxICanvas2DFills the current path.- Specified by:
fillin interfacemxICanvas2D
-
fillAndStroke
public void fillAndStroke()
Description copied from interface:mxICanvas2DFills and paints the outline of the current path.- Specified by:
fillAndStrokein interfacemxICanvas2D
-
paintCurrentPath
protected void paintCurrentPath(boolean filled, boolean stroked)
-
paintShadow
protected void paintShadow(boolean filled, boolean stroked)
-
setShadow
public void setShadow(boolean value)
Description copied from interface:mxICanvas2DEnables or disables the painting of shadows.- Specified by:
setShadowin interfacemxICanvas2D- Parameters:
value- Whether the shadow should be enabled.
-
setShadowColor
public void setShadowColor(java.lang.String value)
Description copied from interface:mxICanvas2DDefault valuemxConstants.NONE.- Specified by:
setShadowColorin interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setShadowAlpha
public void setShadowAlpha(double value)
Description copied from interface:mxICanvas2DDefault valuemxConstants.NONE.- Specified by:
setShadowAlphain interfacemxICanvas2D- Parameters:
value- Hex representation of the color ormxConstants.NONE.
-
setShadowOffset
public void setShadowOffset(double dx, double dy)Description copied from interface:mxICanvas2DDefault valuemxConstants.NONE.- Specified by:
setShadowOffsetin interfacemxICanvas2D
-
updateFont
protected void updateFont()
-
createFont
protected java.awt.Font createFont(java.lang.String family, int style, int size)Hook for subclassers to implement font caching.
-
getFontName
protected java.lang.String getFontName(java.lang.String family)
Returns a font name for the given CSS values for font-family. This implementation returns the first entry for comma-separated lists of entries.
-
updateStroke
protected void updateStroke()
-
-