J2ME实现精确秒表计时

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
 
 
public class WatchCanvas extends Canvas implements Runnable{
	/**存储几个时间*/
	private static final int NUM = 5;
	/**起始时间*/
	private long startTime;
	/**结束时间*/
	private long endTime;
	/**经过的时间*/
	private long passTime;
	/**字体大小*/
	private int fontSize;
	/**是否停表*/
	private boolean isStop;
	/**是否准备好重置*/
	private boolean isReady;
	/**时间字符串*/
	private String strTime;
	/**小时*/
	private String strHour;
	/**分钟*/
	private String strMin;
	/**秒*/
	private String strSec;
	/**毫秒*/
	private String strMsec;
	/**字体*/
	private Font font;
	/**时间存储数组*/
	private long time[];
 
	public WatchCanvas(){
		this.setFullScreenMode(true);
		reset();
	}
 
	protected void paint(Graphics g) {
		g.setColor(~0);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		g.setColor(0);
		//绘制当前读秒
		strTime = toStringTime(passTime);
		g.drawString(strTime, this.getWidth()/2-strTime.length()*fontSize/2, 0, Graphics.TOP|Graphics.LEFT);
		//绘制存储时间
		for(int i = 0 ; i  < time.length ; i++){
			if(time[i]!=0){
				strTime = toStringTime(time[i]);
				g.drawString(strTime, this.getWidth()/2-strTime.length()*fontSize/2, (i+1)*(font.getHeight()+3), Graphics.TOP|Graphics.LEFT);
			}else{
				break;
			}
		}
	}
	/**
	 * 时间重置
	 * */
	private void reset(){
		passTime = 0;
		isStop = false;
		font = Font.getDefaultFont();
		fontSize = font.charWidth('0');
		time = new long[NUM];
		isReady = false;
		repaint();
	}
	/**
	 * 时间字符串化,不够位数填0补充
	 * @param time
	 * 		——时间
	 * @return
	 * 		——时间字符串
	 */
	private String toStringTime(long time){
		long temp;
		temp = time%1000;
		if(temp == 0){
			strMsec = "000";
		}else if(temp < 10){
			strMsec = "00"+Long.toString(temp);
		}else if(temp < 100){
			strMsec = "0"+Long.toString(temp);
		}else{
			strMsec = Long.toString(temp);
		}
 
		temp = time/1000%60;
		if(temp == 0){
			strSec = "00";
		}else if(temp < 10){
			strSec = "0"+Long.toString(temp);
		}else{
			strSec = Long.toString(temp);
		}
 
		temp = time/60/1000%60;
		if(temp == 0){
			strMin = "00";
		}else if(temp < 10){
			strMin = "0"+Long.toString(temp);
		}else{
			strMin = Long.toString(temp);
		}
 
		temp = time/60/1000/60%60;
		if(temp == 0){
			strHour = "00";
		}else if(temp < 10){
			strHour = "0"+Long.toString(temp);
		}else{
			strHour = Long.toString(temp);
		}
		return strHour + " : " + strMin + " : " + strSec + "." + strMsec;
	}
	/**
	 * 存储时间
	 * @param times
	 * 		——时间
	 */
	private void saveTime(long times){
		for(int i = time.length-2; i >= 0; i--){
			time[i+1] = time[i];
		}
		time[0] = times;
	}
 
	protected void keyPressed(int keyCode){
		int action = getGameAction(keyCode);
		if(keyCode == Canvas.KEY_NUM5 && !isStop){
			if(!isReady){
				new Thread(this).start();
				isReady = true;
			}else{
				saveTime(passTime);
				if(time[time.length-1]!=0){
					isStop = true;
					isReady = false;
				}
			}
		}
		if(action == Canvas.FIRE && !isReady){
			reset();
		}
	}
 
	public void run() {
		startTime = System.currentTimeMillis();
		while(!isStop){
			if(!isStop){
				endTime = System.currentTimeMillis();
				passTime = endTime-startTime;
				repaint();
				serviceRepaints();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
 
}
阅读更多
想对作者说点什么? 我来说一句

Android毫秒级秒表(带秒针动画)

2016年05月31日 19.17MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