上一篇文章介绍了一个简单的秒表程序,是使用J2ME提供的StringItem开发的,简单,但是不够美观,下面贴一个使用Canvas编写的秒表程序,就当做一个进阶的例子吧。
package midp.wallimn.com;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
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.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/** *//**
* 秒表程序的高级版本,使用Canvas,外观稍好。 <br/>
*
* @version : V1.0<br/>
* @author : (Email: [email protected] QQ: 54871876)<br/>
* @date : 2008-1-14 下午01:37:28<br/>
*/
public class StopWatchMIDlet extends MIDlet implements CommandListener ...{
private MyCanvas mainForm;
private Timer m_timer;// 定时器
private Display display;
private Command EXIT_CMD = new Command("退出", Command.EXIT, 1);
private Command STOP_CMD = new Command("停止", Command.STOP, 1);
private Command START_CMD = new Command("开始", Command.STOP, 1);
private MyTimerTask timerTask;
class MyTimerTask extends TimerTask ...{
public int s = 0;// 秒
public int m = 0;// 分
public int h = 0;// 时
public int ms = 0;// 1/100秒
public MyTimerTask() ...{
}
public void init() ...{
s = 0;
m = 0;
h = 0;
ms = 0;
}
public void run() ...{
if (ms == 9) ...{
ms = 0;
if (s == 59) ...{
s = 0;
if (m == 59) ...{
m = 0;
h++;
} else ...{
m++;
}
} else ...{
s++;
}
} else ...{
ms++;
}
mainForm.repaint();
}
}
public StopWatchMIDlet() ...{
display = Display.getDisplay(this);
mainForm = new MyCanvas();
mainForm.addCommand(EXIT_CMD);// 添加命令显示
mainForm.addCommand(START_CMD);// 添加命令显示
mainForm.setCommandListener(this);// 添加事件监听
}
protected void destroyApp(boolean arg0) ...{
}
protected void pauseApp() ...{
}
protected void startApp() throws MIDletStateChangeException ...{
display.setCurrent(mainForm);
}
public void commandAction(Command c, Displayable s) ...{
if (c == EXIT_CMD) ...{
if (m_timer != null) ...{// 停止计时器,释放资源
m_timer.cancel();
m_timer = null;
}
destroyApp(false);
notifyDestroyed();
} else if (c == STOP_CMD) ...{
this.m_timer.cancel();
m_timer = null;
mainForm.removeCommand(STOP_CMD);
mainForm.addCommand(START_CMD);
} else if (c == START_CMD) ...{
// 初始化一个计时器,初始化一个任务
m_timer = new Timer();
timerTask = null;// 先释放旧的任务
timerTask = new MyTimerTask();
timerTask.init();
m_timer.schedule(timerTask, new Date(), 100);
mainForm.removeCommand(START_CMD);
mainForm.addCommand(STOP_CMD);
}
}
/** *//**
* 日历显示输出 <br/>
*
* @version : V1.0<br/>
* @author : wallimn(Email: [email protected] QQ: 54871876)<br/>
* @date : 2008-1-22 下午04:19:51<br/>
*/
class MyCanvas extends Canvas ...{
int m_width, m_height;
private Image numberImage = null;
/** *//**
* 数字的宽度
*/
private final int numWidth = 11;
/** *//**
* 数字的高度
*/
private final int numHeight = 15;
/** *//**
* 冒号的宽度
*/
private final int colonWidth = 7;
private int xPos, yPos;
/** *//**
* 数字的Y坐标
*/
private int numStyle = 15;
public MyCanvas() ...{
try ...{
numberImage = Image.createImage("/images/misc.png");
} catch (Exception e) ...{
return;
}
// 计算数字显示位置,以便使数字居中显示
xPos = (this.getWidth() - numWidth * 9) / 2;
yPos = (this.getHeight() - numHeight) / 2;
}
protected void paint(Graphics g) ...{
g.setColor(0xFFFFFF);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
drawMyFlag(g);
int h = 0, m = 0, s = 0, ms = 0;
if (timerTask != null) ...{
h = timerTask.h;
m = timerTask.m;
s = timerTask.s;
ms = timerTask.ms;
}
int x = xPos;
drawNumber(g, x, yPos, h / 10, numStyle);
x += numWidth;
drawNumber(g, x, yPos, h % 10, numStyle);
x += numWidth;
drawNumber(g, x, yPos, 10, numStyle);
x += colonWidth;
drawNumber(g, x, yPos, m / 10, numStyle);
x += numWidth;
drawNumber(g, x, yPos, m % 10, numStyle);
x += numWidth;
drawNumber(g, x, yPos, 10, numStyle);
x += colonWidth;
drawNumber(g, x, yPos, s / 10, numStyle);
x += numWidth;
drawNumber(g, x, yPos, s % 10, numStyle);
x += numWidth;
drawNumber(g, x, yPos, 10, numStyle);
x += colonWidth;
drawNumber(g, x, yPos, ms, numStyle);
}
/** *//**
* 输出图形格式的数字,这种方式利于数字的多样化 <br/> 编码:wallimn 时间:2008-1-22 下午04:22:55<br/>
*/
private void drawNumber(Graphics g, int x, int y, int num, int ypos) ...{
int wid = numWidth;
if(num==10)wid =colonWidth;
g.drawRegion(numberImage, num * numWidth, ypos, wid,
numHeight, Sprite.TRANS_NONE, x, y, Graphics.HCENTER
| Graphics.VCENTER);
}
private void drawMyFlag(Graphics g)...{
g.drawRegion(numberImage, 0, 32, 118, 22, Sprite.TRANS_NONE, 0, 0, Graphics.TOP
| Graphics.LEFT);
}
protected void showNotify() ...{
}
protected void hideNotify() ...{
}
/** *//**
* 按钮处理 功能描述: 编码时间:2008-1-22 下午04:23:14 参数及返回值:
*
* @param kc
* @see javax.microedition.lcdui.Canvas#keyPressed(int)
*/
protected void keyPressed(int kc) ...{
int ga = this.getGameAction(kc);
if (ga == Canvas.FIRE || kc == Canvas.KEY_NUM5) ...{
if (numStyle == 0) ...{
numStyle = numHeight;
} else if (numStyle == numHeight) ...{
numStyle = 0;
}
repaint();
}
}
protected void keyReleased(int arg0) ...{
// 时间: 2008-1-22 上午10:40:00
}
}
}
/***********本人原创,欢迎转载,转载请保留本人信息*************/
作者:wallimn 电邮:
[email protected] 时间:2008-02-13
博客:http://blog.csdn.net/wallimn http://wallimn.iteye.com
网络硬盘:http://wallimn.ys168.com
/***********文章发表请与本人联系,作者保留所有权利*************/