Implementation

For information about the design and functionality of the MIDlet, see section Design.

The SvgAnimatorMidlet class handles both the MIDlet UI and lifecycle requirements and the setting up and rendering of the animated SVG image. There is no need for a GameCanvas-based class, since the animator provides a Canvas that can be displayed and drawn on.

The constructor of the class first creates an SVGImage using the content3.svg animation file as a source:

    public SvgAnimatorMidlet() {

        // *** load a test svg image
        SVGImage svgImage = null;
        try {
            InputStream svgStream = getClass().getResourceAsStream("content3.svg");
            svgImage = (SVGImage) (SVGImage.createImage(svgStream, null));
        } catch (Exception e) {
            e.printStackTrace();
        }

The constructor then creates an SVGAnimator from the SVGImage, sets the Canvas provided by the animator as the current Display, thus displaying the animated SVG image on the device screen, and sets listeners for receiving events from the Canvas and commands from the user:

        // *** setup the SVGAnimator
        svgAnimator = SVGAnimator.createAnimator(svgImage);
        svgAnimator.setSVGEventListener(new MySvgEventListener());
        Canvas c = (Canvas) (svgAnimator.getTargetComponent());
        Display.getDisplay(this).setCurrent(c);
        c.setCommandListener(this);

The menu command handler, implemented in the commandAction method, commands the animator to play, stop, and modify the animation frame rate:

    public void commandAction(Command c, Displayable s) {
        Canvas canvas = (Canvas) (svgAnimator.getTargetComponent());
        if (c == cmExit) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == cmPlay) {
            svgAnimator.play();
            canvas.addCommand(cmStop);
            canvas.removeCommand(cmPlay);
        } else if (c == cmStop) {
            canvas.addCommand(cmPlay);
            canvas.removeCommand(cmStop);
            svgAnimator.stop();
        } else if (c == cmIncrease) {
            float dt = svgAnimator.getTimeIncrement();
            dt = dt - 0.01f;
            if (dt < 0) {
                dt = 0f;
            }
            svgAnimator.setTimeIncrement(dt);

        } else if (c == cmDecrease) {
            float dt = svgAnimator.getTimeIncrement();
            dt = dt + 0.01f;
            svgAnimator.setTimeIncrement(dt);

        }
    }

Implementation specific to the Series 40 full touch version of the MIDlet

In the SvgAnimatorMidlet class, map the Play and Stop commands to action button 1:

        /** 
         * IconCommand is supported from Java Runtime 2.0.0 for Series 40 onwards.
         * Creates a new IconCommand Play with the given label, unselected icon, selected icon, type and priority.
         */
        cmPlay = new IconCommand("Play",startIcon,startIcon, IconCommand.ICON_OK,priority);
        c.addCommand(cmPlay);

        /** 
         * IconCommand is supported from Java Runtime 2.0.0 for Series 40 onwards.
         * Creates a new IconCommand Stop with the given label, unselected icon, selected icon, type and priority.
         */
        cmStop = new IconCommand("Stop", stopIcon,stopIcon,IconCommand.ICON_OK, priority++);

        // ...