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 paddy <pa...@hotmail.co.uk> on 2009/01/20 21:41:05 UTC

Problem rendering an SVG DOM built on the fly in Applet

Hi, I have written an applet which requests data from a server, which it then
recieves, decodes and builds an SVG DOM from scratch. However, I cant seem
to get the document to render. I've proven that the SVG is ok as I output it
to a text file and it renders fine in a browser. I did try implementing the
build listeners, and it seemed to be being built, but the render listeners
were never getting called. Could anyone shed any light on what's going
wrong?

Incidentally my DrawNode object just draws a rectangle on the SVG with a
label, and pnlSVGMain is directly on the applet. 

m_objDomImpl = SVGDOMImplementation.getDOMImplementation();
            m_objSVGDoc =
m_objDomImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg",
null);
            m_objSVGRoot = m_objSVGDoc.getDocumentElement();
            m_objSVGRoot = m_objSVGDoc.getDocumentElement();
            m_objSVGRoot.setAttributeNS(null, "width", "100%");
            m_objSVGRoot.setAttributeNS(null, "height", "100%");

            objSVGCanvas = new JSVGCanvas();
            objSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
            pnlSVGMain.add("Center",objSVGCanvas);

            CDrawNode testNode = new
CDrawNode("100","100",objStartNode.getAttributes().getNamedItem("id").getTextContent(),m_objSVGDoc,m_objSVGRoot);

            objSVGCanvas.setSVGDocument((SVGDocument)m_objSVGDoc);
            objSVGCanvas.repaint();

            SVGGraphics2D svgGen = new SVGGraphics2D(m_objSVGDoc);

            try
            {
               OutputStream os = new FileOutputStream("c:/systemdoc.svg");
               Writer w = new OutputStreamWriter(os, "iso-8859-1");
               svgGen.stream(m_objSVGRoot,w);
            }
            catch(Exception e)
            {
                System.out.println("Exception writing System SVG File: "+e);
            }


this is the SVG output in the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg contentScriptType="text/ecmascript" width="100%"
xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify"
contentStyleType="text/css" height="100%" preserveAspectRatio="xMidYMid
meet" xmlns="http://www.w3.org/2000/svg" version="1.0"
><rect x="100" width="40" height="40" y="100" style="stroke:black;
fill:white;"
  /><text x="100" y="100" style="fill:black" dx="2" dy="20"
lengthAdjust="spacingAndGlyphs" textLength="36"
  >BactonTerminal</text
></svg
>
-- 
View this message in context: http://www.nabble.com/Problem-rendering-an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21571223.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


RE: Problem rendering an SVG DOM built on the fly in Applet

Posted by paddy <pa...@hotmail.co.uk>.
Admittedly i've only been testing it in debug mode in netbeans, which is
possibly why that bit worked. That's how I validated that it is making valid
XML. Worth remembering though, like you say, as soon as it gets served up by
the webserver, that bit probaby wont work, although it was only for
debugging purposes. Cheers tho. Has anyone sucessfully got an applet to
render a SVG DOM built entirely from within code?



