Class WSVGGraphics2D
- java.lang.Object
-
- java.awt.Graphics
-
- java.awt.Graphics2D
-
- kcl.waterloo.common.deploy.AbstractDeployableGraphics2D
-
- kcl.waterloo.deploy.svg.WSVGGraphics2D
-
public class WSVGGraphics2D extends AbstractDeployableGraphics2D
WSVGGraphics2D - a Graphics2D subclass that generates SVG.
WSVGGraphics2D is simply a wrapper class for Apache Batik SVGGraphics2D instances. Apache Batik does all the real work. WSVGGraphics2D justs adds a few groups to the DOM to facilitate interaction with the charts using JavaScript, e.g. via d3d.js - and adds HTML file writing support together with support for translating the SVG to HTML5 canvas commands using canvg.js.
For info about canvg.js and known issues see the README.txt file in package
kcl.waterloo.deploy.canvgUsage:
WSVGGraphics2D instances are used in AWT/Swing component paint methods in the same way as any other Graphics2D class object. When painting is complete, a Processing script can be accessed by calling the
getCode()method on the instance which returns the SVG as a String. Alternatively, call one of thewrite(...)methods to save the code to file. These methods optionally support the creation of HTML files, together with CSS style sheets, to display the generated script in an HTML5 browser canvas via canvg.js [see https://code.google.com/p/canvg/].The methods are not thread-safe. All painting should be done on the EDT.
To create a PDEGraphics2D instance, pass the constructor an instance of a standard Graphics2D object
PDEGraphics2D g= new PDEGraphics2D(Graphics2D g)
Write methods:
The
writemethods create files following a convention that is intended to mimic those used in PDEGraphics2D.The convention is as follows
If you write a file named e.g. "script_name.svg", the file will be created in a folder named "script_name". This folder will be created if it does not exist. Thus a file specified as "/Users/ML/Documents/script_name", would create "/Users/ML/Documents/script_name/script_name.svg" - the .svg extension will be added if not specified.
If the the paint methods called with the PDEGraphics2D instance include calls to the
drawImagemethods, the required images will be added to a "/script_name/data/" folder as PNG images. Gradient paints, including MultipleGradientPaints, are also supported through drawing images and require a /data folder.The
writemethods also support the creation of HTML files. If thehtmlFlagon input istrue, the following files can be created:script_name.html - a simple file that shows only the graphics
index.html - a styled file that includes titles etc
WSVGGraphics.css - styling sheet for index.html
index.xml - an xml file describing the contents of the /data folder
htppd.py - a Python 2.7 script that displays index.html in the system default brpwser using a local server. Use this if the default browser's security settings prevent JavaScript accessing the local file system directly.
See the write{)} methods for full details of the options available.WSVGGraphics2D has been tested with the following browsers
Adding code and comments to the scripts:
Several methods allow the generated script to be modified on-the-fly when users have access to the paint methods of the components being painted. These methods are defined in AbstractDeployableGraphics2D, the WSVGGraphics superclass.- public void setPaintObject(Object referenceObject) Adds an SVG group to DOM and adds subsequent elements to that group.
- public void addComment(String comment) Adds a comment to the script at the current writing location. Format markers will be added automatically.
Titles etc
- public void setTitle(String title) Gives the script a title. This will added as the name of the displayed Window or displayed in HTML files. It will also be used to set the title tag in HTML.
- public void setDescription(String description) The description will be displayed with index.html
- public void setKeyWords(ArrayList
keyWords)
Adds the specified strings as keywords to HTML documents.
- public void addKeyWord(String word) Adds the keyword to the current list for HTML files.
WSVGGraphics can only render graphics normally rendered through Java2D graphics calls. When painting using a proprietary, platform-specific, Java look-and-feel, some features may be painted using native graphics calls and will not appear in the generated scripts. For example, JFrames will not be decorated with close/maximize buttons etc.
In addition, some components may not render fully perhaps because the componentUIs are cloning the wrapped Graphics2D instance instead of the WSVGGraphics instance or maybe generating a new instance with a call to Component.getGraphics(). This seems to affect mainly the Mac Aqua L&F. Some fixes/workarounds are included for specific L&Fs and AWT/Swing components where the cause of the problem has been identified.
-
-
Constructor Summary
Constructors Constructor and Description WSVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D g)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddCode(java.lang.String code)Inserts user-specified code into the coded output at the current location.voidaddComment(java.lang.String comment)Sets a string that will be added to the code output as a comment.voidappend(java.awt.Component c)voidappend(java.awt.Component c, int elapsedTime)voidappendCode(java.lang.String code)Adds user-specified code to the end of the generated code.java.lang.Objectclone()booleancopyImage(java.awt.Image img, int dx, int dy, int sx, int sy, int width, int height, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Implemented in some Graphics2D subclasses e.g.java.awt.Graphicscreate()java.awt.Graphicscreate(int x, int y, int width, int height)static voiddeploy(java.io.File f, java.lang.String svgCode, java.awt.Dimension dim, boolean inline, java.lang.String jsLoc, java.lang.String cssLoc, boolean httpdFlag)static voiddeploy(java.io.File f, java.lang.String svgCode, java.awt.Dimension dim, boolean inline, java.lang.String jsLoc, java.lang.String cssLoc, boolean httpdFlag, java.util.ArrayList<java.lang.String> keyWords, java.lang.String title, java.lang.String description)java.lang.StringgetDescription()java.lang.StringgetTitle()voidprependCode(java.lang.String code)Inserts user-specified code at the start of the generated code, but after any automatically generated declarations (which are therefore potentially available for use in that code).voidsetDescription(java.lang.String description)voidsetPaintObject(java.lang.Object referenceObject)Call this routine in the painting mechanism of an AWT/Swing hierarchy.voidsetTitle(java.lang.String title)-
Methods inherited from class kcl.waterloo.common.deploy.AbstractDeployableGraphics2D
addRenderingHints, clearRect, clip, clipRect, copyArea, dispose, draw, draw3DRect, drawArc, drawBytes, drawGlyphVector, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawImage, drawLine, drawOval, drawPolygon, drawPolyline, drawRect, drawRenderableImage, drawRenderedImage, drawRoundRect, drawString, drawString, drawString, drawString, fill, fill3DRect, fillArc, fillOval, fillPolygon, fillRect, fillRoundRect, getActionDelay, getBackground, getClip, getClipBounds, getCode, getColor, getComposite, getDeviceConfiguration, getFont, getFontMetrics, getFontRenderContext, getPaint, getRenderingHint, getRenderingHints, getStroke, getTransform, hit, isAlsoPaintToSource, rotate, rotate, scale, setActionDelay, setAlsoPaintToSource, setBackground, setClip, setClip, setColor, setComposite, setFont, setPaint, setPaintMode, setRenderingHint, setRenderingHints, setStroke, setTransform, setXORMode, shear, transform, translate, translate, write, write
-
-
-
-
Method Detail
-
append
public void append(java.awt.Component c, int elapsedTime)- Specified by:
appendin classAbstractDeployableGraphics2D
-
append
public void append(java.awt.Component c)
- Specified by:
appendin classAbstractDeployableGraphics2D
-
setPaintObject
public void setPaintObject(java.lang.Object referenceObject)
Description copied from class:AbstractDeployableGraphics2DCall this routine in the painting mechanism of an AWT/Swing hierarchy. How a concrete class responds is an implementation detail for the subclass. Typically, this will simply be used to add a comment to the generated code in a custom component e.g.public void paintComponent(Graphics g){
if (g instanceof AbstractDeployableGraphics2D) {
((AbstractDeployableGraphics2D)g).setPaintObject(this);
}
}- Specified by:
setPaintObjectin classAbstractDeployableGraphics2D- Parameters:
referenceObject- the paintObject to set
-
addComment
public void addComment(java.lang.String comment)
Description copied from class:AbstractDeployableGraphics2DSets a string that will be added to the code output as a comment. Do not added comment markers - these should be added by any concrete implementations automatically and will be specific to the code language being used.- Specified by:
addCommentin classAbstractDeployableGraphics2D- Parameters:
comment- the comment to set as a String
-
prependCode
public void prependCode(java.lang.String code)
Description copied from class:AbstractDeployableGraphics2DInserts user-specified code at the start of the generated code, but after any automatically generated declarations (which are therefore potentially available for use in that code).- Specified by:
prependCodein classAbstractDeployableGraphics2D- Parameters:
code- fully formatted code string
-
addCode
public void addCode(java.lang.String code)
Description copied from class:AbstractDeployableGraphics2DInserts user-specified code into the coded output at the current location.- Specified by:
addCodein classAbstractDeployableGraphics2D- Parameters:
code- fully formatted code string
-
appendCode
public void appendCode(java.lang.String code)
Description copied from class:AbstractDeployableGraphics2DAdds user-specified code to the end of the generated code.- Specified by:
appendCodein classAbstractDeployableGraphics2D- Parameters:
code- the code to append
-
copyImage
public boolean copyImage(java.awt.Image img, int dx, int dy, int sx, int sy, int width, int height, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)Description copied from class:AbstractDeployableGraphics2DImplemented in some Graphics2D subclasses e.g. com.sun.java.Graphics2D. This is not part of the java.awt.Graphics2D API.- Specified by:
copyImagein classAbstractDeployableGraphics2D- Returns:
- true or false
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
create
public java.awt.Graphics create()
- Specified by:
createin classjava.awt.Graphics
-
create
public java.awt.Graphics create(int x, int y, int width, int height)- Overrides:
createin classjava.awt.Graphics
-
deploy
public static void deploy(java.io.File f, java.lang.String svgCode, java.awt.Dimension dim, boolean inline, java.lang.String jsLoc, java.lang.String cssLoc, boolean httpdFlag) throws java.io.IOException- Throws:
java.io.IOException
-
deploy
public static void deploy(java.io.File f, java.lang.String svgCode, java.awt.Dimension dim, boolean inline, java.lang.String jsLoc, java.lang.String cssLoc, boolean httpdFlag, java.util.ArrayList<java.lang.String> keyWords, java.lang.String title, java.lang.String description) throws java.io.IOException- Parameters:
f- SVG File to write to.svgCode- SVG code as Stringdim- the Dimensions of the component being paintedinline- if true, SVG code will be inlined in the HTML filejsLoc- Location of the canvg.js filecssLoc- Location of the CSS filehttpdFlag- If true, generated a Python script- Throws:
java.io.IOException
-
getTitle
public java.lang.String getTitle()
- Returns:
- the title
-
setTitle
public void setTitle(java.lang.String title)
- Parameters:
title- the title to set
-
getDescription
public java.lang.String getDescription()
- Returns:
- the description
-
setDescription
public void setDescription(java.lang.String description)
- Parameters:
description- the description to set
-
-
DMelt 3.0 © DataMelt by jWork.ORG