- 在学习j2me的过程中,可能很多学员对屏幕缓冲理解方面有很多的误解,其实,屏幕缓冲简单的可以说成:就是使用自己创建的屏幕画笔在创建画笔的屏幕上作画,然后再将画出的屏幕做为Image的对象画到改屏幕上,这样就解决了屏幕闪烁的问题:
- 屏幕缓冲的代码贴上以供大家理解:
在学习j2me的过程中,可能很多学员对屏幕缓冲理解方面有很多的误解,其实,屏幕缓冲简单的可以说成:就是使用自己创建的屏幕画笔在创建画笔的屏幕上作画,然后再将画出的屏幕做为Image的对象画到改屏幕上,这样就解决了屏幕闪烁的问题:
现将屏幕缓冲的代码贴上以供大家理解:
- package com.redarmy.tes;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- public class ExCanvas extends Canvas{
- public Image offScreen;//离屏缓冲区
- public Graphics offg; //离屏画笔
- public ExCanvas() {
- //绘制和屏幕一样大小的缓冲区
- offScreen = Image.createImage(this.getWidth(), this.getHeight());
- //得到离屏缓冲区的画笔
- offg = offScreen.getGraphics();
- }
- protected void paint(Graphics arg0) {
- //绘制离屏缓冲区
- arg0.drawImage(offScreen, 0, 0, Graphics.LEFT|Graphics.TOP);
- }
- }
- //以上内容均有本人亲自撰写,不要随意转载。如有问题请发送邮件到;RedArmy.Chen@gmail.com
http://developers.sun.com/mobility/midp/articles/gameapi/(这是原文参考)
MIDP applications often use double-buffering to make animation smoother. In this approach, you do not draw directly to the display, but instead to a copy of the display, an off-screen buffer that's maintained in memory. When you are done drawing to the buffer, you then copy its contents to the display. The rationale here is that drawing using primitives takes longer than a single memory-copy operation; the same drawing operations are performed, but in the background. The user sees only the net change in the display each time, so the animation flows more smoothly. To implement double-buffering, first create a mutable image the same size as the screen: ...
int width = getWidth();
int height = getHeight();
Image buffer = Image.createImage(width, height);
...
Then obtain a graphics context for the buffer: ...
Graphics g = buffer.getGraphics();
...
Now draw to the buffer: ...
// animate
// ...
g.drawRexr(20, 20, 25, 30);
...
When you need to copy the buffer to the screen, you can override the paint() method to draw the buffer to the device's display: public void paint(Graphics g)
g.drawImage(buffer, 0, 0, 0);
}