John C. Turnbull wrote:
> 
> I am not sure if it has anything to do with your problem but the first
> thing
> I see is that you won't be able to write to the C: drive as that is not
> accessible from within an applet.
> 
> HTH,
> 
> John
> 
>> -----Original Message-----
>> From: paddy [mailto:patrick_arnould@hotmail.co.uk]
>> Sent: Wednesday, 21 January 2009 07:41
>> To: batik-users@xmlgraphics.apache.org
>> Subject: Problem rendering an SVG DOM built on the fly in Applet
>> 
>> 
>> Hi, I have written an applet which requests data from a server, which
>> it then
>> recieves, decodes and builds an SVG DOM from scratch. However, I cant
>> seem
>> to get the document to render. I've proven that the SVG is ok as I
>> output it
>> to a text file and it renders fine in a browser. I did try implementing
>> the
>> build listeners, and it seemed to be being built, but the render
>> listeners
>> were never getting called. Could anyone shed any light on what's going
>> wrong?
>> 
>> Incidentally my DrawNode object just draws a rectangle on the SVG with
>> a
>> label, and pnlSVGMain is directly on the applet.
>> 
>> m_objDomImpl = SVGDOMImplementation.getDOMImplementation();
>>             m_objSVGDoc =
>> m_objDomImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
>> "svg",
>> null);
>>             m_objSVGRoot = m_objSVGDoc.getDocumentElement();
>>             m_objSVGRoot = m_objSVGDoc.getDocumentElement();
>>             m_objSVGRoot.setAttributeNS(null, "width", "100%");
>>             m_objSVGRoot.setAttributeNS(null, "height", "100%");
>> 
>>             objSVGCanvas = new JSVGCanvas();
>>             objSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
>>             pnlSVGMain.add("Center",objSVGCanvas);
>> 
>>             CDrawNode testNode = new
>> CDrawNode("100","100",objStartNode.getAttributes().getNamedItem("id").g
>> etTextContent(),m_objSVGDoc,m_objSVGRoot);
>> 
>>             objSVGCanvas.setSVGDocument((SVGDocument)m_objSVGDoc);
>>             objSVGCanvas.repaint();
>> 
>>             SVGGraphics2D svgGen = new SVGGraphics2D(m_objSVGDoc);
>> 
>>             try
>>             {
>>                OutputStream os = new
>> FileOutputStream("c:/systemdoc.svg");
>>                Writer w = new OutputStreamWriter(os, "iso-8859-1");
>>                svgGen.stream(m_objSVGRoot,w);
>>             }
>>             catch(Exception e)
>>             {
>>                 System.out.println("Exception writing System SVG File:
>> "+e);
>>             }
>> 
>> 
>> this is the SVG output in the file:
>> 
>> <?xml version="1.0" encoding="ISO-8859-1"?>
>> <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
>>           'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
>> <svg contentScriptType="text/ecmascript" width="100%"
>> xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify"
>> contentStyleType="text/css" height="100%" preserveAspectRatio="xMidYMid
>> meet" xmlns="http://www.w3.org/2000/svg" version="1.0"
>> ><rect x="100" width="40" height="40" y="100" style="stroke:black;
>> fill:white;"
>>   /><text x="100" y="100" style="fill:black" dx="2" dy="20"
>> lengthAdjust="spacingAndGlyphs" textLength="36"
>>   >BactonTerminal</text
>> ></svg
>> >
>> --
>> View this message in context: http://www.nabble.com/Problem-rendering-
>> an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21571223.html
>> Sent from the Batik - Users mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-
>> help@xmlgraphics.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Problem-rendering-an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21572377.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


RE: Problem rendering an SVG DOM built on the fly in Applet

Posted by "John C. Turnbull" <oz...@ozemail.com.au>.
I am not sure if it has anything to do with your problem but the first thing
I see is that you won't be able to write to the C: drive as that is not
accessible from within an applet.

HTH,

John

> -----Original Message-----
> From: paddy [mailto:patrick_arnould@hotmail.co.uk]
> Sent: Wednesday, 21 January 2009 07:41
> To: batik-users@xmlgraphics.apache.org
> Subject: Problem rendering an SVG DOM built on the fly in Applet
> 
> 
> Hi, I have written an applet which requests data from a server, which
> it then
> recieves, decodes and builds an SVG DOM from scratch. However, I cant
> seem
> to get the document to render. I've proven that the SVG is ok as I
> output it
> to a text file and it renders fine in a browser. I did try implementing
> the
> build listeners, and it seemed to be being built, but the render
> listeners
> were never getting called. Could anyone shed any light on what's going
> wrong?
> 
> Incidentally my DrawNode object just draws a rectangle on the SVG with
> a
> label, and pnlSVGMain is directly on the applet.
> 
> m_objDomImpl = SVGDOMImplementation.getDOMImplementation();
>             m_objSVGDoc =
> m_objDomImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
> "svg",
> null);
>             m_objSVGRoot = m_objSVGDoc.getDocumentElement();
>             m_objSVGRoot = m_objSVGDoc.getDocumentElement();
>             m_objSVGRoot.setAttributeNS(null, "width", "100%");
>             m_objSVGRoot.setAttributeNS(null, "height", "100%");
> 
>             objSVGCanvas = new JSVGCanvas();
>             objSVGCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
>             pnlSVGMain.add("Center",objSVGCanvas);
> 
>             CDrawNode testNode = new
> CDrawNode("100","100",objStartNode.getAttributes().getNamedItem("id").g
> etTextContent(),m_objSVGDoc,m_objSVGRoot);
> 
>             objSVGCanvas.setSVGDocument((SVGDocument)m_objSVGDoc);
>             objSVGCanvas.repaint();
> 
>             SVGGraphics2D svgGen = new SVGGraphics2D(m_objSVGDoc);
> 
>             try
>             {
>                OutputStream os = new
> FileOutputStream("c:/systemdoc.svg");
>                Writer w = new OutputStreamWriter(os, "iso-8859-1");
>                svgGen.stream(m_objSVGRoot,w);
>             }
>             catch(Exception e)
>             {
>                 System.out.println("Exception writing System SVG File:
> "+e);
>             }
> 
> 
> this is the SVG output in the file:
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
>           'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
> <svg contentScriptType="text/ecmascript" width="100%"
> xmlns:xlink="http://www.w3.org/1999/xlink" zoomAndPan="magnify"
> contentStyleType="text/css" height="100%" preserveAspectRatio="xMidYMid
> meet" xmlns="http://www.w3.org/2000/svg" version="1.0"
> ><rect x="100" width="40" height="40" y="100" style="stroke:black;
> fill:white;"
>   /><text x="100" y="100" style="fill:black" dx="2" dy="20"
> lengthAdjust="spacingAndGlyphs" textLength="36"
>   >BactonTerminal</text
> ></svg
> >
> --
> View this message in context: http://www.nabble.com/Problem-rendering-
> an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21571223.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-
> help@xmlgraphics.apache.org


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


RE: Problem rendering an SVG DOM built on the fly in Applet

Posted by paddy <pa...@hotmail.co.uk>.
Ive sorted it guys :jumping:, was a combination of the namespaces and where i
was setting the canvas. It seems it only works if I set the canvas after
everythings been added to the root node.
-- 
View this message in context: http://www.nabble.com/Problem-rendering-an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21589865.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


RE: Problem rendering an SVG DOM built on the fly in Applet

Posted by "John C. Turnbull" <oz...@ozemail.com.au>.
Maybe try an existing SVG that you know renders OK and embed it in a String
and load the canvas with that to see if it renders.

John

> -----Original Message-----
> From: paddy [mailto:patrick_arnould@hotmail.co.uk]
> Sent: Wednesday, 21 January 2009 10:13
> To: batik-users@xmlgraphics.apache.org
> Subject: Re: Problem rendering an SVG DOM built on the fly in Applet
> 
> 
> Thanks for the quick reply, I tried it as below, but still no joy i'm
> afraid.
> This is the relevant code from DrawNode:
> 
> Element nodeElmt =
> m_objSVGDoc.createElementNS(m_objSVGDoc.getNamespaceURI(), "rect");
>         nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "x",
> m_strX);
>         nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "y",
> m_strY);
>         nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "width",
> "40");
>         nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(),
> "height",
> "40");
>         nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "style",
> "stroke:black; fill:white;");
>         registerListeners(nodeElmt);
>         m_objSVGRoot.appendChild(nodeElmt);
>         array[0] = nodeElmt;
> 
>         Element txtElmt =
> m_objSVGDoc.createElementNS(m_objSVGDoc.getNamespaceURI(), "text");
>         Text value = m_objSVGDoc.createTextNode(m_strName);
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "x",
> m_strX);
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "y",
> m_strY);
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "dx",
> "2");
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "dy",
> "20");
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(),
> "textLength",
> "36" );
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(),
> "lengthAdjust", "spacingAndGlyphs" );
>         txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "style",
> "fill:black");
>         txtElmt.appendChild(value);
>         m_objSVGRoot.appendChild(txtElmt);
>         registerListeners(txtElmt);
>         array[1] = txtElmt;
> 
> 
> Is there something I should be doing when initialising the applet, or
> some
> special way the canvas has to be added to a swing component. I'm really
> at a
> loss.
> 
> 
> 
> Hi Paddy,
> 
> paddy <pa...@hotmail.co.uk> wrote on 01/20/2009 03:41:05 PM:
> 
> > Incidentally my DrawNode object just draws a rectangle on the SVG
> with a
> > label, and pnlSVGMain is directly on the applet.
> 
>    I suspect the problem is in your DrawNode code.  In particular I
> suspect that you are using 'createElement' instead of 'createElementNS'
> with the SVG_NAMESPACE_URI for the namespace.  It would produce
> exactly this sort of problem.
> 
> >             objSVGCanvas.setSVGDocument((SVGDocument)m_objSVGDoc);
> >             objSVGCanvas.repaint();
> 
>     The above repaint is not useful and could be harmful.  I suggest
> you remove it.  The Canvas will repaint as needed.
> 
> --
> View this message in context: http://www.nabble.com/Problem-rendering-
> an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21573842.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-
> help@xmlgraphics.apache.org


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


