网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.使用J2ME实现移动支付.
.会话EJB完整开发过程(以weblogi.
.JNDI 常见问题.
.Spring中的持久性.
.基于MIDP1.0实现RMS容量探测器.
.MIDlet应用程序模型基础.
.J2ME中使用 Canvas 制作简单的游.
.精通Hibernate映射继承关系之四.
.谈谈J2ME解决手机Game的内存问题.
.关于手机的IMEI号问题的一些总结.
.介绍J2ME的几个重要概念.
.使用EasyMock使单元测试更加容易.
.Java中关于XML的API一瞥.
.hibernate源码浅析(net.sf.hibe.
.J2ME可选包—PIM介绍.
.EnterpriseJavaBeans导论三.
.基于Hibernate和Struts的J2EE应用.
.Axis2 中的工具wsdl2Java 的使用.
.Servlet容器工作原理(四).
.轻松维护RDF元数据模型.

时钟计时器

发表日期:2007-12-23


*--------------------------------------------------
 * AnimatedTimer - Main midlet.
 * Shows canvas with an animated timer. Includes
 * configuration options to start/stop the timer
 * and to adjust the sleep interval of the thread
 *
 * Example from the book:     Core J2ME Technology
 * Copyright John W. MUChow   http://www.CoreJ2ME.com
 * You may use/modify for any non-commercial purpose 
 *-------------------------------------------------*/

import Java.util.Stack;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;

public class AnimatedTimer extends MIDlet {
  private Display display; // The display

  protected TimerCanvas cvTimer; // Canvas to display timer

  protected OptionsList lsOptions; // List to change timer options

  protected SleepForm fmSleep; // Form with gauge to set timer sleep

  protected DisplayManager displayMgr; // Class to help manage screens

  public AnimatedTimer() {
    display = Display.getDisplay(this);
    cvTimer = new TimerCanvas(this);
    lsOptions = new OptionsList("Timer options", List.IMPLICIT, this);
    fmSleep = new SleepForm("Adjust sleep", this);

    // Create a display manager object
    displayMgr = new DisplayManager(display, cvTimer);
  }

  protected void startApp() {
    // Start with the canvas
    display.setCurrent(cvTimer);
  }

  protected void pauseApp() {
  }

  protected void destroyApp(boolean unconditional) {
  }

  public void exitMIDlet() {
    destroyApp(true);
    notifyDestroyed();
  }
}

/*--------------------------------------------------
 * Use a stack to push and pop displayable objects
 *
 * public void pushDisplayable(Displayable)
 * public void popDisplayable()
 * public void home()  
 *
 * Example from the book:     Core J2ME Technology
 * Copyright John W. Muchow   http://www.CoreJ2ME.com
 * You may use/modify for any non-commercial purpose 
 *-------------------------------------------------*/

class DisplayManager extends Stack {
  private Display display; // Reference to Display object

  private Displayable mainDisplayable; // Main displayable for MIDlet

  private Alert alStackError; // Alert for error conditions

  /*--------------------------------------------------
   * Display manager constructor
   *-------------------------------------------------*/
  public DisplayManager(Display display, Displayable mainDisplayable) {
    // Only one display object per midlet, this is it
    this.display = display;
    this.mainDisplayable = mainDisplayable;

    // Create an alert displayed when an error occurs
    alStackError = new Alert("Displayable Stack Error");
    alStackError.setTimeout(Alert.FOREVER); // Modal
  }

  /*--------------------------------------------------
   * Push the current displayable onto stack and set
   * the passed in displayable as active
   *-------------------------------------------------*/
  public void pushDisplayable(Displayable newDisplayable) {
    push(display.getCurrent());
    display.setCurrent(newDisplayable);
  }

  /*--------------------------------------------------
   * Return to the main displayable object of MIDlet
   *-------------------------------------------------*/
  public void home() {
    while (elementCount > 1)
      pop();
    display.setCurrent(mainDisplayable);
  }

  /*--------------------------------------------------
   * Pop displayable from stack and set as active
   *-------------------------------------------------*/
  public void popDisplayable() {
    // If the stack is not empty, pop next displayable
    if (empty() == false)
      display.setCurrent((Displayable) pop());
    else
      // On error show an alert
      // Once acknowledged, set 'mainDisplayable' as active
      display.setCurrent(alStackError, mainDisplayable);
  }
}

/*--------------------------------------------------
 * Class TimerCanvas
 *
 * Animate a sequence of images to simulate 
 * a moving timer
 *
 * Example from the book:     Core J2ME Technology
 * Copyright John W. Muchow   http://www.CoreJ2ME.com
 * You may use/modify for any non-commercial purpose 
 *-------------------------------------------------*/

class TimerCanvas extends Canvas implements Runnable, CommandListener {
  private AnimatedTimer midlet; // Main midlet

  private Command cmExit; // Exit midlet

  private Command cmOptions; // Display options list

  private Image im = null; // Sequence of images

  private int imageCount = 4; // Four images in the sequence

  private int imageWidth; // Width of one image in the sequence

  private int imageHeight; // Height of one image in the sequence

  private int imageIndex; // Current image in the sequence

  private int translate_x; // Translated x and y

  private int translate_y;

  private int viewport_x; // Location of the viewport

  private int viewport_y;

  private boolean active = false; // Timer active?

  private boolean requestedToStop = false; // Did user request to stop timer

  private int sleepTime = 400; // Current sleep time (milliseconds)

  public TimerCanvas(AnimatedTimer midlet) {
    // Call canvas constructor
    super();

    // Save reference to MIDlet so we can
    // Access the display manager class
    this.midlet = midlet;

    // Create commands & listen for events
    cmExit = new Command("Exit", Command.EXIT, 1);
    cmOptions = new Command("Config", Command.SCREEN, 2);
    addCommand(cmExit);
    addCommand(cmOptions);
    setCommandListener(this);
  }

  /*--------------------------------------------------
   * Application manager is about to display canvas
   *-------------------------------------------------*/
  protected void showNotify() {
    if (im == null) {
      try {
        // Read the png from a file and get width and
        // height of one image in the sequence
        im = Image.createImage("/timer.png");
        imageHeight = im.getHeight();
        imageWidth = im.getWidth() / imageCount;

        // Get the coordinates for x/y of viewport
        // Viewport is centered on the display
        viewport_x = (getWidth() / 2) - (imageWidth / 2);
        viewport_y = (getHeight() / 2) - (imageHeight / 2);

        // Set first translated coordinate to match viewport
        translate_x = viewport_x;
        translate_y = viewport_y;
      } catch (Exception e) {
        System.err.println("Unable to read png file.");
      }

      // Begin with the first image in the sequence
      imageIndex = 0;
    }

    // If the user has not requested to stop the timer...
    if (!requestedToStop)
      active = true;

    new Thread(this).start();
  }

  /*--------------------------------------------------
   * Application manager is no longer displaying canvas
   *-------------------------------------------------*/
  protected void hideNotify() {
    active = false;
  }

  /*--------------------------------------------------
   * Draw next timer in sequence
   *-------------------------------------------------*/
  protected void paint(Graphics g) {
    if (im != null) {
      // Due to a bug in MIDP 1.0.3 we need to
      // force a clear of the display
      g.setColor(255, 255, 255); // White pen
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(0, 0, 0); // Black pen

      // Viewport at center of display
      g.setClip(viewport_x, viewport_y, imageWidth, imageHeight);

      // Draw image at translated coordinates
      g.drawImage(im, translate_x, translate_y, Graphics.TOP
           Graphics.LEFT);
    }
  }

  /*--------------------------------------------------
   * Loop forever, translating image coordinates
   *-------------------------------------------------*/
  public void run() {
    try {
      while (active) {
        Thread.sleep(sleepTime);
        repaint();

        // Reached the last image in sequence
        if (imageIndex == imageCount - 1) {
          // Reset translated coordinates
          translate_x = viewport_x;
          translate_y = viewport_y;
        } else {
          // Translate coordinate system to the left
          translate_x -= imageWidth;
        }

        // Which image in the sequence is next
        imageIndex = (imageIndex + 1) % imageCount;
      }
    } catch (InterruptedException e) {
    }
  }

  /*--------------------------------------------------
   * Called from the "Config" options menu
   *-------------------------------------------------*/
  public void startTimer() {
    requestedToStop = false;
    active = true;
    repaint();
  }

  /*--------------------------------------------------
   * Called from the "Config" options menu
   *-------------------------------------------------*/
  public void stopTimer() {
    requestedToStop = true;
    active = false;
    repaint();
  }

  /*--------------------------------------------------
   * Called from form/gauge to adjust sleep
   *-------------------------------------------------*/
  public void setSleep(int sleepTime) {
    this.sleepTime = sleepTime;
  }

  /*--------------------------------------------------
   * Called from form/gauge to adjust sleep
   *-------------------------------------------------*/
  public int getSleep() {
    return sleepTime;
  }

  /*--------------------------------------------------
   * Command event handling
   *-------------------------------------------------*/
  public void commandAction(Command c, Displayable s) {
    if (c == cmOptions) {
      // Push current displayable and show the options list
      midlet.displayMgr.pushDisplayable(midlet.lsOptions);
    } else if (c == cmExit) {
      midlet.exitMIDlet();
    }
  }
}

/*--------------------------------------------------
 * Class SleepForm
 *
 * Form with gauge to adjust sleep interval of timer
 *
 * Example from the book:     Core J2ME Technology
 * Copyright John W. Muchow   http://www.CoreJ2ME.com
 * You may use/modify for any non-commercial purpose 
 *-------------------------------------------------*/

class SleepForm extends Form implements CommandListener {
  private AnimatedTimer midlet; // Main midlet

  private Command cmBack, // Back to options list
      cmHome, // Go to main displayable (canvas)
      cmSave; // Save new sleep time

  private Gauge gaSleep; // Gauge to adjust sleep

  public SleepForm(String title, AnimatedTimer midlet) {
    // Call the form constructor
    super(title);

    // Save reference to MIDlet so we can
    // access the display manager class
    this.midlet = midlet;

    // Commands
    cmSave = new Command("Save", Command.SCREEN, 1);
    cmBack = new Command("Back", Command.BACK, 2);
    cmHome = new Command("Home", Command.SCREEN, 2);

    // Gauge to adjust the length of timer sleep
    gaSleep = new Gauge("Timer Sleep", true, 100, 1000);

    // Set to current sleep. Gauge holds values 0 to 100,
    // divide the current sleep (milliseconds) by 10
    gaSleep.setValue(midlet.cvTimer.getSleep() / 10);

    // Add to form and listen for events
    append(gaSleep);
    addCommand(cmSave);
    addCommand(cmBack);
    addCommand(cmHome);
    setCommandListener(this);
  }

  /*--------------------------------------------------
   * Command event handling
   *-------------------------------------------------*/
  public void commandAction(Command c, Displayable s) {
    if (c == cmSave) {
      // Gauge returns a value between 0 and 100
      // We want milliseconds, so multiply by 10
      midlet.cvTimer.setSleep(gaSleep.getValue() * 10);

      // Return to main midlet
      midlet.displayMgr.home();

    } else if (c == cmBack) {
      // Pop the last displayable off the stack
      midlet.displayMgr.popDisplayable();
    } else if (c == cmHome) {
      // Return to main midlet
      midlet.displayMgr.home();
    }
  }
}

/*--------------------------------------------------
 * Class OptionsList
 *
 * List to provide options for configuring of timer
 *
 * Example from the book:     Core J2ME Technology
 * Copyright John W. Muchow   http://www.CoreJ2ME.com
 * You may use/modify for any non-commercial purpose 
 *-------------------------------------------------*/

class OptionsList extends List implements CommandListener {
  private AnimatedTimer midlet; // Main midlet

  private Command cmBack;

  public OptionsList(String title, int listType, AnimatedTimer midlet) {
    // Call list constructor
    super(title, listType);

    // Save reference to MIDlet so we can
    // access the display manager class
    this.midlet = midlet;

    // Create the list entries
    append("Sleep interval", null);
    append("Start", null);
    append("Stop", null);

    // Create command and listen for events
    cmBack = new Command("Back", Command.BACK, 1);
    addCommand(cmBack);
    setCommandListener(this);
  }

  /*--------------------------------------------------
   * Command event handling
   *-------------------------------------------------*/
  public void commandAction(Command c, Displayable s) {
    // Event generated by the implicit list
    if (c == List.SELECT_COMMAND) {
      switch (getSelectedIndex()) {
      case 0:
        // Push current displayable and show the form
        // to adjust the timer sleep
        midlet.displayMgr.pushDisplayable(midlet.fmSleep);
        break;

      case 1:
        // Start timer and return to previous displayable
        midlet.cvTimer.startTimer();
        midlet.displayMgr.popDisplayable();
        break;

      case 2:
        // Stop timer and return to previous displayable
        midlet.cvTimer.stopTimer();
        midlet.displayMgr.popDisplayable();
        break;
      }
    } else if (c == cmBack) {
      // Return to previous displayable
      midlet.displayMgr.popDisplayable();
    }
  }
}

时钟计时器

(出处:)


上一篇:类似卫星扫描效果 人气:1318
下一篇:通过计时器做一个简单的活动画面事例 人气:761
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