xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maria Carolina Arce Terceros <caroa...@gmail.com>
Subject Problems adding text dynamically
Date Mon, 09 Jan 2006 15:42:51 GMT
Hi!

I'm working on my Software Engineering thesis, therefore I need to be
able to manipulate svg content dynamically. So far I've been able to
add shapes to a document, but I'm having trouble adding text... I've
debugged it and it looks like the text is actually being added but it
is not shown in the SVGCanvas...

I'm pasting  an example...

It would be extremely helpfull if someone could give me a hint of what
I'm doing wrong :)

Thanks for your time!

M.Carolina Arce Terceros

import java.awt.*;

import javax.swing.*;

import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderListener;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherAdapter;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.svg.SVGDocument;

public class TextTest
{

    //~ Methods
..............................................................................................

    public static void main(String[] args)
    {
        final JSVGCanvas svgCanvas = new JSVGCanvas();
        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        final String nSvg = SVGDOMImplementation.SVG_NAMESPACE_URI;

        Document doc =
SVGDOMImplementation.getDOMImplementation().createDocument(nSvg,
"svg", null);
        svgCanvas.setDocument(doc);
        JPanel panel = new JPanel();
        panel.add(svgCanvas);

        svgCanvas.addSVGLoadEventDispatcherListener(new
SVGLoadEventDispatcherAdapter() {
                public void
svgLoadEventDispatchStarted(SVGLoadEventDispatcherEvent event)
                {
                    Element root =
svgCanvas.getSVGDocument().getDocumentElement();
                    root.setAttributeNS(nSvg, "width", "400");
                    root.setAttributeNS(nSvg, "height", "400");
                    svgCanvas.setBackground(Color.RED);
                }
            });
        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
                public void gvtRenderingPrepare(GVTTreeRendererEvent e) {}

                public void gvtRenderingCompleted(GVTTreeRendererEvent e)
                {
                    UpdateManager updateManager = svgCanvas.getUpdateManager();
                   
updateManager.getUpdateRunnableQueue().invokeLater(new Runnable() {
                            public void run()
                            {
                                String      nSvg =
SVGDOMImplementation.SVG_NAMESPACE_URI;
                                SVGDocument doc = svgCanvas.getSVGDocument();

                                Element text =
doc.createElementNS(nSvg, "text");
                                text.setAttributeNS(nSvg, "font-size", "15");
                                text.setAttributeNS(nSvg, "fill", "black");
                                text.setAttributeNS(nSvg, "x", "200");
                                text.setAttributeNS(nSvg, "y", "200");
                                Text textNode = doc.createTextNode("hello");
                                text.appendChild(textNode);

                                //                               
Element rect = doc.createElementNS(nSvg, "rect");
                                //                               
rect.setAttributeNS(null, "x", "100");
                                //                               
rect.setAttributeNS(null, "y", "100");
                                //                               
rect.setAttributeNS(null, "width", "40");
                                //                               
rect.setAttributeNS(null, "height", "50");
                                //                               
rect.setAttributeNS(null, "style", "fill:green");
                                //                               
rect.appendChild(text);
                                //                               
doc.getDocumentElement().appendChild(rect);
                                doc.getDocumentElement().appendChild(text);
                            }
                        });
                }
            });

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

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