Re: Problem rendering an SVG DOM built on the fly in Applet

Posted by paddy <pa...@hotmail.co.uk>.
Thanks for the quick reply, I tried it as below, but still no joy i'm afraid.
This is the relevant code from DrawNode:

Element nodeElmt =
m_objSVGDoc.createElementNS(m_objSVGDoc.getNamespaceURI(), "rect");
        nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "x", m_strX);
        nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "y", m_strY);
        nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "width",
"40");
        nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "height",
"40");
        nodeElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "style",
"stroke:black; fill:white;");
        registerListeners(nodeElmt);
        m_objSVGRoot.appendChild(nodeElmt);
        array[0] = nodeElmt;

        Element txtElmt =
m_objSVGDoc.createElementNS(m_objSVGDoc.getNamespaceURI(), "text");
        Text value = m_objSVGDoc.createTextNode(m_strName);
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "x", m_strX);
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "y", m_strY);
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "dx", "2");
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "dy", "20");
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "textLength",
"36" );
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(),
"lengthAdjust", "spacingAndGlyphs" );
        txtElmt.setAttributeNS(m_objSVGDoc.getNamespaceURI(), "style",
"fill:black");
        txtElmt.appendChild(value);
        m_objSVGRoot.appendChild(txtElmt);
        registerListeners(txtElmt);
        array[1] = txtElmt;


