关于j2me game双缓冲实现探讨
时间:
2015-09-25 15:04:14
阅读:
432
评论:
收藏:
0
[点我收藏+]
双缓冲技术的应用很广泛,设计游戏的时候更是需要它,
在midp1.0中,api中并没有game这个包,看到网上很多人在讨论设计游戏的时候会出现图片断裂,屏幕闪烁等问题。
我经过这几天的学习整理下自己的学习心得,用来抛砖,希望对此有研究高手们相互讨论。让我也学习学习。
双缓冲的原理可以这样形象的理解:把电脑屏幕看作一块黑板。首先我们在内存环境中建立一个“虚拟“的黑板,然后在这块黑板上绘制复杂的图形,等图形全部绘 制完毕的时候,再一次性的把内存中绘制好的图形“拷贝”到另一块黑板(屏幕)上。采取这种方法可以提高绘图速度,极大的改善绘图效果。
对于手机来说。具体的过程就是通过extends Canvas。然后获取bufferImage。再然后就getGraphics。最后就是在这个graphics中绘制图片等,再最后就是把这个绘制好的bufferImage绘制的屏幕上。
说归说。具体还是要看代码的。里面的代码参照了一些开源的代码。
java 代码
-
-
-
-
-
-
package org.wuhua.game;
-
-
import javax.microedition.lcdui.Canvas;
-
import javax.microedition.lcdui.Graphics;
-
import javax.microedition.lcdui.Image;
-
-
-
-
-
-
-
-
-
-
-
-
-
public abstract class GameCanvas extends Canvas {
-
-
-
-
-
private Image bufferImage;
-
-
private int height;
-
-
private int width;
-
-
private int clipX, clipY, clipWidth, clipHeight;
-
-
private boolean setClip;
-
-
protected GameCanvas() {
-
-
super();
-
-
width = getWidth();
-
height = getHeight();
-
-
this.bufferImage = Image.createImage(width, height);
-
-
}
-
-
protected void paint(Graphics g) {
-
-
if (this.setClip) {
-
g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);
-
this.setClip = false;
-
}
-
g.drawImage(this.bufferImage, 0, 0, Graphics.TOP | Graphics.LEFT);
-
-
}
-
-
public void flushGraphics(int x, int y, int width, int height) {
-
this.setClip = true;
-
this.clipX = x;
-
this.clipY = y;
-
this.clipWidth = width;
-
this.clipHeight = height;
-
-
repaint();
-
serviceRepaints();
-
}
-
-
public void flushGraphics() {
-
repaint();
-
serviceRepaints();
-
}
-
-
-
-
-
-
protected Graphics getGraphics() {
-
return this.bufferImage.getGraphics();
-
}
-
-
-
-
-
protected final void sizeChanged(int w, int h) {
-
if (h > height) {
-
this.bufferImage = Image.createImage(w, h);
-
}
-
}
-
}
关于j2me game双缓冲实现探讨