?? timerdemo.java
字號:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class TimerDemo extends MIDlet {
Display display; //設備的顯示器
NumberCanvas numCanvas = new NumberCanvas(); //繪制數字的Canvas對象
NumberMovie mov = new NumberMovie();
Timer timer = new Timer(); //定時器
public TimerDemo() {}
protected void startApp() {
display = Display.getDisplay( this ); //獲取設備的顯示器
display.setCurrent( numCanvas ); //設置當前的繪圖對象
timer.schedule( mov, 2, 2 ); //設備定時器的運行時間
}
protected void pauseApp() {}
protected void destroyApp( boolean u) {}
public void exit(){
timer.cancel(); //退出定時器
destroyApp( true );
notifyDestroyed();
}
class NumberMovie extends TimerTask {
public void run(){
numCanvas.scroll(); //滾動屏幕
}
}
class NumberCanvas extends Canvas {
int height;
int width;
int[] stars; //繪制數字的坐標點
Random random = new Random();
int ran; //用于生成0或1的隨機數
public NumberCanvas(){
height = getHeight();
width = getWidth();
stars = new int[ height ];
for( int i = 0; i < height; ++i ){
stars[i] = -1;
}
}
public void scroll() {
for( int i = height-1; i > 0; --i ){
stars[i] = stars[i-1];
}
stars[0] = random.nextInt() % width ; //生成繪制數字的坐標點
if( stars[0] >= width ){
stars[0] = -1;
}
repaint(); //重繪屏幕
}
protected void paint( Graphics g ){
g.setColor( 0, 0, 0 ); //設置當前繪圖顏色為黑色
g.fillRect( 0, 0, width, height ); //填充背景
g.setColor( 255, 255, 255 ); //設置當前繪圖顏色為白色
for( int y = 0; y < height; y++ ){
int x = stars[y];
if( x == -1 ) continue;
ran=random.nextInt()%2; //生成-1到1的隨機數
if (ran<0)
ran=1;
g.drawString(Integer.toString(ran),x,y,Graphics.BOTTOM|Graphics.LEFT); //繪制數字
}
}
protected void keypressed( int keycode ){
exit(); //退出程序
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -