Timer使用实例
2006-02-08 14:50
330 查看
/*
*TimerMidlet.java
*/
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class TimerMidlet extends MIDlet {
private TimerCanvas canvas;
public TimerMidlet() {
canvas=new TimerCanvas();
Display.getDisplay(this).setCurrent(canvas);
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}
/*
*MyTimerTask.java
*/
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
private TimerCanvas c;
public MyTimerTask(TimerCanvas c) {
this.c=c;
}
public void run() {
c.addSec();
}
}
/*
*TimerCanvas.java
*/
import java.util.Timer;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
public class TimerCanvas extends Canvas implements CommandListener {
private long timelong;
private int screenWidth;
private int screenHeight;
private Timer timer=null;
private Command start;
private Command pause;
public TimerCanvas() {
init();
}
private void init(){
timelong=0;
screenWidth=getWidth();
screenHeight=getHeight();
start=new Command("Start",Command.SCREEN,1);
pause=new Command("Pause",Command.SCREEN,1);
addCommand(start);
setCommandListener(this);
}
public void doStartTimer(){
if(timer==null){
MyTimerTask task=new MyTimerTask(this);
timer=new Timer();
timer.schedule(task,1000,1000);
}
}
public void doStopTimer(){
if(timer!=null){
timer.cancel();
timer=null;
}
}
protected void paint(Graphics g) {
// 将背景以白色清除
g.setColor(0x00FFFFFF);
g.fillRect(0, 0, screenWidth, screenHeight);
g.setColor(0x00FF00FF);
int hour=(int)(timelong/3600);
int minute=(int)((timelong-(hour*3600))/60);
int second=(int)(timelong-(hour*3600)-(minute*60));
String hourSt=String.valueOf(hour);
hourSt=(hourSt.length()<2?"0"+hourSt:hourSt);
String minuteSt=String.valueOf(minute);
minuteSt=(minuteSt.length()<2?"0"+minuteSt:minuteSt);
String secondSt=String.valueOf(second);
secondSt=(secondSt.length()<2?"0"+secondSt:secondSt);
String drawSt=hourSt+":"+minuteSt+":"+secondSt;
g.drawString(drawSt,10,10,Graphics.TOP|Graphics.LEFT);
}
public void addSec(){
timelong++;
repaint();
}
public void commandAction(Command cmd, Displayable arg1) {
if(cmd==start){
removeCommand(start);
addCommand(pause);
doStartTimer();
}else if(cmd==pause){
removeCommand(pause);
addCommand(start);
doStopTimer();
}
}
}
*TimerMidlet.java
*/
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class TimerMidlet extends MIDlet {
private TimerCanvas canvas;
public TimerMidlet() {
canvas=new TimerCanvas();
Display.getDisplay(this).setCurrent(canvas);
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}
/*
*MyTimerTask.java
*/
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
private TimerCanvas c;
public MyTimerTask(TimerCanvas c) {
this.c=c;
}
public void run() {
c.addSec();
}
}
/*
*TimerCanvas.java
*/
import java.util.Timer;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
public class TimerCanvas extends Canvas implements CommandListener {
private long timelong;
private int screenWidth;
private int screenHeight;
private Timer timer=null;
private Command start;
private Command pause;
public TimerCanvas() {
init();
}
private void init(){
timelong=0;
screenWidth=getWidth();
screenHeight=getHeight();
start=new Command("Start",Command.SCREEN,1);
pause=new Command("Pause",Command.SCREEN,1);
addCommand(start);
setCommandListener(this);
}
public void doStartTimer(){
if(timer==null){
MyTimerTask task=new MyTimerTask(this);
timer=new Timer();
timer.schedule(task,1000,1000);
}
}
public void doStopTimer(){
if(timer!=null){
timer.cancel();
timer=null;
}
}
protected void paint(Graphics g) {
// 将背景以白色清除
g.setColor(0x00FFFFFF);
g.fillRect(0, 0, screenWidth, screenHeight);
g.setColor(0x00FF00FF);
int hour=(int)(timelong/3600);
int minute=(int)((timelong-(hour*3600))/60);
int second=(int)(timelong-(hour*3600)-(minute*60));
String hourSt=String.valueOf(hour);
hourSt=(hourSt.length()<2?"0"+hourSt:hourSt);
String minuteSt=String.valueOf(minute);
minuteSt=(minuteSt.length()<2?"0"+minuteSt:minuteSt);
String secondSt=String.valueOf(second);
secondSt=(secondSt.length()<2?"0"+secondSt:secondSt);
String drawSt=hourSt+":"+minuteSt+":"+secondSt;
g.drawString(drawSt,10,10,Graphics.TOP|Graphics.LEFT);
}
public void addSec(){
timelong++;
repaint();
}
public void commandAction(Command cmd, Displayable arg1) {
if(cmd==start){
removeCommand(start);
addCommand(pause);
doStartTimer();
}else if(cmd==pause){
removeCommand(pause);
addCommand(start);
doStopTimer();
}
}
}
相关文章推荐
- java中的定时器Timer的使用及实例
- C#创建windows服务搭配定时器Timer使用实例
- C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)
- 自娱自乐10之Linux DMA使用2(DMA使用实例,用timer作DMA请求源实现流水灯)
- 使用Timer类的两个实例 动态时钟
- C#中的Timer和DispatcherTimer使用实例
- java.util.Timer的使用实例
- java--timer--task定时器使用实例
- Java 定时器(Timer)及线程池里使用定时器实例代码
- C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)(从iteye搬到博客园)
- [C#].NET中几种Timer的使用实例
- 关于Java Web 使用Java的TimerTask, 触发定时器(执行某些任务)的实例
- java中的定时器Timer的使用及实例
- 官方NotePad实例学习--读取数据Cursor的使用
- oracle 存储过程使用实例(含事物和异常处理)
- PHP Memcache的安装使用及与mysql交互实例
- python使用clear方法清除字典内全部数据实例
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- 在Html中使用Requirejs进行模块化开发实例详解
- Apache组件commons的Codec包的使用实例