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 massimo citterio <ci...@sinapto.net> on 2008/06/27 17:08:34 UTC

Re: smil animation broken after first run - with testcase application

On Fri, 2008-06-27 at 09:59 +1000, Cameron McCormack wrote:
> -batik-users
> 
> Hi Massimo.
> 
> massimo citterio:
> > I am trying to create a small application that can reproduce the
> > problem, as soon as it's done, I will post it
> 
> Thanks for looking in to this.  I haven’t had time to investigate the
> issue or reply to the list lately, but I don’t want you to be
> discouraged from a lack of response.  I look forward to seeing your
> reduced test case.
> 
> Thanks,
> 
> Cameron
> 



thank you for the reply.
Here it is.
It is basically the default SVGApplication from
http://xmlgraphics.apache.org/batik/using/scripting/java.html

it tries to do (10 times) 
-add a rect
-add an anim (translation of the rect)
-start the anim
-wait for some sec (2.5)
-destroy anim
-destroy rect

the fist time it's ok
the second time animation doesn't work anymore
and there's a batik exception 

java.lang.NullPointerException
SVGException: java.lang.NullPointerException
        at
org.apache.batik.anim.AnimationEngine.tick(AnimationEngine.java:389)
        at org.apache.batik.bridge.SVGAnimationEngine.access
$601(SVGAnimationEngine.java:99)
        at org.apache.batik.bridge.SVGAnimationEngine
$AnimationTickRunnable.run(SVGAnimationEngine.java:859)
        at
org.apache.batik.util.RunnableQueue.run(RunnableQueue.java:237)
        at java.lang.Thread.run(Thread.java:619)


I attach the code and the svg document

the code:

package demo.test;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherAdapter;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;
import org.apache.batik.script.Window;

import org.apache.batik.swing.svg.SVGUserAgentAdapter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGAnimationElement;

public class SVGApplication {

    public static void main(String[] args) {
        new SVGApplication();
    }
    JFrame frame;
    JSVGCanvas canvas;
    Document document;
    Window window;
    private boolean loaded;

    class LocalSVGUserAgentHandler extends SVGUserAgentAdapter {

        @Override
        public void displayError(String message) {
            System.out.println("SVGError: " + message);
        }

        @Override
        public void displayError(Exception ex) {
            System.out.println("SVGException: " + ex);
            ex.printStackTrace();
        }
    }

    public SVGApplication() {
        frame = new JFrame();
        //canvas = new JSVGCanvas();
        canvas = new JSVGCanvas(new LocalSVGUserAgentHandler(), true,
true);
        // Forces the canvas to always be dynamic even if the current
        // document does not contain scripting or animation.
        canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        canvas.addSVGLoadEventDispatcherListener(new
SVGLoadEventDispatcherAdapter() {

            public void
svgLoadEventDispatchStarted(SVGLoadEventDispatcherEvent e) {
                // At this time the document is available...
                document = canvas.getSVGDocument();
                // ...and the window object too.
                window = canvas.getUpdateManager().
                        getScriptingEnvironment().createWindow();
                // Registers the listeners on the document
                // just before the SVGLoad event is
                // dispatched.
                registerListeners();
                // It is time to pack the frame.
                frame.pack();
            }
        });

        frame.addWindowListener(new WindowAdapter() {

            public void windowOpened(WindowEvent e) {
                // The canvas is ready to load the base document
                // now, from the AWT thread.

canvas.setURI(getClass().getResource("/img/testanim.svg").toString());
            }
            });

        frame.getContentPane().add(canvas);
        frame.setSize(800, 600);
        frame.show();

        int cont = 0;
        while (true && cont < 10) {
            try {
                Thread.sleep(50);
                if (loaded) {
                    //when loaded, add rect, anim, start it
                    addRect();
                    addAnim();
                    cont++;
                    Thread.sleep(2500);
                    //then remove anim & rect
                    removeRectAnim();
                }
            } catch (InterruptedException ex) {

Logger.getLogger(SVGApplication.class.getName()).log(Level.SEVERE, null,
ex);
            }
        }
        System.exit(0);
    }

    void removeRectAnim() throws InterruptedException {

canvas.getUpdateManager().getUpdateRunnableQueue().invokeAndWait(new
Runnable() {

            public void run() {
                // Insert some actions on the DOM here
                String id = "anim1";
                Element elt = document.getElementById(id);

document.getElementById(id).getParentNode().removeChild(elt);

                id = "testrect2";
                elt = document.getElementById(id);

document.getElementById(id).getParentNode().removeChild(elt);
            }
        });
    }

    void addAnim() throws InterruptedException {

canvas.getUpdateManager().getUpdateRunnableQueue().invokeAndWait(new
Runnable() {

            public void run() {
                // Insert some actions on the DOM here

                System.out.println("create new anim");
                Element elt = document.getElementById("testrect2");

                String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

                //creating animation
                Element anim = document.createElementNS(svgNS,
"animateMotion");
                String id = "anim1";
                anim.setAttributeNS(null, "id", id);
                anim.setAttributeNS(null, "to", "-100,-100");
                anim.setAttributeNS(null, "begin", "indefinite");
                anim.setAttributeNS(null, "dur", "1s");
                anim.setAttributeNS(null, "fill", "remove");
                //adding anim
                elt.appendChild(anim);
                boolean res = ((SVGAnimationElement)
anim).beginElement();
                System.out.println("anim started with res:" + res);

            }
        });

    }

    void addRect() throws InterruptedException {

canvas.getUpdateManager().getUpdateRunnableQueue().invokeAndWait(new
Runnable() {

            public void run() {
                // Insert some actions on the DOM here

                System.out.println("create new rect");
                Element g = document.getElementById("layer1");

                String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
                Element rect = document.createElementNS(svgNS, "rect");

                rect.setAttributeNS(null, "id", "testrect2");
                rect.setAttributeNS(null, "x", "50");
                rect.setAttributeNS(null, "y", "50");
                rect.setAttributeNS(null, "width", "50");
                rect.setAttributeNS(null, "height", "50");
                g.appendChild(rect);

            }
        });

    }

    public void registerListeners() {
        // Gets an element from the loaded document.
        Element elt = document.getElementById("testrect");
        EventTarget t = (EventTarget) elt;

        this.loaded = true;
    }
}


the svg 


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="744.09448819"
   height="1052.3622047"
   id="svg2"
   sodipodi:version="0.32"
   inkscape:version="0.46"
   sodipodi:docname="testanim.svg"
   inkscape:output_extension="org.inkscape.output.svg.inkscape">
  <defs
     id="defs4">
    <inkscape:perspective
       sodipodi:type="inkscape:persp3d"
       inkscape:vp_x="0 : 526.18109 : 1"
       inkscape:vp_y="0 : 1000 : 0"
       inkscape:vp_z="744.09448 : 526.18109 : 1"
       inkscape:persp3d-origin="372.04724 : 350.78739 : 1"
       id="perspective10" />
  </defs>
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     gridtolerance="10000"
     guidetolerance="10"
     objecttolerance="10"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.35"
     inkscape:cx="375"
     inkscape:cy="520"
     inkscape:document-units="px"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="762"
     inkscape:window-height="737"
     inkscape:window-x="141"
     inkscape:window-y="25" />
  <metadata
     id="metadata7">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <rect

style="opacity:1;fill:#fe8f00;fill-opacity:0.9;fill-rule:nonzero;stroke:#ffa400;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;marker:none;marker-start:none;marker-mid:none;marker-end:none;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"
       id="testrect"
       width="245.71428"
       height="137.14285"
       x="80"
       y="175.21933" />
  </g>
</svg>


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