mxICanvas2Dpublic class mxGraphicsCanvas2D extends java.lang.Object implements mxICanvas2D
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| Modifier and Type | Class | Description |
|---|---|---|
protected class |
mxGraphicsCanvas2D.CanvasState |
| Modifier and Type | Field | Description |
|---|---|---|
static int |
COLOR_CACHE_SIZE |
Specifies the size of the cache used to store parsed colors
|
protected java.util.LinkedHashMap<java.lang.String,java.awt.Color> |
colorCache |
Caches parsed colors.
|
protected java.awt.geom.GeneralPath |
currentPath |
Holds the current path.
|
protected java.awt.Graphics2D |
graphics |
Reference to the graphics instance for painting.
|
static double |
HTML_SCALE |
Scale for rendering HTML output.
|
static java.lang.String |
HTML_UNIT |
Unit to be used for HTML labels.
|
static int |
IMAGE_SCALING |
Specifies the image scaling quality.
|
static int |
JAVA_TEXT_WIDTH_DELTA |
Specifies the additional pixels when computing the text width for HTML labels.
|
protected int |
lastCap |
Stroke caching.
|
protected boolean |
lastDashed |
Stroke caching.
|
protected java.lang.Object |
lastDashPattern |
Stroke caching.
|
protected java.awt.Font |
lastFont |
Font caching.
|
protected java.lang.String |
lastFontFamily |
Font caching.
|
protected int |
lastFontSize |
Font caching.
|
protected int |
lastFontStyle |
Font caching.
|
protected int |
lastJoin |
Stroke caching.
|
protected float |
lastMiterLimit |
Stroke caching.
|
protected java.awt.Stroke |
lastStroke |
Stroke caching.
|
protected float |
lastStrokeWidth |
Stroke caching.
|
protected javax.swing.CellRendererPane |
rendererPane |
Optional renderer pane to be used for HTML label rendering.
|
protected java.util.Stack<mxGraphicsCanvas2D.CanvasState> |
stack |
Stack of states for save/restore.
|
protected mxGraphicsCanvas2D.CanvasState |
state |
Represents the current state of the canvas.
|
protected boolean |
textEnabled |
Specifies if text output should be rendered.
|
| Constructor | Description |
|---|---|
mxGraphicsCanvas2D(java.awt.Graphics2D g) |
Constructs a new graphics export canvas.
|
| Modifier and Type | Method | Description |
|---|---|---|
void |
begin() |
Begins a new path.
|
protected mxGraphicsCanvas2D.CanvasState |
cloneState(mxGraphicsCanvas2D.CanvasState state) |
Returns a clone of the given state.
|
void |
close() |
Closes the current path.
|
protected java.awt.Font |
createFont(java.lang.String family,
int style,
int size) |
Hook for subclassers to implement font caching.
|
protected java.lang.String |
createHtmlDocument(java.lang.String text,
java.lang.String style) |
Creates a HTML document for the given text and CSS style.
|
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.
|
protected java.awt.Graphics2D |
createImageGraphics(double x,
double y,
double w,
double h,
boolean flipH,
boolean flipV) |
Creates a graphic instance for rendering an image.
|
protected 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.
|
void |
curveTo(double x1,
double y1,
double x2,
double y2,
double x3,
double y3) |
Draws a bezier curve to the given point.
|
protected void |
drawImage(java.awt.Graphics2D graphics,
java.awt.Image image,
int x,
int y) |
|
void |
ellipse(double x,
double y,
double w,
double h) |
Next fill or stroke should draw an ellipse.
|
void |
fill() |
Fills the current path.
|
void |
fillAndStroke() |
Fills and paints the outline of the current path.
|
protected java.lang.String |
getFontName(java.lang.String family) |
Returns a font name for the given CSS values for font-family.
|
java.awt.Graphics2D |
getGraphics() |
Returns the graphics instance.
|
protected java.awt.Rectangle |
getImageBounds(java.awt.Image img,
double x,
double y,
double w,
double h,
boolean aspect) |
|
protected java.awt.Dimension |
getImageSize(java.awt.Image image) |
Returns the size for the given image.
|
protected java.awt.geom.Point2D |
getMargin(java.lang.String align,
java.lang.String valign) |
|
protected javax.swing.JLabel |
getTextRenderer() |
Hook to return the renderer for HTML formatted text.
|
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.
|
void |
image(double x,
double y,
double w,
double h,
java.lang.String src,
boolean aspect,
boolean flipH,
boolean flipV) |
Draws the given image.
|
boolean |
isTextEnabled() |
Returns true if text should be rendered.
|
void |
lineTo(double x,
double y) |
Draws a line to the given path.
|
protected java.awt.Image |
loadImage(java.lang.String src) |
Hook for image caching.
|
void |
moveTo(double x,
double y) |
Moves to the given path.
|
protected void |
paintCurrentPath(boolean filled,
boolean stroked) |
|
protected void |
paintShadow(boolean filled,
boolean stroked) |
|
protected java.awt.Color |
parseColor(java.lang.String hex) |
Helper method that uses
mxUtils.parseColor(String). |
protected java.awt.Color |
parseColor(java.lang.String hex,
double alpha) |
Helper method that uses
mxUtils.parseColor(String). |
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.
|
void |
quadTo(double x1,
double y1,
double x2,
double y2) |
Draws a quadratic curve to the given point.
|
void |
rect(double x,
double y,
double w,
double h) |
Next fill or stroke should draw a rectangle.
|
void |
restore() |
Restores the last canvas state.
|
void |
rotate(double theta,
boolean flipH,
boolean flipV,
double cx,
double cy) |
Rotates the canvas by the given angle around the given center.
|
void |
roundrect(double x,
double y,
double w,
double h,
double dx,
double dy) |
Implements a rounded rectangle using a path.
|
void |
save() |
Saves the current canvas state.
|
void |
scale(double value) |
Uniformaly scales the canvas by the given amount.
|
protected java.awt.Image |
scaleImage(java.awt.Image img,
int w,
int h) |
Uses
IMAGE_SCALING to scale the given image. |
void |
setAlpha(double value) |
Default value 1.
|
void |
setDashed(boolean value) |
Sets the dashed state.
|
void |
setDashed(boolean value,
boolean fixDash) |
Sets the dashed state.
|
void |
setDashPattern(java.lang.String value) |
Sets the dash pattern.
|
void |
setFillAlpha(double value) |
Default value 1.
|
void |
setFillColor(java.lang.String value) |
Default value
mxConstants.NONE. |
void |
setFontBackgroundColor(java.lang.String value) |
Default value "#000000".
|
void |
setFontBorderColor(java.lang.String value) |
Default value "#000000".
|
void |
setFontColor(java.lang.String value) |
Default value "#000000".
|
void |
setFontFamily(java.lang.String value) |
Default value
mxConstants.DEFAULT_FONTFAMILY. |
void |
setFontSize(double value) |
Default value
mxConstants.DEFAULT_FONTSIZE. |
void |
setFontStyle(int value) |
Default value 0.
|
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) |
Prepares the canvas to draw a gradient.
|
void |
setGraphics(java.awt.Graphics2D value) |
Sets the graphics instance.
|
void |
setLineCap(java.lang.String value) |
Sets the linecap.
|
void |
setLineJoin(java.lang.String value) |
Sets the linejoin.
|
void |
setMiterLimit(double value) |
Sets the miterlimit.
|
void |
setShadow(boolean value) |
Enables or disables the painting of shadows.
|
void |
setShadowAlpha(double value) |
Default value
mxConstants.NONE. |
void |
setShadowColor(java.lang.String value) |
Default value
mxConstants.NONE. |
void |
setShadowOffset(double dx,
double dy) |
Default value
mxConstants.NONE. |
void |
setStrokeAlpha(double value) |
Default value 1.
|
void |
setStrokeColor(java.lang.String value) |
Caches color conversion as it is expensive.
|
void |
setStrokeWidth(double value) |
Sets the stroke width.
|
void |
setTextEnabled(boolean value) |
Disables or enables text rendering.
|
void |
stroke() |
Paints the outline of the current path.
|
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.
|
void |
translate(double dx,
double dy) |
Translates the canvas by the given amount.
|
protected void |
updateFont() |
|
protected void |
updateStroke() |
public static int IMAGE_SCALING
scaleImage(Image, int, int)public static int JAVA_TEXT_WIDTH_DELTA
public static double HTML_SCALE
public static java.lang.String HTML_UNIT
public static int COLOR_CACHE_SIZE
protected java.awt.Graphics2D graphics
protected boolean textEnabled
protected transient mxGraphicsCanvas2D.CanvasState state
protected transient java.util.Stack<mxGraphicsCanvas2D.CanvasState> stack
protected transient java.awt.geom.GeneralPath currentPath
protected javax.swing.CellRendererPane rendererPane
protected transient java.awt.Font lastFont
protected transient int lastFontStyle
protected transient int lastFontSize
protected transient java.lang.String lastFontFamily
protected transient java.awt.Stroke lastStroke
protected transient float lastStrokeWidth
protected transient int lastCap
protected transient int lastJoin
protected transient float lastMiterLimit
protected transient boolean lastDashed
protected transient java.lang.Object lastDashPattern
protected transient java.util.LinkedHashMap<java.lang.String,java.awt.Color> colorCache
public mxGraphicsCanvas2D(java.awt.Graphics2D g)
public void setGraphics(java.awt.Graphics2D value)
public java.awt.Graphics2D getGraphics()
public boolean isTextEnabled()
public void setTextEnabled(boolean value)
public void save()
save in interface mxICanvas2Dpublic void restore()
restore in interface mxICanvas2Dprotected mxGraphicsCanvas2D.CanvasState cloneState(mxGraphicsCanvas2D.CanvasState state)
public void scale(double value)
mxICanvas2Dscale in interface mxICanvas2Dvalue - The new scale value.public void translate(double dx,
double dy)
mxICanvas2Dtranslate in interface mxICanvas2Ddx - X-coordinate of the translation.dy - Y-coordinate of the translation.public void rotate(double theta,
boolean flipH,
boolean flipV,
double cx,
double cy)
mxICanvas2Drotate in interface mxICanvas2Dtheta - 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.public void setStrokeWidth(double value)
mxICanvas2DsetStrokeWidth in interface mxICanvas2Dvalue - Width of the stroke. The value should be multiplied by the
current scale.public void setStrokeColor(java.lang.String value)
setStrokeColor in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.public void setDashed(boolean value)
mxICanvas2DsetDashed in interface mxICanvas2Dvalue - Boolean representing the dashed state.public void setDashed(boolean value,
boolean fixDash)
mxICanvas2DsetDashed in interface mxICanvas2Dvalue - Boolean representing the dashed state.public void setDashPattern(java.lang.String value)
mxICanvas2DsetDashPattern in interface mxICanvas2Dvalue - Space separated list of floats representing the dash
pattern. The value should be multiplied by the current scale.public void setLineCap(java.lang.String value)
mxICanvas2DsetLineCap in interface mxICanvas2Dvalue - "flat", "square" or "round".public void setLineJoin(java.lang.String value)
mxICanvas2DsetLineJoin in interface mxICanvas2Dvalue - "miter", "round" or "bevel".public void setMiterLimit(double value)
mxICanvas2DsetMiterLimit in interface mxICanvas2Dpublic void setFontSize(double value)
mxICanvas2DmxConstants.DEFAULT_FONTSIZE.setFontSize in interface mxICanvas2Dpublic void setFontColor(java.lang.String value)
mxICanvas2DsetFontColor in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.public void setFontBackgroundColor(java.lang.String value)
mxICanvas2DsetFontBackgroundColor in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.public void setFontBorderColor(java.lang.String value)
mxICanvas2DsetFontBorderColor in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.public void setFontFamily(java.lang.String value)
mxICanvas2DmxConstants.DEFAULT_FONTFAMILY.setFontFamily in interface mxICanvas2Dpublic void setFontStyle(int value)
mxICanvas2DmxConstants.STYLE_FONTSTYLE.setFontStyle in interface mxICanvas2Dpublic void setAlpha(double value)
mxICanvas2DsetAlpha in interface mxICanvas2Dpublic void setFillAlpha(double value)
mxICanvas2DsetFillAlpha in interface mxICanvas2Dpublic void setStrokeAlpha(double value)
mxICanvas2DsetStrokeAlpha in interface mxICanvas2Dpublic void setFillColor(java.lang.String value)
mxICanvas2DmxConstants.NONE.setFillColor in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.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)
mxICanvas2DsetGradient in interface mxICanvas2Ddirection - Direction may be null. Use default value
mxConstants.DIRECTION_SOUTH.protected java.awt.Color parseColor(java.lang.String hex)
mxUtils.parseColor(String).protected java.awt.Color parseColor(java.lang.String hex,
double alpha)
mxUtils.parseColor(String).public void rect(double x,
double y,
double w,
double h)
mxICanvas2Drect in interface mxICanvas2Dpublic void roundrect(double x,
double y,
double w,
double h,
double dx,
double dy)
roundrect in interface mxICanvas2Dpublic void ellipse(double x,
double y,
double w,
double h)
mxICanvas2Dellipse in interface mxICanvas2Dpublic void image(double x,
double y,
double w,
double h,
java.lang.String src,
boolean aspect,
boolean flipH,
boolean flipV)
mxICanvas2Dimage in interface mxICanvas2Dprotected void drawImage(java.awt.Graphics2D graphics,
java.awt.Image image,
int x,
int y)
protected java.awt.Image loadImage(java.lang.String src)
protected final java.awt.Rectangle getImageBounds(java.awt.Image img,
double x,
double y,
double w,
double h,
boolean aspect)
protected java.awt.Dimension getImageSize(java.awt.Image image)
protected java.awt.Image scaleImage(java.awt.Image img,
int w,
int h)
IMAGE_SCALING to scale the given image.protected final java.awt.Graphics2D createImageGraphics(double x,
double y,
double w,
double h,
boolean flipH,
boolean flipV)
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)
protected java.lang.String createHtmlDocument(java.lang.String text,
java.lang.String style)
protected javax.swing.JLabel getTextRenderer()
protected java.awt.geom.Point2D getMargin(java.lang.String align,
java.lang.String valign)
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)
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)
text in interface mxICanvas2Dpublic 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)
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)
public void begin()
mxICanvas2Dbegin in interface mxICanvas2Dpublic void moveTo(double x,
double y)
mxICanvas2DmoveTo in interface mxICanvas2Dpublic void lineTo(double x,
double y)
mxICanvas2DlineTo in interface mxICanvas2Dpublic void quadTo(double x1,
double y1,
double x2,
double y2)
mxICanvas2DquadTo in interface mxICanvas2Dpublic void curveTo(double x1,
double y1,
double x2,
double y2,
double x3,
double y3)
mxICanvas2DcurveTo in interface mxICanvas2Dpublic void close()
close in interface mxICanvas2Dpublic void stroke()
mxICanvas2Dstroke in interface mxICanvas2Dpublic void fill()
mxICanvas2Dfill in interface mxICanvas2Dpublic void fillAndStroke()
mxICanvas2DfillAndStroke in interface mxICanvas2Dprotected void paintCurrentPath(boolean filled,
boolean stroked)
protected void paintShadow(boolean filled,
boolean stroked)
public void setShadow(boolean value)
mxICanvas2DsetShadow in interface mxICanvas2Dvalue - Whether the shadow should be enabled.public void setShadowColor(java.lang.String value)
mxICanvas2DmxConstants.NONE.setShadowColor in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.public void setShadowAlpha(double value)
mxICanvas2DmxConstants.NONE.setShadowAlpha in interface mxICanvas2Dvalue - Hex representation of the color or mxConstants.NONE.public void setShadowOffset(double dx,
double dy)
mxICanvas2DmxConstants.NONE.setShadowOffset in interface mxICanvas2Dprotected void updateFont()
protected java.awt.Font createFont(java.lang.String family,
int style,
int size)
protected java.lang.String getFontName(java.lang.String family)
protected void updateStroke()
Copyright (c) 2010-2017 Gaudenz Alder, JGraph Ltd. All rights reserved.