You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Maria Carolina Arce Terceros <ca...@fuegolabs.com> on 2006/01/09 16:59:49 UTC

Problems trying to add text dynamically in SVGCanvas

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!

Maria Carolina Arce Terceros
carolina@fuego.com


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);
     }
}