bufferimage.java

来自「《精通JAVA手机游戏与应用程序设计》随书光盘」· Java 代码 · 共 64 行

JAVA
64
字号
/*
 * 双缓存显示图片
 */

import java.io.IOException;

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


public class bufferImage extends MIDlet implements CommandListener {

	private Command exitCommand;
	private bufferImageCanvas sg;

	public bufferImage() {
		exitCommand = new Command("Exit", Command.EXIT, 1);
		sg = new bufferImageCanvas();
		sg.addCommand(exitCommand);
		sg.setCommandListener(this);
		Display.getDisplay(this).setCurrent(sg);
		
	}
	protected void startApp(){
	}

	protected void pauseApp() {
	}

	protected void destroyApp(boolean arg0){		
	}
	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {			
			destroyApp(false);			
			notifyDestroyed();
		}
	}	
}

class bufferImageCanvas extends Canvas{
	Image source; //缓冲区要绘画的图像
	Image copy;//缓冲区对象
	protected bufferImageCanvas(){
		try {
			source = Image.createImage("/house.png");
			//指定缓冲区域为屏幕大小
			copy =Image.createImage(this.getWidth(),this.getHeight());
			Graphics g = copy.getGraphics();// 获得一个新的Graphics对象
			
			g.drawImage(source,this.getWidth()/2,this.getHeight()/2,
					Graphics.BOTTOM|Graphics.HCENTER);
			
		} catch (IOException e) {			
			e.printStackTrace();
		}					
		
	}
	protected void paint(Graphics g) {		
		//显示缓存区的可变Image对象
		g.drawImage(copy,0,0,Graphics.TOP|Graphics.LEFT);
		
	}
	
}

⌨️ 快捷键说明

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