代码如下:
/*
* TestMidlet.java
*/
package com.ceun.timer;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class TestMidlet extends MIDlet {
private TimerCanvas canvas;
public TestMidlet() {
canvas=new TimerCanvas(this);
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(canvas);
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
void exit(){
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
}
/*
* TimerCanvas.java
*/
package com.ceun.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.Font;
import javax.microedition.lcdui.Graphics;
public class TimerCanvas extends Canvas implements CommandListener, Runnable {
private boolean isPause;
/**
* a screen dimension.
*/
static int CORNER_X;
/**
* a screen dimension.
*/
static int CORNER_Y;
/**
* a screen dimension.
*/
static int DISP_WIDTH;
/**
* a screen dimension.
*/
static int DISP_HEIGHT;
/**
* a font dimension.
*/
static int FONT_HEIGHT;
/**
* the default font.
*/
static Font FONT;
/**
* a font dimension.
*/
//static int SCORE_WIDTH;
/**
* The width of the string that displays the time,
* saved for placement of time display.
*/
static int TIME_WIDTH;
/**
* How many ticks we start with.
*/
int myInitialGameTicks = 950;
/**
* this is saved to determine if the time string needs
* to be recomputed.
*/
int myOldGameTicks = myInitialGameTicks;
/**
* the number of game ticks that have passed.
*/
int myGameTicks = myOldGameTicks;
/**
* whether or not this has been painted once.
*/
boolean myInitialized;
/**
* we save the time string to avoid recreating it
* unnecessarily.
*/
static String myInitialString = "1:00";
/**
* we save the time string to avoid recreating it
* unnecessarily.
*/
String myTimeString = myInitialString;
private Command start=new Command("Start",Command.SCREEN,1);
private Command pause=new Command("Pause",Command.SCREEN,1);
private Command exit=new Command("Exit",Command.EXIT,5);
private TestMidlet midlet;
public TimerCanvas(TestMidlet midlet) {
this.midlet=midlet;
addCommand(start);
addCommand(exit);
setCommandListener(this);
isPause=true;
}
protected void paint(Graphics g) {
if(!myInitialized) {
CORNER_X = g.getClipX();
CORNER_Y = g.getClipY();
DISP_WIDTH = g.getClipWidth();
DISP_HEIGHT = g.getClipHeight();
FONT = g.getFont();
FONT_HEIGHT = FONT.getHeight();
//SCORE_WIDTH = FONT.stringWidth("Score: 000");
TIME_WIDTH = FONT.stringWidth("Time: " + myInitialString);
myInitialized = true;
}
// clear the screen:
g.setColor(0xffffff);
g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
g.setColor(0);
g.drawString("Time: " + formatTime(),
(DISP_WIDTH - TIME_WIDTH)/2,
CORNER_Y + FONT_HEIGHT, g.TOP|g.LEFT);
}
public void commandAction(Command cmd, Displayable arg1) {
if(cmd==start){
removeCommand(start);
addCommand(pause);
startTimer();
}else if(cmd==pause){
removeCommand(pause);
addCommand(start);
stopTimer();
}else if(cmd==exit){
midlet.exit();
}
}
public void run() {
while(!isPause){
myGameTicks--;
// paint the display
try {
repaint();
} catch(Exception e) {
}
// we do a very short pause to allow the other thread
// to update the information about which keys are pressed:
synchronized(this) {
try {
wait(1);
} catch(Exception e) {}
}
}
}
public void startTimer(){
isPause=false;
new Thread(this).start();
}
public void stopTimer(){
isPause=true;
}
/**
* a simple utility to make the number of ticks look like a time...
*/
public String formatTime() {
if((myGameTicks / 16) + 1 != myOldGameTicks) {
myTimeString = "";
myOldGameTicks = (myGameTicks / 16) + 1;
int smallPart = myOldGameTicks % 60;
int bigPart = myOldGameTicks / 60;
myTimeString += bigPart + ":";
if(smallPart / 10 < 1) {
myTimeString += "0";
}
myTimeString += smallPart;
}
return(myTimeString);
}
}
2077

被折叠的 条评论
为什么被折叠?



