动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
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,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ J2EE/J2ME教程 ]的信息

本月文章推荐
.基于MIDP1.0实现RMS容量探测器.
.EnterpriseJavaBeans导论一.
.J2ME入门-(1)J2ME概述.
.Apusic应用服务器和CORBA互操作.
.J2EE Web开发技术期待一次新的技.
.RMS概念解析与使用指南.
.BlueTooth探索系列(四)--服务发现.
.J2ME简介.
.JDO2.0的查询语言新特性.
.推荐2本学J2ME的书~(新手必看).
.搭建OTA下载服务器.
.J2EE程序中的SQL语句自动构造方法.
.RMS从入门到精通之三.
.如何给Log4j配上数据库连接池.
.基于J2EE的Blog平台.
.介绍MIDP应用程序的属性.
.使用EclipseRCP的IBMWorkplaceMa.
.KVM简介和编译.
.SpringWebFlow:4(SpringMVC和Web.
.用JNI调用C或C++动态联接库入门.

时钟计时器

发表日期: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();
    }
  }
}

时钟计时器

(出处:http://www.knowsky.com)


上一篇:类似卫星扫描效果 人气:1244
下一篇:通过计时器做一个简单的活动画面事例 人气:716
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-12-1 MyBB v1.4.4 简体中文版 bulid 2
2008-12-1 新云网站内容管理系统 v4.0.0.11
2008-12-1 网趣网上购物系统时尚版 v8.8
2008-12-1 Textpattern v4.0.7 多国语言版
2008-12-1 Piwik ( PHP统计系统,可以和GOOG
2008-12-1 天空网络电影系统SKYUC! v2.6.2
2008-12-1 SiteDynamic企业网站管理系统 v1
2008-12-1 KindEditor HTML在线编辑器 v3.0
2008-12-1 0451sky高校教务管理系统2008 v4
2008-11-29 Tencent Traveler 4.4
2008-11-29 龙卷风网络收音机 v3.0.0.0
2008-11-29 Intel Chipset Software Install
2008-11-29 TweakVI 1.0 Build 1100
2008-11-29 Opera 9.62 Build 10469
2008-11-29 MPlayer WW编译版 SVN-r28044(20
2008-11-29 NetTools网络工具v1.0.0破解版
2008-11-29 3DGallery三维体验1.1破解版
2008-11-29 SecretBook保密本v1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | OPEN参数生成器 | 弹出式窗口代码产生器 | 密码登录生成器 | 在线按钮生成器 | Meta标签生成器 | 多色彩特效字代码生成器 | 网页代码调试器 | 在线FTP登陆 | Flash取色器 | 配色代码对照表 | 配色辞典 | CSS生成器 | 广告代码 | 框架网页代码生成器 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | 在线调色板 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 元素周期表 | 健康查询 | 世界时间 | 万年历 | 二十四节气 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2009 www.knowsky.com All rights reserved | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 中国語教室 ホームページ作成