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.canvg
Usage:
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
the write(...)
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 write
methods 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 drawImage
methods, 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 write
methods also support the creation of HTML files. If the
htmlFlag
on input is true
, 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.Titles etc
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.
AbstractDeployableGraphics2D.GJPathSegmentInfo
latency, parentGraphics, pictures, startTime
Constructor and Description |
---|
WSVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D g) |
Modifier and Type | Method and Description |
---|---|
void |
addCode(java.lang.String code)
Inserts user-specified code into the coded output at the current
location.
|
void |
addComment(java.lang.String comment)
Sets a string that will be added to the code output as a comment.
|
void |
append(java.awt.Component c) |
void |
append(java.awt.Component c,
int elapsedTime) |
void |
appendCode(java.lang.String code)
Adds user-specified code to the end of the generated code.
|
java.lang.Object |
clone() |
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)
Implemented in some Graphics2D subclasses e.g.
|
java.awt.Graphics |
create() |
java.awt.Graphics |
create(int x,
int y,
int width,
int height) |
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) |
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) |
java.lang.String |
getDescription() |
java.lang.String |
getTitle() |
void |
prependCode(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).
|
void |
setDescription(java.lang.String description) |
void |
setPaintObject(java.lang.Object referenceObject)
Call this routine in the painting mechanism of an AWT/Swing hierarchy.
|
void |
setTitle(java.lang.String title) |
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
public void append(java.awt.Component c, int elapsedTime)
append
in class AbstractDeployableGraphics2D
public void append(java.awt.Component c)
append
in class AbstractDeployableGraphics2D
public void setPaintObject(java.lang.Object referenceObject)
AbstractDeployableGraphics2D
public void paintComponent(Graphics g){
if (g instanceof AbstractDeployableGraphics2D) {
((AbstractDeployableGraphics2D)g).setPaintObject(this);
}
}
setPaintObject
in class AbstractDeployableGraphics2D
referenceObject
- the paintObject to setpublic void addComment(java.lang.String comment)
AbstractDeployableGraphics2D
addComment
in class AbstractDeployableGraphics2D
comment
- the comment to set as a Stringpublic void prependCode(java.lang.String code)
AbstractDeployableGraphics2D
prependCode
in class AbstractDeployableGraphics2D
code
- fully formatted code stringpublic void addCode(java.lang.String code)
AbstractDeployableGraphics2D
addCode
in class AbstractDeployableGraphics2D
code
- fully formatted code stringpublic void appendCode(java.lang.String code)
AbstractDeployableGraphics2D
appendCode
in class AbstractDeployableGraphics2D
code
- the code to appendpublic 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)
AbstractDeployableGraphics2D
copyImage
in class AbstractDeployableGraphics2D
public java.lang.Object clone()
clone
in class java.lang.Object
public java.awt.Graphics create()
create
in class java.awt.Graphics
public java.awt.Graphics create(int x, int y, int width, int height)
create
in class java.awt.Graphics
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
java.io.IOException
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
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 scriptjava.io.IOException
public java.lang.String getTitle()
public void setTitle(java.lang.String title)
title
- the title to setpublic java.lang.String getDescription()
public void setDescription(java.lang.String description)
description
- the description to set