<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FJ2ME%2FAnimation</id>
		<title>Java/J2ME/Animation - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FJ2ME%2FAnimation"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/J2ME/Animation&amp;action=history"/>
		<updated>2026-04-22T01:54:55Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/J2ME/Animation&amp;diff=7271&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/J2ME/Animation&amp;diff=7271&amp;oldid=prev"/>
				<updated>2010-06-01T06:37:48Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 06:37, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/J2ME/Animation&amp;diff=7270&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/J2ME/Animation&amp;diff=7270&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:44Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== A MIDlet that displays the Doggy animation. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved.&lt;br /&gt;
 */&lt;br /&gt;
import javax.microedition.midlet.*;&lt;br /&gt;
import javax.microedition.lcdui.*;&lt;br /&gt;
/**&lt;br /&gt;
 * A MIDlet that displays the Doggy animation.&lt;br /&gt;
 *&lt;br /&gt;
 * @author Mark A. Patel - Motorola, Inc.&lt;br /&gt;
 * @author Roger Riggs - Sun Microsystems, Inc.&lt;br /&gt;
 **/&lt;br /&gt;
public class DoggyMIDlet extends MIDlet implements CommandListener {&lt;br /&gt;
    Command cmdExit;&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructs a new DoggyMIDlet&lt;br /&gt;
     **/&lt;br /&gt;
    public DoggyMIDlet() {&lt;br /&gt;
        cmdExit = new Command(&amp;quot;Exit&amp;quot;, Command.EXIT, 1);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    /**&lt;br /&gt;
     * Starts the app by creating a new Doggy instance and displaying it&lt;br /&gt;
     **/&lt;br /&gt;
    protected void startApp() throws MIDletStateChangeException {&lt;br /&gt;
        Doggy d;&lt;br /&gt;
        d = new Doggy();&lt;br /&gt;
        d.addCommand(cmdExit);&lt;br /&gt;
        d.setCommandListener(this);&lt;br /&gt;
        &lt;br /&gt;
        Display.getDisplay(this).setCurrent(d);&lt;br /&gt;
  &lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void pauseApp() {&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void destroyApp(boolean unconditional)&lt;br /&gt;
  throws MIDletStateChangeException {&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void commandAction(Command c, Displayable d) {&lt;br /&gt;
        if (c == cmdExit) {&lt;br /&gt;
            try {&lt;br /&gt;
                destroyApp(false);&lt;br /&gt;
            } catch (Exception e) {}&lt;br /&gt;
            notifyDestroyed();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 class Doggy extends Canvas implements Runnable {&lt;br /&gt;
  /**&lt;br /&gt;
   * Number of frames in the animation&lt;br /&gt;
   **/&lt;br /&gt;
  static final int FRAME_COUNT = 17;&lt;br /&gt;
  /**&lt;br /&gt;
   * Normal frame delay (milliseconds)&lt;br /&gt;
   **/&lt;br /&gt;
  static final int FRAME_DELAY = 180;&lt;br /&gt;
  /**&lt;br /&gt;
   * Frame delay for the last frame where the dog is sleeping&lt;br /&gt;
   **/&lt;br /&gt;
  static final int LAST_FRAME_DELAY = 3000;&lt;br /&gt;
  /**&lt;br /&gt;
   * Relative horizontal position where each of the frames &lt;br /&gt;
   * should be rendered. 0 represents the left edge of the screen&lt;br /&gt;
   * and 1024 represents the right edge of the run distance &lt;br /&gt;
   * (1024 is used so that scaling can be performed using&lt;br /&gt;
   * simple bit shifts instead of division operations). &lt;br /&gt;
   **/&lt;br /&gt;
  static final int[] framePositions = {&lt;br /&gt;
    0, 50, 186, 372, 558, 744, 930, 1024, 1024,&lt;br /&gt;
    834, 651, 465, 279, 93, 0, 0, 0&lt;br /&gt;
  };&lt;br /&gt;
  /**&lt;br /&gt;
   * An Image containing the 17 frames of the dog running,&lt;br /&gt;
   * stacked vertically.&lt;br /&gt;
   * Using a single image is much more efficient than using several &lt;br /&gt;
   * images with each containing a single frame.&lt;br /&gt;
   * Each frame can be rendered seperately by setting the clip &lt;br /&gt;
   * region to the size of a single frame and then&lt;br /&gt;
   * rendering the image at the correct position so that the desired &lt;br /&gt;
   * frame isaligned with the clip region.&lt;br /&gt;
   **/&lt;br /&gt;
  Image doggyImages = null;&lt;br /&gt;
  /**&lt;br /&gt;
   * Width of a single animation frame&lt;br /&gt;
   **/&lt;br /&gt;
  int frameWidth = 0;&lt;br /&gt;
  /**&lt;br /&gt;
   * Height of a single animation frame&lt;br /&gt;
   **/&lt;br /&gt;
  int frameHeight = 0;&lt;br /&gt;
  /**&lt;br /&gt;
   * Index of the current frame&lt;br /&gt;
   **/&lt;br /&gt;
  int frameIndex = 0;&lt;br /&gt;
  /**&lt;br /&gt;
   * The distance, in pixels, that the dog can run (screen width less&lt;br /&gt;
   * the width of a single frame)&lt;br /&gt;
   **/&lt;br /&gt;
  int runLength = 0;&lt;br /&gt;
  /**&lt;br /&gt;
   * Indicates if the animation is currently running&lt;br /&gt;
   **/&lt;br /&gt;
  boolean running = false;&lt;br /&gt;
  /**&lt;br /&gt;
   * Called when this Canvas is shown.  This method starts the timer &lt;br /&gt;
   * that runs the animation sequence.&lt;br /&gt;
   **/&lt;br /&gt;
  protected void showNotify() {&lt;br /&gt;
    if (doggyImages == null) {&lt;br /&gt;
      try {&lt;br /&gt;
        doggyImages =&lt;br /&gt;
          Image.createImage(&amp;quot;/examples/animation/Doggy.png&amp;quot;);&lt;br /&gt;
        frameWidth = doggyImages.getWidth();&lt;br /&gt;
        frameHeight = doggyImages.getHeight() / FRAME_COUNT;&lt;br /&gt;
      } catch (Exception ioe) {&lt;br /&gt;
        return; // no image to animate&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    runLength = getWidth() - frameWidth;&lt;br /&gt;
    running = true;&lt;br /&gt;
    frameIndex = 0;&lt;br /&gt;
    new Thread(this).start();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Called when this Canvas is hidden.  This method stops the&lt;br /&gt;
   * animation timer to free up processing&lt;br /&gt;
   * power while this Canvas is not showing.&lt;br /&gt;
   **/&lt;br /&gt;
  protected void hideNotify() {&lt;br /&gt;
    running = false;&lt;br /&gt;
  }&lt;br /&gt;
  public void run() {&lt;br /&gt;
    // Need to catch InterruptedExceptions and bail if one occurs&lt;br /&gt;
    try {&lt;br /&gt;
      while (running) {&lt;br /&gt;
        Thread.sleep((frameIndex == FRAME_COUNT - 1) ?&lt;br /&gt;
               LAST_FRAME_DELAY : FRAME_DELAY);&lt;br /&gt;
        // Remember the last frame index so we can compute&lt;br /&gt;
        // the repaint region&lt;br /&gt;
        int lastFrameIndex = frameIndex;&lt;br /&gt;
        // Update the frame index&lt;br /&gt;
        frameIndex = (frameIndex + 1) % FRAME_COUNT;&lt;br /&gt;
        // Determine the left edge of the repaint region&lt;br /&gt;
        int repaintLeft = framePositions[lastFrameIndex];&lt;br /&gt;
        int repaintRight = framePositions[frameIndex];&lt;br /&gt;
        if (framePositions[lastFrameIndex] &amp;gt; framePositions[frameIndex]) {&lt;br /&gt;
          repaintLeft = framePositions[frameIndex];&lt;br /&gt;
          repaintRight = framePositions[lastFrameIndex];&lt;br /&gt;
        }&lt;br /&gt;
        // Scale the repaint coordinates to the width of the screen&lt;br /&gt;
        repaintLeft = (repaintLeft * runLength) &amp;gt;&amp;gt; 10;&lt;br /&gt;
        repaintRight = (repaintRight * runLength) &amp;gt;&amp;gt; 10;&lt;br /&gt;
        // Trigger a repaint of the affected portion of the screen&lt;br /&gt;
        // Repaint the region where the last frame was rendered&lt;br /&gt;
        // (ensures that it is cleared)&lt;br /&gt;
        repaint(repaintLeft, 0,&lt;br /&gt;
            frameWidth + repaintRight - repaintLeft, frameHeight);&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {}&lt;br /&gt;
  }&lt;br /&gt;
  public void paint(Graphics g) {&lt;br /&gt;
    // Clear the background (fill with white)     &lt;br /&gt;
    // The clip region will limit the area that&lt;br /&gt;
    // actually gets cleared to save time&lt;br /&gt;
    g.setColor(0xFFFFFF);&lt;br /&gt;
    g.fillRect(0, 0, getWidth(), getHeight());&lt;br /&gt;
    // Translate the graphics to the appropriate&lt;br /&gt;
    // position for the current frame&lt;br /&gt;
    g.translate((framePositions[frameIndex] * runLength) &amp;gt;&amp;gt; 10, 0);&lt;br /&gt;
    // Constrain the clip region to the size of a single frame&lt;br /&gt;
    g.clipRect(0, 0, frameWidth, frameHeight);&lt;br /&gt;
    // Draw the current frame by drawing the entire image with&lt;br /&gt;
    // the appropriate vertical offset so that the desired frame&lt;br /&gt;
    // lines up with the clip region.&lt;br /&gt;
    g.drawImage(doggyImages, 0, -(frameIndex * frameHeight),&lt;br /&gt;
          Graphics.LEFT + Graphics.TOP);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Animated Timer ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*--------------------------------------------------&lt;br /&gt;
 * AnimatedTimer - Main midlet.&lt;br /&gt;
 * Shows canvas with an animated timer. Includes&lt;br /&gt;
 * configuration options to start/stop the timer&lt;br /&gt;
 * and to adjust the sleep interval of the thread&lt;br /&gt;
 *&lt;br /&gt;
 * Example from the book:     Core J2ME Technology&lt;br /&gt;
 * Copyright John W. Muchow   http://www.CoreJ2ME.ru&lt;br /&gt;
 * You may use/modify for any non-commercial purpose &lt;br /&gt;
 *-------------------------------------------------*/&lt;br /&gt;
import java.util.Stack;&lt;br /&gt;
import javax.microedition.lcdui.Alert;&lt;br /&gt;
import javax.microedition.lcdui.Canvas;&lt;br /&gt;
import javax.microedition.lcdui.rumand;&lt;br /&gt;
import javax.microedition.lcdui.rumandListener;&lt;br /&gt;
import javax.microedition.lcdui.Display;&lt;br /&gt;
import javax.microedition.lcdui.Displayable;&lt;br /&gt;
import javax.microedition.lcdui.Form;&lt;br /&gt;
import javax.microedition.lcdui.Gauge;&lt;br /&gt;
import javax.microedition.lcdui.Graphics;&lt;br /&gt;
import javax.microedition.lcdui.Image;&lt;br /&gt;
import javax.microedition.lcdui.List;&lt;br /&gt;
import javax.microedition.midlet.MIDlet;&lt;br /&gt;
public class AnimatedTimer extends MIDlet {&lt;br /&gt;
  private Display display; // The display&lt;br /&gt;
  protected TimerCanvas cvTimer; // Canvas to display timer&lt;br /&gt;
  protected OptionsList lsOptions; // List to change timer options&lt;br /&gt;
  protected SleepForm fmSleep; // Form with gauge to set timer sleep&lt;br /&gt;
  protected DisplayManager displayMgr; // Class to help manage screens&lt;br /&gt;
  public AnimatedTimer() {&lt;br /&gt;
    display = Display.getDisplay(this);&lt;br /&gt;
    cvTimer = new TimerCanvas(this);&lt;br /&gt;
    lsOptions = new OptionsList(&amp;quot;Timer options&amp;quot;, List.IMPLICIT, this);&lt;br /&gt;
    fmSleep = new SleepForm(&amp;quot;Adjust sleep&amp;quot;, this);&lt;br /&gt;
    // Create a display manager object&lt;br /&gt;
    displayMgr = new DisplayManager(display, cvTimer);&lt;br /&gt;
  }&lt;br /&gt;
  protected void startApp() {&lt;br /&gt;
    // Start with the canvas&lt;br /&gt;
    display.setCurrent(cvTimer);&lt;br /&gt;
  }&lt;br /&gt;
  protected void pauseApp() {&lt;br /&gt;
  }&lt;br /&gt;
  protected void destroyApp(boolean unconditional) {&lt;br /&gt;
  }&lt;br /&gt;
  public void exitMIDlet() {&lt;br /&gt;
    destroyApp(true);&lt;br /&gt;
    notifyDestroyed();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*--------------------------------------------------&lt;br /&gt;
 * Use a stack to push and pop displayable objects&lt;br /&gt;
 *&lt;br /&gt;
 * public void pushDisplayable(Displayable)&lt;br /&gt;
 * public void popDisplayable()&lt;br /&gt;
 * public void home()  &lt;br /&gt;
 *&lt;br /&gt;
 * Example from the book:     Core J2ME Technology&lt;br /&gt;
 * Copyright John W. Muchow   http://www.CoreJ2ME.ru&lt;br /&gt;
 * You may use/modify for any non-commercial purpose &lt;br /&gt;
 *-------------------------------------------------*/&lt;br /&gt;
class DisplayManager extends Stack {&lt;br /&gt;
  private Display display; // Reference to Display object&lt;br /&gt;
  private Displayable mainDisplayable; // Main displayable for MIDlet&lt;br /&gt;
  private Alert alStackError; // Alert for error conditions&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Display manager constructor&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public DisplayManager(Display display, Displayable mainDisplayable) {&lt;br /&gt;
    // Only one display object per midlet, this is it&lt;br /&gt;
    this.display = display;&lt;br /&gt;
    this.mainDisplayable = mainDisplayable;&lt;br /&gt;
    // Create an alert displayed when an error occurs&lt;br /&gt;
    alStackError = new Alert(&amp;quot;Displayable Stack Error&amp;quot;);&lt;br /&gt;
    alStackError.setTimeout(Alert.FOREVER); // Modal&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Push the current displayable onto stack and set&lt;br /&gt;
   * the passed in displayable as active&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void pushDisplayable(Displayable newDisplayable) {&lt;br /&gt;
    push(display.getCurrent());&lt;br /&gt;
    display.setCurrent(newDisplayable);&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Return to the main displayable object of MIDlet&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void home() {&lt;br /&gt;
    while (elementCount &amp;gt; 1)&lt;br /&gt;
      pop();&lt;br /&gt;
    display.setCurrent(mainDisplayable);&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Pop displayable from stack and set as active&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void popDisplayable() {&lt;br /&gt;
    // If the stack is not empty, pop next displayable&lt;br /&gt;
    if (empty() == false)&lt;br /&gt;
      display.setCurrent((Displayable) pop());&lt;br /&gt;
    else&lt;br /&gt;
      // On error show an alert&lt;br /&gt;
      // Once acknowledged, set &amp;quot;mainDisplayable&amp;quot; as active&lt;br /&gt;
      display.setCurrent(alStackError, mainDisplayable);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*--------------------------------------------------&lt;br /&gt;
 * Class TimerCanvas&lt;br /&gt;
 *&lt;br /&gt;
 * Animate a sequence of images to simulate &lt;br /&gt;
 * a moving timer&lt;br /&gt;
 *&lt;br /&gt;
 * Example from the book:     Core J2ME Technology&lt;br /&gt;
 * Copyright John W. Muchow   http://www.CoreJ2ME.ru&lt;br /&gt;
 * You may use/modify for any non-commercial purpose &lt;br /&gt;
 *-------------------------------------------------*/&lt;br /&gt;
class TimerCanvas extends Canvas implements Runnable, CommandListener {&lt;br /&gt;
  private AnimatedTimer midlet; // Main midlet&lt;br /&gt;
  private Command cmExit; // Exit midlet&lt;br /&gt;
  private Command cmOptions; // Display options list&lt;br /&gt;
  private Image im = null; // Sequence of images&lt;br /&gt;
  private int imageCount = 4; // Four images in the sequence&lt;br /&gt;
  private int imageWidth; // Width of one image in the sequence&lt;br /&gt;
  private int imageHeight; // Height of one image in the sequence&lt;br /&gt;
  private int imageIndex; // Current image in the sequence&lt;br /&gt;
  private int translate_x; // Translated x and y&lt;br /&gt;
  private int translate_y;&lt;br /&gt;
  private int viewport_x; // Location of the viewport&lt;br /&gt;
  private int viewport_y;&lt;br /&gt;
  private boolean active = false; // Timer active?&lt;br /&gt;
  private boolean requestedToStop = false; // Did user request to stop timer&lt;br /&gt;
  private int sleepTime = 400; // Current sleep time (milliseconds)&lt;br /&gt;
  public TimerCanvas(AnimatedTimer midlet) {&lt;br /&gt;
    // Call canvas constructor&lt;br /&gt;
    super();&lt;br /&gt;
    // Save reference to MIDlet so we can&lt;br /&gt;
    // access the display manager class&lt;br /&gt;
    this.midlet = midlet;&lt;br /&gt;
    // Create commands &amp;amp; listen for events&lt;br /&gt;
    cmExit = new Command(&amp;quot;Exit&amp;quot;, Command.EXIT, 1);&lt;br /&gt;
    cmOptions = new Command(&amp;quot;Config&amp;quot;, Command.SCREEN, 2);&lt;br /&gt;
    addCommand(cmExit);&lt;br /&gt;
    addCommand(cmOptions);&lt;br /&gt;
    setCommandListener(this);&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Application manager is about to display canvas&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  protected void showNotify() {&lt;br /&gt;
    if (im == null) {&lt;br /&gt;
      try {&lt;br /&gt;
        // Read the png from a file and get width and&lt;br /&gt;
        // height of one image in the sequence&lt;br /&gt;
        im = Image.createImage(&amp;quot;/timer.png&amp;quot;);&lt;br /&gt;
        imageHeight = im.getHeight();&lt;br /&gt;
        imageWidth = im.getWidth() / imageCount;&lt;br /&gt;
        // Get the coordinates for x/y of viewport&lt;br /&gt;
        // Viewport is centered on the display&lt;br /&gt;
        viewport_x = (getWidth() / 2) - (imageWidth / 2);&lt;br /&gt;
        viewport_y = (getHeight() / 2) - (imageHeight / 2);&lt;br /&gt;
        // Set first translated coordinate to match viewport&lt;br /&gt;
        translate_x = viewport_x;&lt;br /&gt;
        translate_y = viewport_y;&lt;br /&gt;
      } catch (Exception e) {&lt;br /&gt;
        System.err.println(&amp;quot;Unable to read png file.&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      // Begin with the first image in the sequence&lt;br /&gt;
      imageIndex = 0;&lt;br /&gt;
    }&lt;br /&gt;
    // If the user has not requested to stop the timer...&lt;br /&gt;
    if (!requestedToStop)&lt;br /&gt;
      active = true;&lt;br /&gt;
    new Thread(this).start();&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Application manager is no longer displaying canvas&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  protected void hideNotify() {&lt;br /&gt;
    active = false;&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Draw next timer in sequence&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  protected void paint(Graphics g) {&lt;br /&gt;
    if (im != null) {&lt;br /&gt;
      // Due to a bug in MIDP 1.0.3 we need to&lt;br /&gt;
      // force a clear of the display&lt;br /&gt;
      g.setColor(255, 255, 255); // White pen&lt;br /&gt;
      g.fillRect(0, 0, getWidth(), getHeight());&lt;br /&gt;
      g.setColor(0, 0, 0); // Black pen&lt;br /&gt;
      // Viewport at center of display&lt;br /&gt;
      g.setClip(viewport_x, viewport_y, imageWidth, imageHeight);&lt;br /&gt;
      // Draw image at translated coordinates&lt;br /&gt;
      g.drawImage(im, translate_x, translate_y, Graphics.TOP&lt;br /&gt;
          | Graphics.LEFT);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Loop forever, translating image coordinates&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void run() {&lt;br /&gt;
    try {&lt;br /&gt;
      while (active) {&lt;br /&gt;
        Thread.sleep(sleepTime);&lt;br /&gt;
        repaint();&lt;br /&gt;
        // Reached the last image in sequence&lt;br /&gt;
        if (imageIndex == imageCount - 1) {&lt;br /&gt;
          // Reset translated coordinates&lt;br /&gt;
          translate_x = viewport_x;&lt;br /&gt;
          translate_y = viewport_y;&lt;br /&gt;
        } else {&lt;br /&gt;
          // Translate coordinate system to the left&lt;br /&gt;
          translate_x -= imageWidth;&lt;br /&gt;
        }&lt;br /&gt;
        // Which image in the sequence is next&lt;br /&gt;
        imageIndex = (imageIndex + 1) % imageCount;&lt;br /&gt;
      }&lt;br /&gt;
    } catch (InterruptedException e) {&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Called from the &amp;quot;Config&amp;quot; options menu&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void startTimer() {&lt;br /&gt;
    requestedToStop = false;&lt;br /&gt;
    active = true;&lt;br /&gt;
    repaint();&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Called from the &amp;quot;Config&amp;quot; options menu&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void stopTimer() {&lt;br /&gt;
    requestedToStop = true;&lt;br /&gt;
    active = false;&lt;br /&gt;
    repaint();&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Called from form/gauge to adjust sleep&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void setSleep(int sleepTime) {&lt;br /&gt;
    this.sleepTime = sleepTime;&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Called from form/gauge to adjust sleep&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public int getSleep() {&lt;br /&gt;
    return sleepTime;&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Command event handling&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void commandAction(Command c, Displayable s) {&lt;br /&gt;
    if (c == cmOptions) {&lt;br /&gt;
      // Push current displayable and show the options list&lt;br /&gt;
      midlet.displayMgr.pushDisplayable(midlet.lsOptions);&lt;br /&gt;
    } else if (c == cmExit) {&lt;br /&gt;
      midlet.exitMIDlet();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*--------------------------------------------------&lt;br /&gt;
 * Class SleepForm&lt;br /&gt;
 *&lt;br /&gt;
 * Form with gauge to adjust sleep interval of timer&lt;br /&gt;
 *&lt;br /&gt;
 * Example from the book:     Core J2ME Technology&lt;br /&gt;
 * Copyright John W. Muchow   http://www.CoreJ2ME.ru&lt;br /&gt;
 * You may use/modify for any non-commercial purpose &lt;br /&gt;
 *-------------------------------------------------*/&lt;br /&gt;
class SleepForm extends Form implements CommandListener {&lt;br /&gt;
  private AnimatedTimer midlet; // Main midlet&lt;br /&gt;
  private Command cmBack, // Back to options list&lt;br /&gt;
      cmHome, // Go to main displayable (canvas)&lt;br /&gt;
      cmSave; // Save new sleep time&lt;br /&gt;
  private Gauge gaSleep; // Gauge to adjust sleep&lt;br /&gt;
  public SleepForm(String title, AnimatedTimer midlet) {&lt;br /&gt;
    // Call the form constructor&lt;br /&gt;
    super(title);&lt;br /&gt;
    // Save reference to MIDlet so we can&lt;br /&gt;
    // access the display manager class&lt;br /&gt;
    this.midlet = midlet;&lt;br /&gt;
    // Commands&lt;br /&gt;
    cmSave = new Command(&amp;quot;Save&amp;quot;, Command.SCREEN, 1);&lt;br /&gt;
    cmBack = new Command(&amp;quot;Back&amp;quot;, Command.BACK, 2);&lt;br /&gt;
    cmHome = new Command(&amp;quot;Home&amp;quot;, Command.SCREEN, 2);&lt;br /&gt;
    // Gauge to adjust the length of timer sleep&lt;br /&gt;
    gaSleep = new Gauge(&amp;quot;Timer Sleep&amp;quot;, true, 100, 1000);&lt;br /&gt;
    // Set to current sleep. Gauge holds values 0 to 100,&lt;br /&gt;
    // divide the current sleep (milliseconds) by 10&lt;br /&gt;
    gaSleep.setValue(midlet.cvTimer.getSleep() / 10);&lt;br /&gt;
    // Add to form and listen for events&lt;br /&gt;
    append(gaSleep);&lt;br /&gt;
    addCommand(cmSave);&lt;br /&gt;
    addCommand(cmBack);&lt;br /&gt;
    addCommand(cmHome);&lt;br /&gt;
    setCommandListener(this);&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Command event handling&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void commandAction(Command c, Displayable s) {&lt;br /&gt;
    if (c == cmSave) {&lt;br /&gt;
      // Gauge returns a value between 0 and 100&lt;br /&gt;
      // We want milliseconds, so multiply by 10&lt;br /&gt;
      midlet.cvTimer.setSleep(gaSleep.getValue() * 10);&lt;br /&gt;
      // Return to main midlet&lt;br /&gt;
      midlet.displayMgr.home();&lt;br /&gt;
    } else if (c == cmBack) {&lt;br /&gt;
      // Pop the last displayable off the stack&lt;br /&gt;
      midlet.displayMgr.popDisplayable();&lt;br /&gt;
    } else if (c == cmHome) {&lt;br /&gt;
      // Return to main midlet&lt;br /&gt;
      midlet.displayMgr.home();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*--------------------------------------------------&lt;br /&gt;
 * Class OptionsList&lt;br /&gt;
 *&lt;br /&gt;
 * List to provide options for configuring of timer&lt;br /&gt;
 *&lt;br /&gt;
 * Example from the book:     Core J2ME Technology&lt;br /&gt;
 * Copyright John W. Muchow   http://www.CoreJ2ME.ru&lt;br /&gt;
 * You may use/modify for any non-commercial purpose &lt;br /&gt;
 *-------------------------------------------------*/&lt;br /&gt;
class OptionsList extends List implements CommandListener {&lt;br /&gt;
  private AnimatedTimer midlet; // Main midlet&lt;br /&gt;
  private Command cmBack;&lt;br /&gt;
  public OptionsList(String title, int listType, AnimatedTimer midlet) {&lt;br /&gt;
    // Call list constructor&lt;br /&gt;
    super(title, listType);&lt;br /&gt;
    // Save reference to MIDlet so we can&lt;br /&gt;
    // access the display manager class&lt;br /&gt;
    this.midlet = midlet;&lt;br /&gt;
    // Create the list entries&lt;br /&gt;
    append(&amp;quot;Sleep interval&amp;quot;, null);&lt;br /&gt;
    append(&amp;quot;Start&amp;quot;, null);&lt;br /&gt;
    append(&amp;quot;Stop&amp;quot;, null);&lt;br /&gt;
    // Create command and listen for events&lt;br /&gt;
    cmBack = new Command(&amp;quot;Back&amp;quot;, Command.BACK, 1);&lt;br /&gt;
    addCommand(cmBack);&lt;br /&gt;
    setCommandListener(this);&lt;br /&gt;
  }&lt;br /&gt;
  /*--------------------------------------------------&lt;br /&gt;
   * Command event handling&lt;br /&gt;
   *-------------------------------------------------*/&lt;br /&gt;
  public void commandAction(Command c, Displayable s) {&lt;br /&gt;
    // Event generated by the implicit list&lt;br /&gt;
    if (c == List.SELECT_COMMAND) {&lt;br /&gt;
      switch (getSelectedIndex()) {&lt;br /&gt;
      case 0:&lt;br /&gt;
        // Push current displayable and show the form&lt;br /&gt;
        // to adjust the timer sleep&lt;br /&gt;
        midlet.displayMgr.pushDisplayable(midlet.fmSleep);&lt;br /&gt;
        break;&lt;br /&gt;
      case 1:&lt;br /&gt;
        // Start timer and return to previous displayable&lt;br /&gt;
        midlet.cvTimer.startTimer();&lt;br /&gt;
        midlet.displayMgr.popDisplayable();&lt;br /&gt;
        break;&lt;br /&gt;
      case 2:&lt;br /&gt;
        // Stop timer and return to previous displayable&lt;br /&gt;
        midlet.cvTimer.stopTimer();&lt;br /&gt;
        midlet.displayMgr.popDisplayable();&lt;br /&gt;
        break;&lt;br /&gt;
      }&lt;br /&gt;
    } else if (c == cmBack) {&lt;br /&gt;
      // Return to previous displayable&lt;br /&gt;
      midlet.displayMgr.popDisplayable();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Animation MIDlet  ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
J2ME in a Nutshell&lt;br /&gt;
By Kim Topley&lt;br /&gt;
ISBN: 0-596-00253-X&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import java.util.Timer;&lt;br /&gt;
import java.util.TimerTask;&lt;br /&gt;
import javax.microedition.lcdui.Canvas;&lt;br /&gt;
import javax.microedition.lcdui.rumand;&lt;br /&gt;
import javax.microedition.lcdui.rumandListener;&lt;br /&gt;
import javax.microedition.lcdui.Display;&lt;br /&gt;
import javax.microedition.lcdui.Displayable;&lt;br /&gt;
import javax.microedition.lcdui.Form;&lt;br /&gt;
import javax.microedition.lcdui.Gauge;&lt;br /&gt;
import javax.microedition.lcdui.Graphics;&lt;br /&gt;
import javax.microedition.lcdui.Item;&lt;br /&gt;
import javax.microedition.lcdui.ItemStateListener;&lt;br /&gt;
import javax.microedition.midlet.MIDlet;&lt;br /&gt;
public class AnimationMIDlet extends MIDlet &lt;br /&gt;
                        implements CommandListener, ItemStateListener {&lt;br /&gt;
    // The MIDlet&amp;quot;s Display object&lt;br /&gt;
    private Display display;&lt;br /&gt;
        &lt;br /&gt;
    // Flag indicating first call of startApp&lt;br /&gt;
    protected boolean started;&lt;br /&gt;
    &lt;br /&gt;
    // Exit command&lt;br /&gt;
    private Command exitCommand;&lt;br /&gt;
    &lt;br /&gt;
    // Setup command&lt;br /&gt;
    private Command setupCommand;&lt;br /&gt;
    &lt;br /&gt;
    // Run command&lt;br /&gt;
    private Command runCommand;&lt;br /&gt;
    &lt;br /&gt;
    // Configuration form&lt;br /&gt;
    private Form form;&lt;br /&gt;
    &lt;br /&gt;
    // Animation canvas&lt;br /&gt;
    private AnimationCanvas canvas;&lt;br /&gt;
    &lt;br /&gt;
    // Gauge for block count&lt;br /&gt;
    private Gauge blockGauge;&lt;br /&gt;
    &lt;br /&gt;
    // Gauge for frame rate&lt;br /&gt;
    private Gauge rateGauge;&lt;br /&gt;
    &lt;br /&gt;
    // Initial frame rate&lt;br /&gt;
    private static final int FRAME_RATE = 1;&lt;br /&gt;
    &lt;br /&gt;
    // Initial number of blocks&lt;br /&gt;
    private static final int BLOCK_COUNT = 1;&lt;br /&gt;
    &lt;br /&gt;
    protected void startApp() {&lt;br /&gt;
        if (!started) {&lt;br /&gt;
            display = Display.getDisplay(this);&lt;br /&gt;
            form = new Form(&amp;quot;Animation&amp;quot;);&lt;br /&gt;
            rateGauge = new Gauge(&amp;quot;Frame rate&amp;quot;, true, 10, FRAME_RATE);&lt;br /&gt;
            blockGauge = new Gauge(&amp;quot;Blocks&amp;quot;, true, 4, BLOCK_COUNT);&lt;br /&gt;
            form.append(rateGauge);&lt;br /&gt;
            form.append(blockGauge);&lt;br /&gt;
            form.setItemStateListener(this);&lt;br /&gt;
            &lt;br /&gt;
            canvas = createAnimationCanvas();            &lt;br /&gt;
            &lt;br /&gt;
            exitCommand = new Command(&amp;quot;Exit&amp;quot;, Command.EXIT, 0);&lt;br /&gt;
            setupCommand = new Command(&amp;quot;Setup&amp;quot;, Command.SCREEN, 0);&lt;br /&gt;
            runCommand = new Command(&amp;quot;Run&amp;quot;, Command.SCREEN, 0);&lt;br /&gt;
            &lt;br /&gt;
            canvas.addCommand(exitCommand);&lt;br /&gt;
            canvas.addCommand(setupCommand);&lt;br /&gt;
            form.addCommand(exitCommand);&lt;br /&gt;
            form.addCommand(runCommand);&lt;br /&gt;
            &lt;br /&gt;
            form.setCommandListener(this);&lt;br /&gt;
            canvas.setCommandListener(this);&lt;br /&gt;
            &lt;br /&gt;
            display.setCurrent(form);&lt;br /&gt;
            started = true;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    protected void pauseApp() {&lt;br /&gt;
    }&lt;br /&gt;
    protected void destroyApp(boolean unconditional) {&lt;br /&gt;
    }    &lt;br /&gt;
    public void commandAction(Command c, Displayable d) {&lt;br /&gt;
        if (c == exitCommand) {&lt;br /&gt;
            // Exit. No need to call destroyApp&lt;br /&gt;
            // because it is empty.&lt;br /&gt;
            notifyDestroyed();&lt;br /&gt;
        } else if (c == runCommand) {&lt;br /&gt;
            display.setCurrent(canvas);&lt;br /&gt;
        } else if (c == setupCommand) {&lt;br /&gt;
            display.setCurrent(form);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    public void itemStateChanged(Item item) {&lt;br /&gt;
        if (item == blockGauge) {&lt;br /&gt;
            int count = blockGauge.getValue();&lt;br /&gt;
            if (count &amp;lt; 1) {&lt;br /&gt;
                count = 1;&lt;br /&gt;
            }&lt;br /&gt;
            canvas.setBlockCount(count);&lt;br /&gt;
        } else if (item == rateGauge) {&lt;br /&gt;
            int count = rateGauge.getValue();&lt;br /&gt;
            if (count &amp;lt; 1) {&lt;br /&gt;
                count = 1;&lt;br /&gt;
            }&lt;br /&gt;
            canvas.setFrameRate(count);&lt;br /&gt;
        }            &lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
    // Creates the canvas that will draw the block&lt;br /&gt;
    protected AnimationCanvas createAnimationCanvas() {&lt;br /&gt;
        return new AnimationCanvas();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class AnimationCanvas extends Canvas {&lt;br /&gt;
        // Size of each block&lt;br /&gt;
        protected static final int SIZE = 4;&lt;br /&gt;
        &lt;br /&gt;
        // Initial speeds in the X direction&lt;br /&gt;
        protected final int[] xSpeeds = { 2, -2, 0, -2 };&lt;br /&gt;
        &lt;br /&gt;
        // Initial speeds in the Y direction&lt;br /&gt;
        protected final int[] ySpeeds = { 2, -2, 2, -0 };&lt;br /&gt;
        &lt;br /&gt;
        // Background color&lt;br /&gt;
        protected int background = display.isColor() ? 0 : 0xc0c0c0;&lt;br /&gt;
            &lt;br /&gt;
        // Foreground color&lt;br /&gt;
        protected int foreground = display.isColor() ? 0xffff00 : 0;&lt;br /&gt;
        &lt;br /&gt;
        // Width of screen&lt;br /&gt;
        protected int width = getWidth();&lt;br /&gt;
        &lt;br /&gt;
        // Height of screen&lt;br /&gt;
        protected int height = getHeight();&lt;br /&gt;
        &lt;br /&gt;
        // The screen update rate&lt;br /&gt;
        protected int frameRate;&lt;br /&gt;
        &lt;br /&gt;
        // The blocks to draw on the screen&lt;br /&gt;
        protected Block[] blocks;&lt;br /&gt;
        &lt;br /&gt;
        // The update timer&lt;br /&gt;
        protected Timer timer;&lt;br /&gt;
        &lt;br /&gt;
        // The update timer task&lt;br /&gt;
        protected TimerTask updateTask;&lt;br /&gt;
        &lt;br /&gt;
        // Gets the maximum number of blocks&lt;br /&gt;
        public int getMaxBlocks() {&lt;br /&gt;
            return blocks.length;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Constructs a canvas with default settings&lt;br /&gt;
        AnimationCanvas() {&lt;br /&gt;
            setBlockCount(BLOCK_COUNT);&lt;br /&gt;
            setFrameRate(FRAME_RATE);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Sets the number of blocks to draw&lt;br /&gt;
        public void setBlockCount(int count) {&lt;br /&gt;
            if (count &amp;gt; xSpeeds.length) {&lt;br /&gt;
                throw new IllegalArgumentException(&amp;quot;Cannot have more than &amp;quot; &lt;br /&gt;
                                + xSpeeds.length + &amp;quot; blocks&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            &lt;br /&gt;
            blocks = new Block[count];&lt;br /&gt;
            createBlocks();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Gets the number of blocks to draw&lt;br /&gt;
        public int getBlockCount() {&lt;br /&gt;
            return blocks.length;&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Sets the number of updates per second&lt;br /&gt;
        public void setFrameRate(int frameRate) {&lt;br /&gt;
            if (frameRate &amp;lt; 1 || frameRate &amp;gt; 10) {&lt;br /&gt;
                throw new IllegalArgumentException(&amp;quot;Frame rate must be &amp;gt; 0 and &amp;lt;= 10&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            this.frameRate = frameRate;&lt;br /&gt;
            if (isShown()) {&lt;br /&gt;
                startFrameTimer();&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Gets the number of updates per second&lt;br /&gt;
        public int getFrameRate() {&lt;br /&gt;
            return frameRate;&lt;br /&gt;
        }  &lt;br /&gt;
          &lt;br /&gt;
        // Paint canvas background and all&lt;br /&gt;
        // of the blocks in their correct locations.&lt;br /&gt;
        protected void paint(Graphics g) {&lt;br /&gt;
            // Paint with the background color&lt;br /&gt;
            g.setColor(background);&lt;br /&gt;
            g.fillRect(0, 0, width, height);&lt;br /&gt;
            &lt;br /&gt;
            // Draw all of the blocks&lt;br /&gt;
            g.setColor(foreground);&lt;br /&gt;
            synchronized (this) {&lt;br /&gt;
                for (int i = 0, count = blocks.length; i &amp;lt; count; i++) {&lt;br /&gt;
                    g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Notification that the canvas has been made visible&lt;br /&gt;
        protected void showNotify() {&lt;br /&gt;
            // Start the frame timer running&lt;br /&gt;
            startFrameTimer();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Notification that the canvas is no longer visible&lt;br /&gt;
        protected void hideNotify() {&lt;br /&gt;
            // Stop the frame timer &lt;br /&gt;
            stopFrameTimer();&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Creates the blocks to be displayed&lt;br /&gt;
        private void createBlocks() {&lt;br /&gt;
            int startX = (width - SIZE)/2;&lt;br /&gt;
            int startY = (height - SIZE)/2;&lt;br /&gt;
            for (int i = 0, count = blocks.length; i &amp;lt; count; i++) {&lt;br /&gt;
                blocks[i] = new Block(startX, startY, xSpeeds[i], ySpeeds[i]);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Starts the frame redraw timer&lt;br /&gt;
        protected void startFrameTimer() {&lt;br /&gt;
            timer = new Timer();&lt;br /&gt;
            &lt;br /&gt;
            updateTask = new TimerTask() {&lt;br /&gt;
                public void run() {&lt;br /&gt;
                    moveAllBlocks();&lt;br /&gt;
                }&lt;br /&gt;
            };&lt;br /&gt;
            long interval = 1000/frameRate;&lt;br /&gt;
            timer.schedule(updateTask, interval, interval);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Stops the frame redraw timer&lt;br /&gt;
        protected void stopFrameTimer() {&lt;br /&gt;
            timer.cancel();            &lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Called on expiry of timer.&lt;br /&gt;
        public synchronized void moveAllBlocks() {&lt;br /&gt;
            // Update the positions and speeds&lt;br /&gt;
            // of all of the blocks&lt;br /&gt;
            for (int i = 0, count = blocks.length; i &amp;lt; count; i++) {&lt;br /&gt;
                blocks[i].move();&lt;br /&gt;
                &lt;br /&gt;
                // Request a repaint of the screen&lt;br /&gt;
                repaint();                &lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        // Inner class used to represent a block on the screen&lt;br /&gt;
        class Block {&lt;br /&gt;
            int x;      // X position&lt;br /&gt;
            int y;      // Y position&lt;br /&gt;
            int xSpeed; // Speed in the X direction&lt;br /&gt;
            int ySpeed; // Speed in the Y direction&lt;br /&gt;
            &lt;br /&gt;
            Block(int x, int y, int xSpeed, int ySpeed) {&lt;br /&gt;
                this.x = x;&lt;br /&gt;
                this.y = y;&lt;br /&gt;
                this.xSpeed = xSpeed;&lt;br /&gt;
                this.ySpeed = ySpeed;&lt;br /&gt;
            }&lt;br /&gt;
            &lt;br /&gt;
            void move() {&lt;br /&gt;
                x += xSpeed;&lt;br /&gt;
                if (x &amp;lt;= 0 || x + SIZE &amp;gt;= width) {&lt;br /&gt;
                    xSpeed = -xSpeed;&lt;br /&gt;
                }&lt;br /&gt;
                &lt;br /&gt;
                y += ySpeed;&lt;br /&gt;
                if (y &amp;lt;= 0 || y + SIZE &amp;gt;= height) {&lt;br /&gt;
                    ySpeed = -ySpeed;&lt;br /&gt;
                }                &lt;br /&gt;
            }            &lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Animation MIDlet 2 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
J2ME in a Nutshell&lt;br /&gt;
By Kim Topley&lt;br /&gt;
ISBN: 0-596-00253-X&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
import javax.microedition.midlet.*;&lt;br /&gt;
import javax.microedition.lcdui.*;&lt;br /&gt;
public class AnimationMIDlet2 extends AnimationMIDlet {&lt;br /&gt;
    // Create an AnimationCanvas2&lt;br /&gt;
    protected AnimationMIDlet.AnimationCanvas createAnimationCanvas() {&lt;br /&gt;
        return new AnimationCanvas2();&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class AnimationCanvas2 extends AnimationMIDlet.AnimationCanvas {&lt;br /&gt;
        // Override superclass paint method to take&lt;br /&gt;
        // into account the clipping rectangle&lt;br /&gt;
        protected void paint(Graphics g) {&lt;br /&gt;
            // Get the clipping rectange&lt;br /&gt;
            int clipX = g.getClipX();&lt;br /&gt;
            int clipY = g.getClipY();&lt;br /&gt;
            int clipWidth = g.getClipWidth();&lt;br /&gt;
            int clipHeight = g.getClipHeight();&lt;br /&gt;
            &lt;br /&gt;
            // Paint with the background color - only&lt;br /&gt;
            // the area within the clipping rectangle&lt;br /&gt;
            g.setColor(background);&lt;br /&gt;
            g.fillRect(clipX, clipY, clipWidth, clipHeight);&lt;br /&gt;
            &lt;br /&gt;
            // Draw all of the blocks&lt;br /&gt;
            g.setColor(foreground);&lt;br /&gt;
            synchronized (this) {&lt;br /&gt;
                for (int i = 0, count = blocks.length; i &amp;lt; count; i++) {&lt;br /&gt;
                    g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE);&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }       &lt;br /&gt;
                &lt;br /&gt;
        // Called on expiry of timer.&lt;br /&gt;
        public synchronized void moveAllBlocks() {&lt;br /&gt;
            // Update the positions and speeds&lt;br /&gt;
            // of all of the blocks and repaint&lt;br /&gt;
            // only the part of the screen that&lt;br /&gt;
            // they occupy&lt;br /&gt;
            for (int i = 0, count = blocks.length; i &amp;lt; count; i++) {&lt;br /&gt;
                // Request a repaint of the current location&lt;br /&gt;
                Block block = blocks[i];&lt;br /&gt;
                repaint(block.x, block.y, SIZE, SIZE);&lt;br /&gt;
                &lt;br /&gt;
                blocks[i].move();&lt;br /&gt;
                &lt;br /&gt;
                // Request a repaint of the new location&lt;br /&gt;
                repaint(block.x, block.y, SIZE, SIZE);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>