timermidlet.java.svn-base

来自「example2 众多JAVA实例源码...学习java基础的好帮手」· SVN-BASE 代码 · 共 91 行

SVN-BASE
91
字号
package opusmicro.demos.animate;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class TimerMIDlet extends MIDlet {
	Display display;
	StarField field = new StarField();
	FieldMover mover = new FieldMover();
	Timer timer = new Timer();

	public TimerMIDlet() {
		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;
			}
//			stars[1] = 80;
		}

		public void scroll() {
			if ( painting) return;
			for ( int i = height - 1 ; i > 0 ; --i) {
				stars[i] = stars[i - 1];
			}
			stars[0] = (generator.nextInt() % (3 * width)) / 2;
//			if ( stars[0] >= width) {
//				stars[0] = -1;
//			}
			repaint();
		}

		protected void paint(Graphics g) {
			painting = true;
			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];
//				System.out.print(" " + x + "(" + y + ")");
				if ( x == -1) continue;
				//g.drawLine(x, y, x, y);
				g.fillRect(x, y, 1, 1);
			}
			System.out.println();
			painting = false;
		}

		protected void keyPressed(int keyCode) {
			exit();
		}
		protected void keyRepeated(int keyCode){}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?