mxGraph 2.4.0.0


com.mxgraph.canvas
Class mxGraphicsCanvas2D

java.lang.Object
  extended by com.mxgraph.canvas.mxGraphicsCanvas2D
All Implemented Interfaces:
mxICanvas2D

public class mxGraphicsCanvas2D
extends 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
protected  class mxGraphicsCanvas2D.CanvasState
           
 
Field Summary
static int COLOR_CACHE_SIZE
          Specifies the size of the cache used to store parsed colors
protected  LinkedHashMap<String,Color> colorCache
          Caches parsed colors.
protected  GeneralPath currentPath
          Holds the current path.
protected  Graphics2D graphics
          Reference to the graphics instance for painting.
static int IMAGE_SCALING
          Specifies the image scaling quality.
protected  int lastCap
          Stroke caching.
protected  boolean lastDashed
          Stroke caching.
protected  Object lastDashPattern
          Stroke caching.
protected  Font lastFont
          Font caching.
protected  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  Stroke lastStroke
          Stroke caching.
protected  float lastStrokeWidth
          Stroke caching.
protected  CellRendererPane rendererPane
          Optional renderer pane to be used for HTML label rendering.
protected  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 Summary
mxGraphicsCanvas2D(Graphics2D g)
          Constructs a new graphics export canvas.
 
Method Summary
 void begin()
          Begins a new path.
protected  mxGraphicsCanvas2D.CanvasState cloneState(mxGraphicsCanvas2D.CanvasState state)
          Returns a clone of thec given state.
 void close()
          Closes the current path.
protected  Font createFont(String family, int style, int size)
          Hook for subclassers to implement font caching.
protected  String createHtmlDocument(String text, String align, String valign, int w, int h, boolean wrap, String overflow, boolean clip)
          Creates a HTML document around the given markup.
protected  Graphics2D createImageGraphics(double x, double y, double w, double h, boolean flipH, boolean flipV)
          Creates a graphic instance for rendering an image.
