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 Cui Jian <ke...@informatik.uni-bremen.de> on 2009/05/19 17:44:24 UTC

[Color animation]

Dear Batik experts,

I am trying to do something with svg's animation features and having the 
following problem (which is illustrated in the attached program)
a ball's color is made to change from red to blue during one second and 
repeats all over.
But I want to stop the color-changing but pressing the button "stop" and 
also start the color-chaning again by pressing the button "blink" by 
setting the repeatcount attribute using the following the codes:
                    
animateColorE.setAttribute(SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "0");
and
                    
animateColorE.setAttribute(SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, 
"indefinite");
But it just doesn't work...
Could you guys please tell me why or how I can do it right?

thanks very much,
Cui

--------------------------------------------------------------------

package rotationTest;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.SVGConstants;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ColorChangingAndStopping extends JFrame{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private JSVGCanvas canvas;
    private Document svgDoc;
    private Element animateColorE;
       
    public ColorChangingAndStopping(){
       
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        this.getContentPane().setLayout(new BorderLayout());
       
        this.canvas = new JSVGCanvas();
        this.canvas.setDocumentState (JSVGCanvas.ALWAYS_DYNAMIC);
       
        DOMImplementation dom = 
SVGDOMImplementation.getDOMImplementation ();
        svgDoc =
            dom.createDocument(SVGConstants.SVG_NAMESPACE_URI, 
SVGConstants.SVG_SVG_TAG, null);
       
        this.initBall();
       
        this.initStopBtn();
       
        this.canvas.setDocument(svgDoc);
       
        this.canvas.setMySize(new Dimension(300, 300));
       
        this.getContentPane().add(this.canvas, "Center");
       
        this.pack();
        this.setVisible(true);
       
    }
   
    private void initStopBtn() {
        final JButton centerBtn = new JButton("stop");
        centerBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                if(centerBtn.getText().equals("stop")){
                    
animateColorE.setAttribute(SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "0");
                    centerBtn.setText("blink");
                }
                else{
                    
animateColorE.setAttribute(SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "0");
                    centerBtn.setText("stop");                   
                }
            }
        });
        this.getContentPane().add(centerBtn, "South");
    }
   
    private void initBall(){
        Element ballE = 
svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 
SVGConstants.SVG_CIRCLE_TAG);
        svgDoc.getDocumentElement().appendChild(ballE);
        ballE.setAttributeNS(null, SVGConstants.SVG_CX_ATTRIBUTE, ""+80);
        ballE.setAttributeNS(null, SVGConstants.SVG_CY_ATTRIBUTE, ""+100);
        ballE.setAttributeNS(null, SVGConstants.SVG_R_ATTRIBUTE, ""+20);
        ballE.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, "red");
        animateColorE = 
svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 
SVGConstants.SVG_ANIMATE_COLOR_TAG);
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_ATTRIBUTE_NAME_ATTRIBUTE, SVGConstants.SVG_FILL_ATTRIBUTE);
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_ATTRIBUTE_TYPE_ATTRIBUTE, "xml");
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_FROM_ATTRIBUTE, "red");
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_TO_ATTRIBUTE, "blue");
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_FILL_ATTRIBUTE, "freeze");
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_DUR_ATTRIBUTE, "1s");
        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "indefinite");
        ballE.appendChild(animateColorE);
    }
       
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ColorChangingAndStopping();
            }
        });
    }
   
}


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


Re: [Color animation]

Posted by Cameron McCormack <ca...@mcc.id.au>.
Cui Jian:
> however, removing the element is the last thing I want to do, because it  
> might cause concurrent exception if any related thread doesn't work in  
> time, and most of the time they don't...

As Helder and Thomas said, if you’re manipulating the DOM from the Swing
event dispatch thread (in response to the JButton clicks), you’re doing
it wrong.

