⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animatorcanvas.java

📁 J2ME 无线通信技术应用开发源代码
💻 JAVA
字号:
//AnimatorCanvas.java,项目Animator
//用线程实现动画
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class AnimatorCanvas extends Canvas implements Runnable {
	Image[] frames;
	int left, top;
	boolean alive;
	int interval;
	int currentFrame;
	int numFrames;
	MIDlet midlet;
	Thread thread;

	public AnimatorCanvas(MIDlet midlet, Image[] frames, int left, int top, int interval) {
		this.frames = frames;
		this.left = left;
		this.top = top;
		this.interval = interval;
		this.alive = true;
		this.currentFrame = 0;
		this.numFrames = frames.length;
		this.midlet = midlet;
		thread= new Thread(this);
		thread.start();
	}
	
	public AnimatorCanvas(MIDlet midlet, Image[] frames, int interval) {
		this(midlet, frames, 0, 0, interval );
		this.left = ( this.getWidth() - frames[0].getWidth() ) / 2;
		this.top = ( this.getHeight() - frames[0].getHeight() ) / 2;
	}

	public void paint(Graphics g)	{
		g.setColor(0x00000000);
		g.fillRect(0,0,getWidth(),getHeight());
		g.drawImage(frames[currentFrame++], left, top, Graphics.LEFT | Graphics.TOP);
		if (currentFrame >= numFrames) {
			currentFrame = 0;
		}
	}

	public void keyPressed(int keyCode)	{
		if (getGameAction(keyCode) == FIRE) {
      alive = !alive;
    } else {
    	thread = null;
    	((Animator)midlet).exit();
    }
	}

	public void run() {
		//while(true) {
			if (alive) {
				repaint();
				try {
					Thread.sleep(interval);
				}	catch (InterruptedException e) {
				}
			//}
		}
				Display.getDisplay(midlet).callSerially(this);
	}
}

⌨️ 快捷键说明

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