Implementing the LoadStaticMIDlet

A MIDlet that will display a previously created SVG graphic of a lion cub which you can manipulate by zooming and rotating.

  1. Create the LoadStaticMIDlet class file.

  2. Import the required classes and assign this class to the com.nokia.midp.examples.svg package.

    /* Copyright © 2006 Nokia. */
    package com.nokia.midp.examples.svg;
    
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    
    
  3. Set LoadStaticMIDlet to extend midlet and to implement CommandListener.

    /**
     * Simple midlet that displays a static SVG file.
     */
    public class LoadStaticMIDlet extends MIDlet implements CommandListener {
    
    	
  4. Write the below code to construct the MIDlet.

    /**
     * Constructor.
     */
        public LoadStaticMIDlet() {
        	
            // *** create the SVG canvas
            svgCanvas = new SvgCanvas2 (false);
            svgCanvas.setCommandListener(this);
            
    		

    Create a new SvgCanvas2 object for building the actual image.

            // *** grab a reference to the display
            display = Display.getDisplay(this);
            display.setCurrent(svgCanvas);
            
            
  5. Set up the menu that will hold the commands you can manipulate the image with. The commands will be defined at the end of the code, in Step 7.

    // *** set up the midlet menu
            int hotKey = 0;
            
            cmZoomIn = new Command("Zoom In", Command.SCREEN, hotKey++);
            svgCanvas.addCommand(cmZoomIn);
            
            cmZoomOut = new Command("Zoom Out", Command.SCREEN, hotKey++);
            svgCanvas.addCommand(cmZoomOut);
    
            cmRotateIn = new Command("Rotate Counter", Command.SCREEN, hotKey++);
            svgCanvas.addCommand(cmRotateIn);
    
            cmRotateOut = new Command("Rotate Clockwise", Command.SCREEN, hotKey++);
            svgCanvas.addCommand(cmRotateOut);
            
            cmRestoreView = new Command("Restore View", Command.SCREEN, hotKey++);
            svgCanvas.addCommand(cmRestoreView);
            
            cmExit = new Command("Exit", Command.SCREEN, hotKey++);
            svgCanvas.addCommand(cmExit);
        }
    
    
    	
  6. Add the following three required methods.

    /**
    	 * Required midlet method.
    	 */
        public void startApp() {
        }
    
    
    	/**
    	 * Required midlet method.
    	 */
        public void pauseApp() {
        }
    
        
    	/**
    	 * Required midlet method.
    	 */
        public void destroyApp(boolean unconditional) {
        }
    
    
    	
  7. Define the functionality for the menu items you created in step 5.

    /**
    	 * Handle the various menu actions.
    	 */
        public void commandAction(Command c, Displayable s) {
            if (c == cmExit) {
                destroyApp(false);
                notifyDestroyed();
            } else if ( c == cmZoomIn ) {
                svgCanvas.zoomIn();
            } else if ( c == cmZoomOut ) {
                svgCanvas.zoomOut();
            } else if ( c == cmRotateIn ) {
                svgCanvas.rotateIn();
            } else if ( c == cmRotateOut ) {
                svgCanvas.rotateOut();
            } else if ( c == cmRestoreView ) {
                svgCanvas.restoreView();
            }	
        }
        
    
    	
  8. Initialize the Commands for the menu functions, the display and object of the SvgCanvas2 class that is used with this MIDlet.

    /*
    * Private members
    */
    
        private Display display;
        private SvgCanvas2 svgCanvas;
    
        private Command cmZoomIn;
        private Command cmZoomOut;
        private Command cmRotateIn;
        private Command cmRotateOut;
        private Command cmRestoreView;
        private Command cmExit;
    
    }