> I've tried the elementTimeControl and it works perfectly, remaining one  
> problem though:
>
> Actually I want the animation only start if I press the button, which  
> means it is stopped at first as the svg is loaded.
> But if I do the endElement() directly after I initiate the animateColorE  
> like this:
>
>        animateColorE.setAttributeNS(null,  
> SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "indefinite");
>        ((ElementTimeControl)animateColorE).endElement();
>
> I would get a null pointer exception when I start the program:

endElement() probably doesn’t work until it’s in the document, which
will initialise it.

But you can avoid that by having begin="indefinite" on the element.
That’ll make it not start when inserted, and just wait for a
beginElement() call.

-- 
Cameron McCormack ≝ http://mcc.id.au/

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


Re: [Color animation]

Posted by th...@kodak.com.
Hi Cui,
Helder Magalhães <he...@gmail.com> wrote on 05/20/2009 05:02:19 
PM:

> >> Weird, this hints towards the common "not using the update manager
> >> thread" issue [1]...
> >
> > m... I am still quite a beginner so just dare not touch this part :-)
> 
> On the contrary, this is exactly where you want to go, unless you
> prefer voodoo chicken coding [1] forever! :-D

   Let me second this.  You _must_ make sure that you only ever
modify the DOM tree in the UpdateManager's runnableQueue or else
you will just make loads more work for yourself (as well as making
a very unstable product - on a faster or slower machine it _will_
break make no mistake about it).


Re: [Color animation]

Posted by Helder Magalhães <he...@gmail.com>.
Hi Cui,

>> Weird, this hints towards the common "not using the update manager
>> thread" issue [1]...
>
> m... I am still quite a beginner so just dare not touch this part :-)

On the contrary, this is exactly where you want to go, unless you
prefer voodoo chicken coding [1] forever! :-D

Please check the already stated FAQ item (you may read it all, as
there are lot's of tightly related things [2] as well as other
interesting stuff). Also, while manipulating the document with Java
you'll be probably interested in "Scripting With Java" [3]. ;-)

Hope this helps,
 Helder

[1] http://c2.com/cgi/wiki?VoodooChickenCoding
[2] http://xmlgraphics.apache.org/batik/faq.html#null-updatemanager
[3] http://xmlgraphics.apache.org/batik/using/scripting/java.html

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


Re: [Color animation]

Posted by Cui Jian <ke...@informatik.uni-bremen.de>.
Hi Helder,
> Hi Cui,
>
>
>   
>> however, removing the element is the last thing I want to do, because it
>> might cause concurrent exception if any related thread doesn't work in time,
>> and most of the time they don't...
>>     
>
> Weird, this hints towards the common "not using the update manager
> thread" issue [1]...
>   
m... I am still quite a beginner so just dare not touch this part :-)
>> Actually I want the animation only start if I press the button, which means
>> it is stopped at first as the svg is loaded.
>>     
>
> You can start on element clicking using a syntax similar to
> begin="elementId.click". A set of nice examples is available [2],
> although I haven't checked if Batik even supports this syntax.
>
>   
am not sure if it works, 'cause there is no reference object in my 
case... but am trying to use another way to solve this problem...
>   
>> I would get a null pointer exception when I start the program:
>> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
>>     
>
> Note that there were a couple of fixes in the trunk code [3] recently
> (revision 711768, for example, see bug 46155 for more information)
> which seem tightly related.
>
>   
am looking into it, thanks very much!
> Please remember to include relevant information such as Batik version
> (or revision when working with SVN), Java version and operating system
> (as a minimum).
>
>
> Hope this helps,
>  Helder
>
>
> [1] http://xmlgraphics.apache.org/batik/faq.html#batik-error
> [2] http://www.xml.com/pub/a/2004/06/30/svgtype.html
> [3] http://xmlgraphics.apache.org/batik/download.html#Subversion+repository
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>   
best,
Cui


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


Re: [Color animation]

Posted by Helder Magalhães <he...@gmail.com>.
Hi Cui,


> however, removing the element is the last thing I want to do, because it
> might cause concurrent exception if any related thread doesn't work in time,
> and most of the time they don't...

Weird, this hints towards the common "not using the update manager
thread" issue [1]...


