您的位置:首页 > 其它

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();
  }
 }
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息