0

I'm using SVG Salamander and Java(1.8). How should I animate the SVG image?

The loaded .svg file already contains the animations. I think I need to update somehow the image, but how?

I heard you can animate each DOM element through SVGUniverse. But that sounds kinda ugly...

current hierarchial usage, if it helps: SVGPanel <- Jlabel <- SVGIcon

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
Hehe Muha
  • 19
  • 6

1 Answers1

1

I found this answer.

new Timer(100, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    long diff = System.currentTimeMillis() - start_time;
                    icon.getSvgUniverse().setCurTime(diff % (10 * 1000));
                    try {
                        icon.getSvgUniverse().updateTime();
                    } catch (SVGException ex) {
                        ex.printStackTrace();
                    }
                    icon.paintIcon(null, g2, 0, 0);
                    //or repaint it somehow!
                }
            }).start();

Notice that when your animation is T seconds, (10 secons * 1000 here), you have to animate your file within 0ms-9999ms. hitting 10th seconds will return it to 0-second.

Soley
  • 1,716
  • 1
  • 19
  • 33
  • works like a charm! thx a lot! but i don't realy get why System.currentTimeMillis() is used here... why not just Double time = icon.getSvgUniverse().getCurTime(); icon.getSvgUniverse().setCurTime(time += 0.0147); ? – Hehe Muha Mar 17 '15 at 14:58
  • If you want to have more realistic timing, (like in games which you have to have your game sync with machine clock to make it universal), I used the system time. You can add time by yourself manually, like what you said, but because of some background tasks, your animation may have some 1, 5, etc ms delays which makes it huge when it runs for some seconds or minutes. Therefore, because of using system time, it will be exactly at that time no matter when you call or draw your svg. :D – Soley Mar 17 '15 at 19:15