/** Resumes the animation if it has been interrupted in its playing state. */
 private void resumeAnimation() {
   if (state == STATE_INTERRUPTED) {
     // resume the interrupted animation
     svgAnimator.play();
     state = STATE_PLAYING;
   }
 }
  /** Pauses the animation if it is being played. */
  private void interruptAnimation() {
    if (state == STATE_PLAYING) {
      svgAnimator.pause();
      state = STATE_INTERRUPTED;
    }

    // Otherwise, the animation is paused or stopped.
  }
  public void destroyApp(boolean unconditional) {
    if (state != STATE_STOPPED) {
      svgAnimator.stop();
      state = STATE_STOPPED;
    }

    svgAnimator = null;
    svgCanvas = null;
    System.gc();
  }
  public void keyPressed(int keyCode) {
    if ((keyCode == KEY_PLAY) && (state != STATE_PLAYING)) {
      svgAnimator.play();
      state = STATE_PLAYING;
      System.err.println("PLAYING...");
    }

    if ((keyCode == KEY_PAUSE) && (state == STATE_PLAYING)) {
      svgAnimator.pause();
      state = STATE_PAUSED;
      System.err.println("PAUSED...");
    }

    if ((keyCode == KEY_STOP) && (state != STATE_STOPPED)) {
      svgAnimator.stop();
      svg.setCurrentTime(0);
      state = STATE_STOPPED;
      System.err.println("STOPPED...");
    }
  }
  public void startApp() {
    if (svgCanvas == null) {
      // If there is a splash screen defined, show it immediately.
      boolean hasSplash = ((splashImageName != null) && !"".equals(splashImageName));

      if (hasSplash) {
        InputStream splashStream = getClass().getResourceAsStream(splashImageName);

        try {
          Image splashImage = Image.createImage(splashStream);
          splashCanvas = new SplashCanvas(splashImage);
        } catch (IOException ioe) {
          ioe.printStackTrace();
          hasSplash = false;
        }

        if (splashCanvas != null) {
          Display.getDisplay(this).setCurrent(splashCanvas);
        }
      }

      long start = System.currentTimeMillis();

      // Get input stream to the SVG image stored in the MIDlet's jar.
      InputStream svgDemoStream = getClass().getResourceAsStream(svgImageName);

      // Build an SVGImage instance from the stream
      svgImage = null;

      if (svgDemoStream != null) {
        try {
          svgImage = (SVGImage) SVGImage.createImage(svgDemoStream, null);
        } catch (IOException io) {
          io.printStackTrace();
        }
      }

      if (svgImage == null) {
        throw new Error(ERROR_COULD_NOT_LOAD_SVG + svgImageName);
      }

      // Build an SVGAnimator from the SVGImage. The SVGAnimator will handle
      // all the animation details and run the SVG animation in a
      // Canvas instance.
      svgAnimator = SVGAnimator.createAnimator(svgImage);

      // Set to 10 fps (frames per second)
      svgAnimator.setTimeIncrement(0.01f);

      // Get the Canvas managed by the SVGAnimator and set the
      // svgImage's size to the canvas display area.
      svgCanvas = (Canvas) svgAnimator.getTargetComponent();
      svgImage.setViewportWidth(svgCanvas.getWidth());
      svgImage.setViewportHeight(svgCanvas.getHeight());

      // Hook the exit command so that we can exit the animation demo.
      svgCanvas.addCommand(exitCommand);
      svgCanvas.setCommandListener(this);

      // The SVG root element is used to reset the time on a stop operation.
      doc = svgImage.getDocument();
      svg = (SVGSVGElement) doc.getDocumentElement();

      // Hook-in key listeners to play, pause and stop the animation.
      svgAnimator.setSVGEventListener(this);

      long end = System.currentTimeMillis();

      if (hasSplash) {
        long waitMore = SPLASH_MIN_LENGTH - (end - start);

        if (waitMore > 0) {
          try {
            Thread.currentThread().sleep(waitMore);
          } catch (InterruptedException ie) {
            // Do nothing.
          }
        }
      }

      if (autoStart) {
        svgAnimator.play();
        state = STATE_PLAYING;
        System.err.println("PLAYING...");
      }
    }

    Display.getDisplay(this).setCurrent(svgCanvas);
    resumeAnimation();
  }