|
import Javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*;
public class TimerDemo extends MIDlet {
Display display; StarField field = new StarField(); FieldMover mover = new FieldMover(); Timer timer = new Timer();
public TimerDemo() { display = Display.getDisplay( this ); }
protected void destroyApp( boolean unconditional ) { }
protected void startApp() { display.setCurrent( field ); timer.schedule( mover, 100, 100 ); }
protected void pauseApp() { }
public void exit(){ timer.cancel(); // stop scrolling destroyApp( true ); notifyDestroyed(); }
class FieldMover extends TimerTask { public void run(){ field.scroll(); } }
class StarField extends Canvas { int height; int width; int[] stars; Random generator = new Random(); boolean painting = false;
public StarField(){ height = getHeight(); width = getWidth(); stars = new int[ height ];
for( int i = 0; i < height; ++i ){ stars[i] = -1; } }
public void scroll() { if( painting ) return;
for( int i = height-1; i > 0; --i ){
上一篇:从自定义字节数组创建图片
下一篇:一个图形用户界面绘画和事件处理的案例
|