> Actually I want the animation only start if I press the button, which means
> it is stopped at first as the svg is loaded.

You can start on element clicking using a syntax similar to
begin="elementId.click". A set of nice examples is available [2],
although I haven't checked if Batik even supports this syntax.


> I would get a null pointer exception when I start the program:
> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Note that there were a couple of fixes in the trunk code [3] recently
(revision 711768, for example, see bug 46155 for more information)
which seem tightly related.


Please remember to include relevant information such as Batik version
(or revision when working with SVN), Java version and operating system
(as a minimum).


Hope this helps,
 Helder


[1] http://xmlgraphics.apache.org/batik/faq.html#batik-error
[2] http://www.xml.com/pub/a/2004/06/30/svgtype.html
[3] http://xmlgraphics.apache.org/batik/download.html#Subversion+repository

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


Re: [Color animation]

Posted by Cui Jian <ke...@informatik.uni-bremen.de>.
Cameron McCormack schrieb:
> Modifying the attributes of an animation element while it is in the
> document will not change the animation.  This is half bug, half
> underdefined-ness in the SVG specification.  What will work, though, is
> removing the animation element from the document to stop it, and
> re-inserting it when you want to start it again.
>
> Alternatively, you could use the beginElement() and endElement() methods
> on animateColorE (you’ll need to cast it to ElementTimeControl to get
> those methods).  That will start or stop the animation.  (Technically,
> it’s like adding an entry to the begin="" or end="" attribute with the
> current time.)
>   
thanks very much for your answer!
however, removing the element is the last thing I want to do, because it 
might cause concurrent exception if any related thread doesn't work in 
time, and most of the time they don't...
I've tried the elementTimeControl and it works perfectly, remaining one 
problem though:

Actually I want the animation only start if I press the button, which 
means it is stopped at first as the svg is loaded.
But if I do the endElement() directly after I initiate the animateColorE 
like this:

        animateColorE.setAttributeNS(null, 
SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "indefinite");
        ((ElementTimeControl)animateColorE).endElement();

I would get a null pointer exception when I start the program:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at 
org.apache.batik.dom.svg.SVGOMAnimationElement.endElement(SVGOMAnimationElement.java:158)
    at 
rotationTest.ColorChangingAndStopping.initBall(ColorChangingAndStopping.java:124)
    at 
rotationTest.ColorChangingAndStopping.<init>(ColorChangingAndStopping.java:75)
    at 
rotationTest.ColorChangingAndStopping$4.run(ColorChangingAndStopping.java:131)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I suppose the animateColorE is not properly initiated yet when I tried 
to stop it.
so the question is, where is the right place I should do the 
endElement() if I want the animation remaining stopped firstly, or when 
is animateColorE completely initiated so that I could use the endElement 
on it to stop it in time before it shows itself?

thanks very much in advance,
Cui

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


Re: [Color animation]

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hi Cui.

Cui Jian:
> I am trying to do something with svg's animation features and having the  
> following problem (which is illustrated in the attached program)
> a ball's color is made to change from red to blue during one second and  
> repeats all over.
> But I want to stop the color-changing but pressing the button "stop" and  
> also start the color-chaning again by pressing the button "blink" by  
> setting the repeatcount attribute using the following the codes:
>                     
> animateColorE.setAttribute(SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE, "0");
> and
>                     
> animateColorE.setAttribute(SVGConstants.SVG_REPEAT_COUNT_ATTRIBUTE,  
> "indefinite");
> But it just doesn't work...

Modifying the attributes of an animation element while it is in the
document will not change the animation.  This is half bug, half
underdefined-ness in the SVG specification.  What will work, though, is
removing the animation element from the document to stop it, and
re-inserting it when you want to start it again.

Alternatively, you could use the beginElement() and endElement() methods
on animateColorE (you’ll need to cast it to ElementTimeControl to get
those methods).  That will start or stop the animation.  (Technically,
it’s like adding an entry to the begin="" or end="" attribute with the
current time.)

-- 
Cameron McCormack ≝ http://mcc.id.au/

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