SvgAnimatorMidlet.java

/**
 * Copyright (c) 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia 
Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example;

import com.nokia.mid.ui.IconCommand;
import com.nokia.mid.ui.VirtualKeyboard;
import java.io.InputStream;
import javax.microedition.lcdui.*;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;
import javax.microedition.midlet.MIDlet;
import org.w3c.dom.svg.SVGSVGElement;

/**
 * Simple midlet that displays an animation using SVGAnimator, It also
 * demonstrates how SVGEventListener works.
 */
public class SvgAnimatorMidlet extends MIDlet implements CommandListener {

    SVGImage svgImage = null;
    Canvas c;

    /**
     * Constructor.
     */
    public SvgAnimatorMidlet() {

        /**
         * VirtualKeyboard is supported from Java Runtime 2.0.0 for Series 40
         * onwards. To determine whether a Series 40 device is a full touch
         * device and hide the open keypad command from the Options menu.
         */
        if (System.getProperty("com.nokia.keyboard.type").equals("None")) {
            VirtualKeyboard.hideOpenKeypadCommand(true);
        }

        Image stopIcon = null;
        Image startIcon = null;

        // *** load a test svg image and icon images
        try {
            InputStream svgStream = getClass().getResourceAsStream("content3.svg");
            svgImage = (SVGImage) (SVGImage.createImage(svgStream, null));

            stopIcon = Image.createImage("/stopIcon.png");
            startIcon = Image.createImage("/startIcon.png");


        } catch (Exception e) {
            e.printStackTrace();
        }


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


        /**
         * Scale the image to adapt to screen width
         */
        SVGSVGElement myEl = (SVGSVGElement) (svgImage.getDocument().getDocumentElement());
        myEl.setCurrentScale((float) (c.getWidth() * 1.0f / svgImage.getViewportWidth()));

        // *** set up the midlet menu
        int priority = 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++);

        cmIncrease = new Command("Increase Frame Rate", Command.SCREEN, priority++);
        c.addCommand(cmIncrease);

        cmDecrease = new Command("Decrease Frame Rate", Command.SCREEN, priority++);
        c.addCommand(cmDecrease);

        cmExit = new Command("Exit", Command.BACK, priority++);
        c.addCommand(cmExit);

        svgImage.setViewportWidth(c.getWidth());
        svgImage.setViewportHeight(c.getHeight());
    }

    /**
     * Required midlet method.
     */
    public void startApp() {
    }

    /**
     * Required midlet method.
     */
    public void pauseApp() {
    }

    /**
     * Required midlet method.
     */
    public void destroyApp(boolean unconditional) {
    }

    /**
     * Handle the various menu actions.
     */
    public void commandAction(Command c, Displayable s) {
        Canvas canvas = (Canvas) (svgAnimator.getTargetComponent());
        if (c == cmExit) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == cmPlay) {
            svgAnimator.play();
            /**
             * When play button is pressed play command is removed and stop
             * command is added to action button 1
             */
            canvas.addCommand(cmStop);
            canvas.removeCommand(cmPlay);
        } else if (c == cmStop) {
            /**
             * When stop button is pressed stop command is removed and play
             * command is added to action button 1
             */
            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);

        }
    }
    /*
     * Private members
     */
    private SVGAnimator svgAnimator;
    private IconCommand cmPlay;
    private IconCommand cmStop;
    private Command cmIncrease;
    private Command cmDecrease;
    private Command cmExit;
}

/**
 * Simple SVGEventListener with no action taken in event handling methods.
 */
class MySvgEventListener implements SVGEventListener {

    public void keyPressed(int keyCode) {
    }

    public void keyReleased(int keyCode) {
    }

    public void pointerPressed(int x, int y) {
    }

    public void pointerReleased(int x, int y) {
    }

    public void hideNotify() {
    }

    public void showNotify() {
    }

    public void sizeChanged(int w, int h) {
    }
}