A MIDlet that displays the Doggy animation. : Animation « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » J2ME » Animation 




A MIDlet that displays the Doggy animation.


/*
 * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
 */

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

/**
 * A MIDlet that displays the Doggy animation.
 *
 @author Mark A. Patel - Motorola, Inc.
 @author Roger Riggs - Sun Microsystems, Inc.
 **/
public class DoggyMIDlet extends MIDlet implements CommandListener {
    Command cmdExit;
    /**
     * Constructs a new DoggyMIDlet
     **/
    public DoggyMIDlet() {
        cmdExit = new Command("Exit", Command.EXIT, 1);
    }
    
    /**
     * Starts the app by creating a new Doggy instance and displaying it
     **/
    protected void startApp() throws MIDletStateChangeException {
        Doggy d;

        d = new Doggy();

        d.addCommand(cmdExit);
        d.setCommandListener(this);
        
        Display.getDisplay(this).setCurrent(d);
  
    }
    
    protected void pauseApp() {
    }
    
    protected void destroyApp(boolean unconditional)
  throws MIDletStateChangeException {
    }
    
    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) {
            try {
                destroyApp(false);
            catch (Exception e) {}
            notifyDestroyed();
        }
    }
}
 class Doggy extends Canvas implements Runnable {

  /**
   * Number of frames in the animation
   **/
  static final int FRAME_COUNT = 17;

  /**
   * Normal frame delay (milliseconds)
   **/
  static final int FRAME_DELAY = 180;

  /**
   * Frame delay for the last frame where the dog is sleeping
   **/
  static final int LAST_FRAME_DELAY = 3000;

  /**
   * Relative horizontal position where each of the frames 
   * should be rendered. 0 represents the left edge of the screen
   * and 1024 represents the right edge of the run distance 
   * (1024 is used so that scaling can be performed using
   * simple bit shifts instead of division operations). 
   **/
  static final int[] framePositions = {
    05018637255874493010241024,
    83465146527993000
  };

  /**
   * An Image containing the 17 frames of the dog running,
   * stacked vertically.
   * Using a single image is much more efficient than using several 
   * images with each containing a single frame.
   * Each frame can be rendered seperately by setting the clip 
   * region to the size of a single frame and then
   * rendering the image at the correct position so that the desired 
   * frame isaligned with the clip region.
   **/
  Image doggyImages = null;

  /**
   * Width of a single animation frame
   **/
  int frameWidth = 0;

  /**
   * Height of a single animation frame
   **/
  int frameHeight = 0;

  /**
   * Index of the current frame
   **/
  int frameIndex = 0;

  /**
   * The distance, in pixels, that the dog can run (screen width less
   * the width of a single frame)
   **/
  int runLength = 0;

  /**
   * Indicates if the animation is currently running
   **/
  boolean running = false;

  /**
   * Called when this Canvas is shown.  This method starts the timer 
   * that runs the animation sequence.
   **/
  protected void showNotify() {
    if (doggyImages == null) {
      try {
        doggyImages =
          Image.createImage("/examples/animation/Doggy.png");
        frameWidth = doggyImages.getWidth();
        frameHeight = doggyImages.getHeight() / FRAME_COUNT;
      catch (Exception ioe) {
        return// no image to animate
      }
    }

    runLength = getWidth() - frameWidth;
    running = true;
    frameIndex = 0;

    new Thread(this).start();
  }

  /**
   * Called when this Canvas is hidden.  This method stops the
   * animation timer to free up processing
   * power while this Canvas is not showing.
   **/
  protected void hideNotify() {
    running = false;
  }

  public void run() {

    // Need to catch InterruptedExceptions and bail if one occurs
    try {
      while (running) {
        Thread.sleep((frameIndex == FRAME_COUNT - 1?
               LAST_FRAME_DELAY : FRAME_DELAY);

        // Remember the last frame index so we can compute
        // the repaint region
        int lastFrameIndex = frameIndex;

        // Update the frame index
        frameIndex = (frameIndex + 1% FRAME_COUNT;

        // Determine the left edge of the repaint region
        int repaintLeft = framePositions[lastFrameIndex];
        int repaintRight = framePositions[frameIndex];
        if (framePositions[lastFrameIndex> framePositions[frameIndex]) {
          repaintLeft = framePositions[frameIndex];
          repaintRight = framePositions[lastFrameIndex];
        }
        // Scale the repaint coordinates to the width of the screen
        repaintLeft = (repaintLeft * runLength>> 10;
        repaintRight = (repaintRight * runLength>> 10;

        // Trigger a repaint of the affected portion of the screen
        // Repaint the region where the last frame was rendered
        // (ensures that it is cleared)
        repaint(repaintLeft, 0,
            frameWidth + repaintRight - repaintLeft, frameHeight);
      }
    catch (InterruptedException e) {}
  }

  public void paint(Graphics g) {

    // Clear the background (fill with white)     
    // The clip region will limit the area that
    // actually gets cleared to save time
    g.setColor(0xFFFFFF);
    g.fillRect(00, getWidth(), getHeight());

    // Translate the graphics to the appropriate
    // position for the current frame
    g.translate((framePositions[frameIndex* runLength>> 100);

    // Constrain the clip region to the size of a single frame
    g.clipRect(00, frameWidth, frameHeight);

    // Draw the current frame by drawing the entire image with
    // the appropriate vertical offset so that the desired frame
    // lines up with the clip region.
    g.drawImage(doggyImages, 0, -(frameIndex * frameHeight),
          Graphics.LEFT + Graphics.TOP);
  }
}


           
       














Related examples in the same category
1.Animation MIDlet Animation MIDlet
2.Animation MIDlet 2Animation MIDlet 2
3.Animated TimerAnimated Timer
4.SweepSweep
5.Timer and animationTimer and animation
6.A simple Photo and Animation AlbumA simple Photo and Animation Album
7.Demonstrate simple animation using a Timer and TimerTaskDemonstrate simple animation using a Timer and TimerTask
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.