苦于E63没有秒表,网上找的不好用,就自己写个简单的
苦于E63没有秒表,网上找的不好用,就自己写个简单的
两个类
- package com.winder;
- import java.io.IOException;
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- public class StopTimeMain extends MIDlet implements CommandListener {
- private Display display; // 引用MIDlet的Display 对象
- private Command cmdExit; // 设定按钮用于退出MIDlet
- private Command cmdStart;
- private MainCanvas cv ;
- private String[] times = {"00:00:00.000","","","",""};
- // MIDlet构造程序
- public StopTimeMain() {
- display = Display.getDisplay(this);
- cmdExit = new Command("Exit", Command.OK, 1);
- cmdStart = new Command("Start", Command.CANCEL, 1);
- cv = new MainCanvas();
- String s[] = new String[5];
- cv.setStopTimes(times);
- cv.addCommand(cmdExit);
- cv.addCommand(cmdStart);
- cv.setCommandListener(this);
- try{
- Image[] ims = new Image[12];
- for(int i = 0; i < 12; i++){
- ims[i] = Image.createImage("/"+i+".png");
- }
- cv.setImages(ims);
- }catch(IOException e){}
- }
- public void startApp() {
- display.setCurrent(cv);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- /**
- * 按钮处理
- */
- public void commandAction(Command c, Displayable s) {
- String cmdLabel = c.getLabel();
- if (cmdLabel.equals(cmdExit.getLabel())) {
- destroyApp(false);
- notifyDestroyed();
- }else if(cmdLabel.equals(cmdStart.getLabel())){
- if(!cv.getTime()){
- if( !"00:00:00.000".equals(times[0])){
- int len = times.length - 1;
- for(int i = 0; i < len ; i++){
- times[len - i] = times[len - i - 1];
- }
- times[0] = "00:00:00.000";
- }
- }
- boolean ret = cv.setTime();
- if(ret){
- Thread t = new Thread(cv);
- t.start();
- cv.removeCommand(cmdStart);
- cmdStart = new Command("Stop", Command.CANCEL, 1);
- cv.addCommand(cmdStart);
- }else{
- cv.removeCommand(cmdStart);
- cmdStart = new Command("Start", Command.CANCEL, 1);
- cv.addCommand(cmdStart);
- }
- }
- }
- }
第二个
- package com.winder;
- import java.io.IOException;
- import java.util.Date;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.DateField;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- public class MainCanvas extends Canvas implements Runnable {
- private String[] stopTimes;
- private boolean time = false;
- private long longTime = 0;
- private Image images[] = new Image[0];
- public void setImages(Image[] i){
- this.images = i;
- }
- public synchronized void setStopTimes(String[] stopTimes){
- this.stopTimes = stopTimes;
- }
- public synchronized String[] getStopTimes(){
- return stopTimes;
- }
- public synchronized boolean setTime(){
- this.time = !time;
- return time;
- }
- public synchronized boolean getTime(){
- return time;
- }
- protected void paint(Graphics g) {
- Font font=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,Font.SIZE_LARGE);
- g.setFont(font);
- int len = 0;
- int width = this.getWidth();
- int height = this.getHeight();
- g.setColor(0x000000);
- g.fillRect(0, 0, width, height);
- g.setColor(0xffffff);
- String stimes[] = getStopTimes();
- if(stimes != null ) len = stimes.length;
- int y = height/2 - (len * 30 +10)/2 ;
- for(int i = 0; i < len; i++){
- String text = stimes[i];
- if(text == null || text.equals("")) continue;
- int length = text.length();
- int[] num = new int[length];
- int x = width/2 - text.length()/2;
- x = x - length * 13 /2;
- System.out.println("text=" + text );
- for(int j = 0; j < length; j++){
- String str = text.substring(j, j + 1);
- if(":".equals(str)){
- this.drawNum(g, 10 , x, y);
- }else if(".".equals(str)){
- this.drawNum(g, 11 , x, y);
- }else{
- num[j] = Integer.parseInt(str);
- this.drawNum(g, Integer.parseInt(str) , x, y);
- }
- x = x + 13;
- }
- y = y + 30;
- }
- }
- private void drawNum(Graphics g,int n,int x,int y) {
- if(images == null || images.length < n || images[n] == null) return;
- g.drawImage(images[n], x, y,Graphics.LEFT|Graphics.TOP);
- }
- public void run() {
- if(time && stopTimes != null){
- longTime = System.currentTimeMillis();
- while(this.getTime()){
- long mill = System.currentTimeMillis() - longTime;
- String str = "";
- str = "" + mill % 1000;
- long h = mill / 1000 / 60 / 60;
- long m = mill / 1000 / 60 %60;
- long mi = mill / 1000 % 60;
- long ss = mill % 1000;
- String sss = "";
- if(ss < 10){
- sss = "00" + ss;
- }else if(ss < 100){
- sss = "0" + ss;
- }else{
- sss = "" + ss;
- }
- str = "" + ((h < 10)? "0" : "") + h + ":" + (m < 10?"0":"") + m + ":" +(mi < 10?"0":"") + mi+ "." + sss ;
- stopTimes[0] = str;
- repaint();
- this.serviceRepaints();
- try{
- Thread.sleep(10);
- }catch(InterruptedException e){}
- }
- }
- }
- }