Hi,

   I’m trying to figure out of when certain methods become available to use on their way to being drawn by Batik and found something that doesn’t make sense to me.  I make a simple JSVGCanvas with one text Element in it and prints out some location information about that element.  The text will or will not show up depending upon when I define the JFrame for this canvas.  In the code below everywhere that has a “//YES” means that if the definition (frame = new JFrame("Test Module")) goes there, the text string will show up on the JSVGCanvas, where there is a “//NO”, the canvas will be blank.  It seems kind of strange that it causes the drop out just after theCanvas.setSVGDocument and up to the svgRoot.appendChild(jbas.theTextElement) lines.

   Any obvious explanations (hopefully not too obvious :) ?

 

Randy

 

package testsvg;

 

import org.apache.batik.swing.JSVGCanvas;

import org.apache.batik.dom.svg.SVGDOMImplementation;

import org.apache.batik.bridge.GVTBuilder;

import org.apache.batik.bridge.BridgeContext;

import org.apache.batik.bridge.UserAgentAdapter;

import org.apache.batik.gvt.GraphicsNode;

import org.w3c.dom.DOMImplementation;

import org.w3c.dom.Text;

import org.w3c.dom.svg.SVGDocument;

import org.w3c.dom.Element;

import org.w3c.dom.Document;

 

import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;

import org.apache.batik.swing.gvt.GVTTreeRendererEvent;

 

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.BorderLayout;

 

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class JSVGBasics {

    Element theTextElement;

 

    /** Creates a new instance of MainSVGTest */

    public JSVGBasics() {

    }

 

   public static void main(String[] args) {

    JFrame frame;

//YES

    JPanel jpan = new JPanel();

    Document doc;

    Element svgRoot;

    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

    final String SVG_WIDTH = "600";

    final String SVG_HEIGHT = "600";

    JSVGCanvas theCanvas;

//YES

        JSVGBasics jbas = new JSVGBasics();

//YES

       // Use constructor JSVGCanvas(SVGUserAgent ua, boolean eventsEnabled, boolean selectableText) to disable text selection.

        theCanvas = new JSVGCanvas(null, true, false);

//YES

    // Set up SVG DOM part:

        System.out.println("status: creating svg dom document...");

        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();

        doc = impl.createDocument(svgNS, "svg", null);

//YES

     // get the root element (the svg element)

        svgRoot = doc.getDocumentElement();

     // set the width and height attribute on the root svg element

        svgRoot.setAttributeNS(null, "width", SVG_WIDTH);

        svgRoot.setAttributeNS(null, "height", SVG_HEIGHT);

 

    // Forces the canvas to always be dynamic even if the current

    // document does not contain scripting or animation.

//YES

       theCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);

//YES

       System.out.println("status: doing setSVGDocument method...");

//YES

        theCanvas.setSVGDocument((SVGDocument) doc);

//NO

 

 

       //  Create a simple Element to display a text string.

           String someText = "some test text";

           jbas.theTextElement =  doc.createElementNS(svgNS,"text");

           Text theText = doc.createTextNode(someText);

           jbas.theTextElement.appendChild(theText);

           jbas.theTextElement.setAttributeNS(null, "id", "testelement");

           jbas.theTextElement.setAttributeNS(null, "x", "50");

           jbas.theTextElement.setAttributeNS(null, "y","100");

           jbas.theTextElement.setAttributeNS(null, "font-size", "14");

           jbas.theTextElement.setAttributeNS(null, "font-family", "Courier");

 

//NO

 

 

            // copy theTextElement into a final one so that it can be passed into the anonymous inner class.

            final Element theTextElement2 = jbas.theTextElement;

 

                   theCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {

                       public void gvtRenderingPrepare(GVTTreeRendererEvent e) {

                           System.out.println("Status: Rendering Started...");

                       }

                       public void gvtRenderingCompleted(GVTTreeRendererEvent e) {

                            System.out.println("");

                            System.out.println("Status: Rendering Completed...");

                            GVTBuilder builder = new GVTBuilder();

                            BridgeContext ctx = new BridgeContext(new UserAgentAdapter());

                            GraphicsNode aTextElementNode = builder.build(ctx, theTextElement2);

                            int y = (int) aTextElementNode.getBounds().getMaxY();

                            int x = (int) aTextElementNode.getBounds().getMinX();

                            System.out.println("Here comes the bridge-based info: ");

                            System.out.println("Text node info x/y: " + x + "/" + y);

                       }

                   });

 

 

 //NO

 

        System.out.println("status: appending text element.");

//NO

        svgRoot.appendChild(jbas.theTextElement);

//YES

        frame = new JFrame("Test Module");

 

        jpan.add("Center", theCanvas);

//YES

 

        frame.getContentPane().add(jpan, BorderLayout.CENTER);

       // Catch window closing event

        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            }

        });

 

        System.out.println("status: packing frame.");

      // theCanvas.repaint();

        frame.pack();

        System.out.println("status: setting frame visible.");

        frame.setVisible(true);

        System.out.println("status: done.");

    }

}

Reply via email to