Is there something I should be doing when initialising the applet, or some
special way the canvas has to be added to a swing component. I'm really at a
loss.



Hi Paddy,

paddy <pa...@hotmail.co.uk> wrote on 01/20/2009 03:41:05 PM:

> Incidentally my DrawNode object just draws a rectangle on the SVG with a
> label, and pnlSVGMain is directly on the applet. 

   I suspect the problem is in your DrawNode code.  In particular I
suspect that you are using 'createElement' instead of 'createElementNS'
with the SVG_NAMESPACE_URI for the namespace.  It would produce
exactly this sort of problem.

>             objSVGCanvas.setSVGDocument((SVGDocument)m_objSVGDoc);
>             objSVGCanvas.repaint();

    The above repaint is not useful and could be harmful.  I suggest
you remove it.  The Canvas will repaint as needed.

-- 
View this message in context: http://www.nabble.com/Problem-rendering-an-SVG-DOM-built-on-the-fly-in-Applet-tp21571223p21573842.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Problem rendering an SVG DOM built on the fly in Applet

Posted by th...@kodak.com.
Hi Paddy,

paddy <pa...@hotmail.co.uk> wrote on 01/20/2009 03:41:05 PM:

> Incidentally my DrawNode object just draws a rectangle on the SVG with a
> label, and pnlSVGMain is directly on the applet. 

   I suspect the problem is in your DrawNode code.  In particular I
suspect that you are using 'createElement' instead of 'createElementNS'
with the SVG_NAMESPACE_URI for the namespace.  It would produce
exactly this sort of problem.

>             objSVGCanvas.setSVGDocument((SVGDocument)m_objSVGDoc);
>             objSVGCanvas.repaint();

    The above repaint is not useful and could be harmful.  I suggest
you remove it.  The Canvas will repaint as needed.

>             SVGGraphics2D svgGen = new SVGGraphics2D(m_objSVGDoc);
> 
>             try
>             {
>                OutputStream os = new 
FileOutputStream("c:/systemdoc.svg");
>                Writer w = new OutputStreamWriter(os, "iso-8859-1");
>                svgGen.stream(m_objSVGRoot,w);

    You might find batik.dom.util.DOMUtilities.writeDocument a little
bit more direct for serializing a DOM.