J2ME中Canvas双缓冲方法(同为屏幕内容转换Image对象方法)

Midlet Code

package zeph.midlet; import javax.microedition.lcdui.Display; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class DoubleBuffer extends MIDlet { private Display display = null; private DoubleBufferCanvas canvas; public DoubleBuffer(){ display = Display.getDisplay(this); canvas = new DoubleBufferCanvas(); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub display.setCurrent(canvas); } }  

Canvas Code

package zeph.midlet; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; public class DoubleBufferCanvas extends Canvas { private int width; private int height; private Image offImage = null; public DoubleBufferCanvas() { width = getWidth(); height = getHeight(); offImage = Image.createImage(width, height);// 创建缓冲图片 Graphics buffered = null; if (offImage != null) buffered = offImage.getGraphics();// 缓冲图片获得画笔 buffered.setColor(0, 0, 0);// 设置画笔颜色 buffered.fillRoundRect((width - 100) / 2, (height - 60) / 2, 100, 60, 5, 3);//绘画一个圆角矩形 } protected void paint(Graphics g) {//在真实屏幕上绘画出刚才的缓冲图片 g.drawImage(offImage, this.getWidth() / 2, this.getHeight() / 2, Graphics.VCENTER | Graphics.HCENTER); } public Image getoffImage() {//获得屏幕中所画的图像 return offImage; } }  

你可能感兴趣的:(J2ME中Canvas双缓冲方法(同为屏幕内容转换Image对象方法))