This is probably a question for the batik list, but I'm not on that at
present so thought I'd ask here. 

What's the best way to get an SVG loaded so I can draw it to a
buffered image? I basically want to load an SVG and turn it into a
raster icon. I'm trying to use JSVGComponent but the loading is so
multithreaded I can't figure it out at all. Just trying to listen does
nothing - I wait forever for a loaded event.

 It seems similar to the annoying Java default image loading
behavior where it incrementally loads images - great for web-like
incremental drawing effects, but annoying if you just want to load an
image, right now, no questions.

I think it might be waiting on something - a paint() command or
similar - to actually load - lazy loading, in other words.

Here's my current craptastic code. It generates the following:
        Loading an SVG document as a Component.
        Loading document from file:/bag2/test/svg/arrowhead-blue.svg
        Waiting on SVG to load...
        Waiting on SVG to load...
        Waiting on SVG to load...
        Waiting on SVG to load...
        Waiting on SVG to load...
ERROR   Exception
 due to java.lang.NullPointerException: null
java.lang.NullPointerException
        at
org.apache.batik.swing.svg.JSVGComponent.getSVGDocumentSize(Unknown
Source)
        at
com.PartnerSoft.gui.SVGLib.createSVGComponent(SVGLib.java:52)
        at
com.PartnerSoft.gui.SVGLib.createImageFromSVG(SVGLib.java:28)
        at
com.PartnerSoft.gui.LocalImageFactory.getImage(LocalImageFactory.java


    private static JLabel scapegoat = new JLabel("BAAAA!!");

    public static Image createImageFromSVG(File svgFile) {
        try {
            Component component = createSVGComponent(svgFile);
            Image result = scapegoat.createImage(component.getWidth(),
component.getHeight());
            Graphics g = result.getGraphics();
            component.paint(g);
            g.dispose();
            return result;
            }
        catch (Exception oopsie) {
            SystemLog.singleton().error(oopsie);
            return GUILib.questionMark();
            }
            
        }

    public static Component createSVGComponent(File svgFile) {
        try {
            SystemLog.singleton().enter("Loading an SVG document as a
Component.");
            JSVGComponent component = new JSVGComponent();
            component.setProgressivePaint(false);
            String url = "file:" + svgFile.getCanonicalPath(); 
            SystemLog.singleton().enter("Loading document from " +
url);
            component.loadSVGDocument(url);
            SVGEar ear = new SVGEar();
            component.addSVGDocumentLoaderListener(ear);
            while (component.getSVGDocument() == null 
                   || component.getSVGDocumentSize() == null
                   || component.getSVGDocumentSize().getWidth() == 0
                   ) {
                SystemLog.singleton().enter("Waiting on SVG to
load...");
                try {Thread.sleep(100);} catch (InterruptedException
oopsie) {}
                }
            Dimension2D preferred = component.getSVGDocumentSize();
            SystemLog.singleton().enter("SVG is " + preferred);
            component.setSize((int)preferred.getWidth(),
(int)preferred.getHeight());
            return component;
            }
        catch (Exception oopsie) {
            SystemLog.singleton().error(oopsie);
            return new JLabel("AAIIEE!!");
            }

        }

    private static class SVGEar extends SVGDocumentLoaderAdapter {
        public boolean done = false;
        public void documentLoadingCompleted(SVGDocumentLoaderEvent
evt) {
            done = true;
            }
        }


I was adding that SVGEar as a loaderlistener to the component but it
never got an event (done was always false).


-- 

Paul Reavis                                      [EMAIL PROTECTED]
Design Lead
Partner Software, Inc.                        http://www.partnersoft.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to