xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vincent Hardy <vincent.ha...@sun.com>
Subject Re: SVGGraphics2D.getRoot() truncates SVG document?
Date Mon, 17 Sep 2001 07:09:21 GMT
George,

George Armhold wrote:
> 
> I'm trying to manipulate the DOM generated by an SVGGraphics2D in order
> to add some javascript.  I find that if I call SVGGraphics.getRoot(), it
> wipes out any existing tags in the DOM.  The example below will
> demonstrate this.  

Right, this is the expected behavior of the SVGGraphics2D. If you'd like
an example of how to use it for what you are trying to do, you can 
have a look at the org.apache.batik.svggen.SwingPrettyPrint class which
does manipulate the DOM generated by SVGGraphics2D. 

You can also have a look at the DOMTreeManager and the DOMGroupManager 
for more explanations on how the DOM is managed in SVGGraphics2D.

I hope this helps.
Vincent.

> If you comment out the two lines starting with
> g.getRoot(), it'll generate an SVG doc with some simple drawing code.
> When the getRoot() line is active, the drawing code gets truncated.    I
> get this behaviour with Batik 1.0 as well as 1.1 RC2.  Am I supposed to
> somehow add the node returned from getRoot() back into the DOM? If so,
> how?
> 
> Any explanations appreciated.
> 
> import javax.xml.parsers.*;
> import java.io.*;
> import org.w3c.dom.*;
> import java.awt.*;
> import org.apache.batik.svggen.SVGGraphics2D;
> import org.apache.batik.dom.svg.SVGDOMImplementation;
> 
> public class Simple {
> 
>   public static void main(String[] args) {
>     String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
>     DOMImplementation impl =
> SVGDOMImplementation.getDOMImplementation();
>     Document doc = impl.createDocument(svgNS, "svg", null);
> 
>     // Create an instance of the SVG Generator
>     SVGGraphics2D g = new SVGGraphics2D(doc);
> 
>     // define the size of the SVG
>     Dimension d = new Dimension(1024, 1024);
>     g.setSVGCanvasSize(d);
> 
>     // set background color
>     g.setColor(Color.white);
>     g.fill(new Rectangle(0, 0, d.width, d.height));
> 
>     // now do our drawing
>     Rectangle rect = new Rectangle(0, 0, 100, 100);
>     g.setColor(Color.blue);
>     g.fill(rect);
> 
>     Element scriptElement = doc.createElement("script");
>     scriptElement.setAttribute("type", "text/ecmascript");
>     scriptElement.setAttribute("xlink:href",
> "http://pablo.rutgers.edu/~armhold/animate.js");
> 
>     // this call to getRoot() appears to corrupt the SVG document:
>     Element svgRoot = g.getRoot();
>     svgRoot.appendChild(scriptElement);
> 
>     try {
>       PrintWriter writer = new PrintWriter(System.out);
>       g.stream(writer, false);
>       writer.flush();
>     } catch (Exception e) {
>       e.printStackTrace();
>     }
> 
>     System.exit(0);
>   }
> }
> 
> --
> George Armhold
> Rutgers University
> Bioinformatics Initiative
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Mime
View raw message