xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Whittemore <mike.whittem...@gmail.com>
Subject Roundtrip Java2D Shape -> SVG File -> Java2D Shape
Date Sun, 06 Nov 2005 03:48:12 GMT
I've spent countless hours trying to get this to work. I simply want to be able
to save some Java2D shapes to an SVG file, and then later read the file back in
and reconstitute my Java2D shapes. I know my code for reading SVG files into
Jaava2D shapes works, as I've used an external program to generate SVG files and
my code successfully reads them in. I know my code for writing shapes to SVG
files works, as I've examined the generated files with an SVG viewer and can see
they are correct. The problem is that when I save to SVG with Batik, and then
try to read the file back in with Batik, I get an endless loop. I do not get
this with files generated by other SVG programs.

I would appreciate guidance on how to get this roundtripping to work. Thanks in
advance. -Mike

Some ugly test code to illustrate what I'm doing in my application:

//////////////////////////////////////
// The Test                         //
//////////////////////////////////////
import org.apache.batik.dom.*;
import org.apache.batik.svggen.*;
import org.w3c.dom.*;

import java.awt.*;
import java.awt.geom.*;
import java.io.*;

public class BatikRoundtripTest {

    public void execute() throws Exception {

        //
        // Write Java2D Shape to SVG File
        //

        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

        Document document = domImpl.createDocument(null, "svg", null);
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        svgGenerator.setSVGCanvasSize(new Dimension(200, 200));

        paint(svgGenerator);

        boolean useCSS = true;
        Writer out = new FileWriter(new File("C:\\rectangle.svg"));
        svgGenerator.stream(out, useCSS);
        out.close();

        //
        // Read same SVG File into Java2D Shape (endless loop when reading
        // files generated by Batik)
        //

        ShapeLoader shapeloader = new ShapeLoader("C:\\rectangle.svg", 0);
        shapeloader.run();
        Shape s = shapeloader.getShape();
        System.out.println("True or false, the shape null? " + (s == null));
     }

    public void paint(Graphics2D g2d) {
        Rectangle2D r = new Rectangle2D.Double(0.0, 0.0, 100.0, 100.0);
        g2d.fill(r);
    }

    public static void main(String[] args) throws Exception {
        new BatikRoundtripTest().execute();
    }

}

/////////////////////////////////////////////////////////////
// A helper file for loading SVG files into JAVA 2D shapes //
/////////////////////////////////////////////////////////////

import org.apache.batik.gvt.*;
import org.apache.batik.swing.svg.*;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.List;

public class ShapeLoader extends Thread {

    private JSVGComponent component = new JSVGComponent();
    private List<Shape> shapes = new LinkedList<Shape>();
    private String shapeFile;
    private int shapeIndex;
    private boolean shapesLoaded = false;
    private Shape shape;

    public ShapeLoader(final String shapeFile, final int shapeIndex) {

        this.shapeFile = shapeFile;
        this.shapeIndex = shapeIndex;

        component.addGVTTreeBuilderListener(

            new GVTTreeBuilderAdapter() {

                public void gvtBuildCompleted(GVTTreeBuilderEvent event) {

                    super.gvtBuildCompleted(event);

                    GVTTreeWalker treeWalker = new
GVTTreeWalker(event.getGVTRoot());

                    GraphicsNode graphicsNode = null;

                    while((graphicsNode = treeWalker.nextGraphicsNode()) != null) {
                        shapes.add(graphicsNode.getOutline());
                    }

                    shapesLoaded = true;
                }
            }
        );
    }

    public void run() {
        loadShape();
    }

    public Shape getShape() {
        return shape;
    }

    public synchronized void loadShape() {

       try {
            String urlString = new
File(shapeFile).getAbsoluteFile().toURL().toString();
            System.out.println(urlString + " loading...");

            component.loadSVGDocument(urlString);

            while(shapesLoaded != true) {

                // TODO: need timeout - file not found is endless loop

                try {
                    wait(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();  // TODO: better error handling
                }
            }

           System.out.println(urlString + " loaded.");
        }
        catch (MalformedURLException e) {
            e.printStackTrace();        // TODO: better error handling
        }

        shape = shapes.get(shapeIndex);
        shapes.clear();
    }
}



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


Mime
View raw message