SvgAnimatorMidlet.java

/*
 * Copyright © 2011 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 java.io.*;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import javax.microedition.m2g.*;

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

    /**
     * Constructor.
     */
    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();
        }

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

        // *** set up the midlet menu
        int priority = 1;

        cmPlay = new Command("Play", Command.SCREEN, priority);
        c.addCommand(cmPlay);

        cmStop = new Command("Stop", Command.SCREEN, 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.SCREEN, priority++);
        c.addCommand(cmExit);
    }

    /**
     * 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();
            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);

        }
    }
    /*
     * Private members
     */
    private SVGAnimator svgAnimator;
    private Command cmPlay;
    private Command 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) {
    }
}