protected  Graphics2D createTextGraphics(double x, double y, double w, double h, double rotation, boolean clip, String align, 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(Graphics2D graphics, 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  String getFontName(String family)
          Returns a font name for the given CSS values for font-family.
 Graphics2D getGraphics()
          Returns the graphics instance.
protected  Rectangle getImageBounds(Image img, double x, double y, double w, double h, boolean aspect)
           
protected  Dimension getImageSize(Image image)
          Returns the size for the given image.
protected  Point2D getMargin(String align, String valign)
           
protected  JLabel getTextRenderer()
          Hook to return the renderer for HTML formatted text.
protected  void htmlText(double x, double y, double w, double h, String str, String align, String valign, boolean wrap, String format, String overflow, boolean clip, double rotation)
          Draws the given HTML text.
 void image(double x, double y, double w, double h, 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  Image loadImage(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  Color parseColor(String hex)
          Helper method that uses mxUtils.parseColor(String).
 void plainText(double x, double y, double w, double h, String str, String align, String valign, boolean wrap, String format, 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  Image scaleImage(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 setDashPattern(String value)
          Sets the dash pattern.
 void setFillColor(String value)
          Default value mxConstants.NONE.
 void setFontBackgroundColor(String value)
          Default value "#000000".
 void setFontBorderColor(String value)
          Default value "#000000".
 void setFontColor(String value)
          Default value "#000000".
 void setFontFamily(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(String color1, String color2, double x, double y, double w, double h, String direction, double alpha1, double alpha2)
          Prepares the canvas to draw a gradient.
 void setGraphics(Graphics2D value)
          Sets the graphics instance.
 void setLineCap(String value)
          Sets the linecap.
 void setLineJoin(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(String value)
          Default value mxConstants.NONE.
 void setShadowOffset(double dx, double dy)
          Default value mxConstants.NONE.
 void setStrokeColor(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, String str, String align, String valign, boolean wrap, String format, String overflow, boolean clip, double rotation)
          Draws the given text.
 void translate(double dx, double dy)
          Translates the canvas by the given amount.
protected  void updateFont()
           
protected  void updateStroke()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IMAGE_SCALING

public static int IMAGE_SCALING
Specifies the image scaling quality. Default is Image.SCALE_SMOOTH. See scaleImage(Image, int, int)


COLOR_CACHE_SIZE

public static int COLOR_CACHE_SIZE
Specifies the size of the cache used to store parsed colors


graphics

protected 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 Stack<mxGraphicsCanvas2D.CanvasState> stack
Stack of states for save/restore.


currentPath

protected transient GeneralPath currentPath
Holds the current path.


rendererPane

protected CellRendererPane rendererPane
Optional renderer pane to be used for HTML label rendering.


lastFont

protected transient Font lastFont
Font caching.


lastFontStyle

protected transient int lastFontStyle
Font caching.


lastFontSize

protected transient int lastFontSize
Font caching.


lastFontFamily

protected transient String lastFontFamily
Font caching.


lastStroke

protected transient 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 Object lastDashPattern
Stroke caching.


colorCache

protected transient LinkedHashMap<String,Color> colorCache
Caches parsed colors.

Constructor Detail

mxGraphicsCanvas2D

public mxGraphicsCanvas2D(Graphics2D g)
Constructs a new graphics export canvas.

Method Detail

setGraphics

public void setGraphics(Graphics2D value)
Sets the graphics instance.


getGraphics

public 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:
save in interface mxICanvas2D

restore

public void restore()
Restores the last canvas state.

Specified by:
restore in interface mxICanvas2D

cloneState

protected mxGraphicsCanvas2D.CanvasState cloneState(mxGraphicsCanvas2D.CanvasState state)
Returns a clone of thec given state.


scale

public void scale(double value)
Description copied from interface: mxICanvas2D
Uniformaly scales the canvas by the given amount.

Specified by:
scale in interface mxICanvas2D
Parameters:
value - The new scale value.

translate

public void translate(double dx,
                      double dy)
Description copied from interface: mxICanvas2D
Translates the canvas by the given amount.

Specified by:
translate in interface mxICanvas2D
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: mxICanvas2D
Rotates the canvas by the given angle around the given center. This method may add rendering overhead and should be used with care.

Specified by:
rotate in interface mxICanvas2D
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: mxICanvas2D
Sets the stroke width. This should default to 1 if unset.

Specified by:
setStrokeWidth in interface mxICanvas2D
Parameters:
value - Width of the stroke. The value should be multiplied by the current scale.

setStrokeColor

public void setStrokeColor(String value)
Caches color conversion as it is expensive.

Specified by:
setStrokeColor in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setDashed

public void setDashed(boolean value)
Description copied from interface: mxICanvas2D
Sets the dashed state. This should default to false if unset.

Specified by:
setDashed in interface mxICanvas2D
Parameters:
value - Boolean representing the dashed state.

setDashPattern

public void setDashPattern(String value)
Description copied from interface: mxICanvas2D
Sets the dash pattern. This should default to "3 3" if unset.

Specified by:
setDashPattern in interface mxICanvas2D
Parameters:
value - Space separated list of floats representing the dash pattern. The value should be multiplied by the current scale.

setLineCap

public void setLineCap(String value)
Description copied from interface: mxICanvas2D
Sets the linecap. This should default to "flat" if unset.

Specified by:
setLineCap in interface mxICanvas2D
Parameters:
value - "flat", "square" or "round".

setLineJoin

public void setLineJoin(String value)
Description copied from interface: mxICanvas2D
Sets the linejoin. This should default to "miter" if unset.

Specified by:
setLineJoin in interface mxICanvas2D
Parameters:
value - "miter", "round" or "bevel".

setMiterLimit

public void setMiterLimit(double value)
Description copied from interface: mxICanvas2D
Sets the miterlimit. This should default to 10 if unset.

Specified by:
setMiterLimit in interface mxICanvas2D

setFontSize

public void setFontSize(double value)
Description copied from interface: mxICanvas2D
Default value mxConstants.DEFAULT_FONTSIZE.

Specified by:
setFontSize in interface mxICanvas2D

setFontColor

public void setFontColor(String value)
Description copied from interface: mxICanvas2D
Default value "#000000".

Specified by:
setFontColor in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setFontBackgroundColor

public void setFontBackgroundColor(String value)
Description copied from interface: mxICanvas2D
Default value "#000000".

Specified by:
setFontBackgroundColor in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setFontBorderColor

public void setFontBorderColor(String value)
Description copied from interface: mxICanvas2D
Default value "#000000".

Specified by:
setFontBorderColor in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setFontFamily

public void setFontFamily(String value)
Description copied from interface: mxICanvas2D
Default value mxConstants.DEFAULT_FONTFAMILY.

Specified by:
setFontFamily in interface mxICanvas2D

setFontStyle

public void setFontStyle(int value)
Description copied from interface: mxICanvas2D
Default value 0. See mxConstants.STYLE_FONTSTYLE.

Specified by:
setFontStyle in interface mxICanvas2D

setAlpha

public void setAlpha(double value)
Description copied from interface: mxICanvas2D
Default value 1. This method may add rendering overhead and should be used with care.

Specified by:
setAlpha in interface mxICanvas2D

setFillColor

public void setFillColor(String value)
Description copied from interface: mxICanvas2D
Default value mxConstants.NONE.

Specified by:
setFillColor in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setGradient

public void setGradient(String color1,
                        String color2,
                        double x,
                        double y,
                        double w,
                        double h,
                        String direction,
                        double alpha1,
                        double alpha2)
Description copied from interface: mxICanvas2D
Prepares the canvas to draw a gradient.

Specified by:
setGradient in interface mxICanvas2D
direction - Direction may be null. Use default value mxConstants.DIRECTION_SOUTH.

parseColor

protected Color parseColor(String hex)
Helper method that uses mxUtils.parseColor(String).


rect

public void rect(double x,
                 double y,
                 double w,
                 double h)
Description copied from interface: mxICanvas2D
Next fill or stroke should draw a rectangle.

Specified by:
rect in interface mxICanvas2D

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:
roundrect in interface mxICanvas2D

ellipse

public void ellipse(double x,
                    double y,
                    double w,
                    double h)
Description copied from interface: mxICanvas2D
Next fill or stroke should draw an ellipse.

Specified by:
ellipse in interface mxICanvas2D

image

public void image(double x,
                  double y,
                  double w,
                  double h,
                  String src,
                  boolean aspect,
                  boolean flipH,
                  boolean flipV)
Description copied from interface: mxICanvas2D
Draws the given image.

Specified by:
image in interface mxICanvas2D

drawImage

protected void drawImage(Graphics2D graphics,
                         Image image,
                         int x,
                         int y)

loadImage

protected Image loadImage(String src)
Hook for image caching.


getImageBounds

protected final Rectangle getImageBounds(Image img,
                                         double x,
                                         double y,
                                         double w,
                                         double h,
                                         boolean aspect)

getImageSize

protected Dimension getImageSize(Image image)
Returns the size for the given image.


scaleImage

protected Image scaleImage(Image img,
                           int w,
                           int h)
Uses IMAGE_SCALING to scale the given image.


createImageGraphics

protected final Graphics2D createImageGraphics(double x,
                                               double y,
                                               double w,
                                               double h,
                                               boolean flipH,
                                               boolean flipV)
Creates a graphic instance for rendering an image.


createHtmlDocument

protected String createHtmlDocument(String text,
                                    String align,
                                    String valign,
                                    int w,
                                    int h,
                                    boolean wrap,
                                    String overflow,
                                    boolean clip)
Creates a HTML document around the given markup.


getTextRenderer

protected JLabel getTextRenderer()
Hook to return the renderer for HTML formatted text. This implementation returns the shared instance of mxLighweightLabel.


getMargin

protected Point2D getMargin(String align,
                            String valign)

htmlText

protected void htmlText(double x,
                        double y,
                        double w,
                        double h,
                        String str,
                        String align,
                        String valign,
                        boolean wrap,
                        String format,
                        String overflow,
                        boolean clip,
                        double rotation)
Draws the given HTML text.


text

public void text(double x,
                 double y,
                 double w,
                 double h,
                 String str,
                 String align,
                 String valign,
                 boolean wrap,
                 String format,
                 String overflow,
                 boolean clip,
                 double rotation)
Draws the given text.

Specified by:
text in interface mxICanvas2D

plainText

public void plainText(double x,
                      double y,
                      double w,
                      double h,
                      String str,
                      String align,
                      String valign,
                      boolean wrap,
                      String format,
                      String overflow,
                      boolean clip,
                      double rotation)
Draws the given text.


createTextGraphics

protected final Graphics2D createTextGraphics(double x,
                                              double y,
                                              double w,
                                              double h,
                                              double rotation,
                                              boolean clip,
                                              String align,
                                              String valign)
Returns a new graphics instance with the correct color and font for text rendering.


begin

public void begin()
Description copied from interface: mxICanvas2D
Begins a new path.

Specified by:
begin in interface mxICanvas2D

moveTo

public void moveTo(double x,
                   double y)
Description copied from interface: mxICanvas2D
Moves to the given path.

Specified by:
moveTo in interface mxICanvas2D

lineTo

public void lineTo(double x,
                   double y)
Description copied from interface: mxICanvas2D
Draws a line to the given path.

Specified by:
lineTo in interface mxICanvas2D

quadTo

public void quadTo(double x1,
                   double y1,
                   double x2,
                   double y2)
Description copied from interface: mxICanvas2D
Draws a quadratic curve to the given point.

Specified by:
quadTo in interface mxICanvas2D

curveTo

public void curveTo(double x1,
                    double y1,
                    double x2,
                    double y2,
                    double x3,
                    double y3)
Description copied from interface: mxICanvas2D
Draws a bezier curve to the given point.

Specified by:
curveTo in interface mxICanvas2D

close

public void close()
Closes the current path.

Specified by:
close in interface mxICanvas2D

stroke

public void stroke()
Description copied from interface: mxICanvas2D
Paints the outline of the current path.

Specified by:
stroke in interface mxICanvas2D

fill

public void fill()
Description copied from interface: mxICanvas2D
Fills the current path.

Specified by:
fill in interface mxICanvas2D

fillAndStroke

public void fillAndStroke()
Description copied from interface: mxICanvas2D
Fills and paints the outline of the current path.

Specified by:
fillAndStroke in interface mxICanvas2D

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: mxICanvas2D
Enables or disables the painting of shadows.

Specified by:
setShadow in interface mxICanvas2D
Parameters:
value - Whether the shadow should be enabled.

setShadowColor

public void setShadowColor(String value)
Description copied from interface: mxICanvas2D
Default value mxConstants.NONE.

Specified by:
setShadowColor in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setShadowAlpha

public void setShadowAlpha(double value)
Description copied from interface: mxICanvas2D
Default value mxConstants.NONE.

Specified by:
setShadowAlpha in interface mxICanvas2D
Parameters:
value - Hex representation of the color or mxConstants.NONE.

setShadowOffset

public void setShadowOffset(double dx,
                            double dy)
Description copied from interface: mxICanvas2D
Default value mxConstants.NONE.

Specified by:
setShadowOffset in interface mxICanvas2D

updateFont

protected void updateFont()

createFont

protected Font createFont(String family,
                          int style,
                          int size)
Hook for subclassers to implement font caching.


getFontName

protected String getFontName(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()

mxGraph 2.4.0.0


Copyright (c) 2010 Gaudenz Alder, David Benson. All rights reserved.