📄 stopwatchcanvas.java
字号:
import javax.microedition.lcdui.*;
public class StopWatchCanvas extends Canvas implements CommandListener, Runnable {
StopWatch watch;
long results[];
boolean running;
boolean suspended;
long startTime;
long suspendTime;
int curr;
int screenWidth;
int screenHeight;
Font font;
Font underlinedFont;
Object dummy;
static final int WHITE = 0xFFFFFF;
static final int BLACK = 0;
static final int REFRESH_RATE = 200;
StopWatchCanvas(StopWatch watch) {
this.watch = watch;
screenWidth = getWidth();
screenHeight = getHeight();
font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
underlinedFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD|Font.STYLE_UNDERLINED, Font.SIZE_LARGE);
setCommandListener(this);
addCommand(StopWatch.QUIT_CMD);
addCommand(StopWatch.START_CMD);
addCommand(StopWatch.HELP_CMD);
Display.getDisplay(watch).setCurrent(this);
dummy = new Object();
results = new long[10];
curr = 1;
(new Thread(this)).start();
}
public void run() {
try {
while (true) {
synchronized (dummy) {
dummy.wait(REFRESH_RATE);
}
if (running) {
repaint();
}
}
} catch (InterruptedException x) {}
}
void appendDigits(StringBuffer buf, int val, int width) {
String s = Integer.toString(val);
int len = s.length();
while (len < width) {
buf.append('0');
len += 1;
}
buf.append(s);
}
protected void paint(Graphics g) {
g.setColor(BLACK);
g.fillRect(0, 0, screenWidth, screenHeight);
g.setColor(WHITE);
if (suspended) {
g.drawString(Locale.SUSPENDED, screenWidth/2, screenHeight/2, Graphics.HCENTER|Graphics.TOP);
} else {
g.setFont(underlinedFont);
g.drawString(Integer.toString(curr), screenWidth/2, 0, Graphics.HCENTER|Graphics.TOP);
g.setFont(font);
long elapsed = running ? System.currentTimeMillis() - startTime : results[curr];
StringBuffer buf = new StringBuffer();
int hours = (int)(elapsed / (60*60*1000));
int minutes = (int)(elapsed / (60*1000) % 60);
int seconds = (int)(elapsed / 1000 % 60);
int fraction = (int)(elapsed % 1000);
appendDigits(buf, hours, 2);
buf.append(':');
appendDigits(buf, minutes, 2);
buf.append(':');
appendDigits(buf, seconds, 2);
buf.append('.');
appendDigits(buf, fraction, 3);
g.drawString(buf.toString(), screenWidth/2, screenHeight/2, Graphics.HCENTER|Graphics.TOP);
}
}
public void keyPressed(int keyCode)
{
int action;
switch (keyCode)
{
case KEY_NUM0:
if (!running) {
start();
}
break;
case KEY_NUM1:
case KEY_NUM2:
case KEY_NUM3:
case KEY_NUM4:
case KEY_NUM5:
case KEY_NUM6:
case KEY_NUM7:
case KEY_NUM8:
case KEY_NUM9:
if (running) {
results[keyCode - KEY_NUM0] = System.currentTimeMillis() - startTime;
curr = keyCode - KEY_NUM0;
if (curr < 9) {
curr += 1;
repaint();
}
} else {
curr = keyCode - KEY_NUM0;
repaint();
}
break;
case KEY_POUND:
if (running) {
if (suspended) {
resume();
} else {
suspend();
}
repaint();
} else {
start();
}
break;
case KEY_STAR:
if (!running) {
start();
} else {
stop();
}
break;
default:
action = getGameAction(keyCode);
switch (action) {
case LEFT:
case RIGHT:
case UP:
case DOWN:
case FIRE:
if (running) {
results[curr] = System.currentTimeMillis() - startTime;
if (curr < 9) {
curr += 1;
repaint();
}
} else {
if (action == LEFT || action == UP) {
if (curr > 1) {
curr -= 1;
repaint();
}
} else {
if (curr < 9) {
curr += 1;
repaint();
}
}
}
}
}
}
void start() {
startTime = System.currentTimeMillis();
for (int i = 0; i < results.length; i++) {
results[i] = 0;
}
running = true;
removeCommand(StopWatch.START_CMD);
addCommand(StopWatch.STOP_CMD);
addCommand(StopWatch.SUSPEND_CMD);
curr = 1;
repaint();
}
void stop() {
results[curr] = (suspended ? suspendTime : System.currentTimeMillis()) - startTime;
running = false;
suspended = false;
removeCommand(StopWatch.STOP_CMD);
removeCommand(StopWatch.SUSPEND_CMD);
removeCommand(StopWatch.RESUME_CMD);
addCommand(StopWatch.START_CMD);
curr = 1;
repaint();
}
void suspend() {
suspendTime = System.currentTimeMillis();
suspended = true;
removeCommand(StopWatch.SUSPEND_CMD);
addCommand(StopWatch.RESUME_CMD);
repaint();
}
void resume() {
startTime += System.currentTimeMillis() - suspendTime;
suspended = false;
removeCommand(StopWatch.RESUME_CMD);
addCommand(StopWatch.SUSPEND_CMD);
}
public void commandAction(Command c, Displayable d) {
if (c == StopWatch.QUIT_CMD) {
watch.destroyApp(true);
watch.notifyDestroyed();
} else if (c == StopWatch.START_CMD) {
start();
} else if (c == StopWatch.STOP_CMD) {
stop();
} else if (c == StopWatch.SUSPEND_CMD) {
suspend();
} else if (c == StopWatch.RESUME_CMD) {
resume();
} else if (c == StopWatch.HELP_CMD) {
new HelpForm(watch);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -