Documentation of 'kcl.waterloo.deploy.svg.WSVGGraphics2D' Java class
WSVGGraphics2D
kcl.waterloo.deploy.svg

Class 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.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.

    • 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 Detail

      • WSVGGraphics2D

        public WSVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D g)
    • Method Detail

      • setPaintObject

        public void setPaintObject(java.lang.Object referenceObject)
        Description copied from class: AbstractDeployableGraphics2D
        Call 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:
        setPaintObject in class AbstractDeployableGraphics2D
        Parameters:
        referenceObject - the paintObject to set
      • addComment

        public void addComment(java.lang.String comment)
        Description copied from class: AbstractDeployableGraphics2D
        Sets 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:
        addComment in class AbstractDeployableGraphics2D
        Parameters:
        comment - the comment to set as a String
      • prependCode

        public void prependCode(java.lang.String code)
        Description copied from class: AbstractDeployableGraphics2D
        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).
        Specified by:
        prependCode in class AbstractDeployableGraphics2D
        Parameters:
        code - fully formatted code string
      • 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: AbstractDeployableGraphics2D
        Implemented in some Graphics2D subclasses e.g. com.sun.java.Graphics2D. This is not part of the java.awt.Graphics2D API.
        Specified by:
        copyImage in class AbstractDeployableGraphics2D
        Returns:
        true or false
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • create

        public java.awt.Graphics create()
        Specified by:
        create in class java.awt.Graphics
      • create

        public java.awt.Graphics create(int x,
                                        int y,
                                        int width,
                                        int height)
        Overrides:
        create in class java.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 String
        dim - the Dimensions of the component being painted
        inline - if true, SVG code will be inlined in the HTML file
        jsLoc - Location of the canvg.js file
        cssLoc - Location of the CSS file
        httpdFlag - 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

You see the box below because you did not login.