路是爬出来的

[导入]关于j2me game双缓冲实现探讨

      双缓冲技术的应用很广泛,设计游戏的时候更是需要它,

     在midp1.0中,api中并没有game这个包,看到网上很多人在讨论设计游戏的时候会出现图片断裂,屏幕闪烁等问题。

     我经过这几天的学习整理下自己的学习心得,用来抛砖,希望对此有研究高手们相互讨论。让我也学习学习。

    

     双缓冲的原理可以这样形象的理解:把电脑屏幕看作一块黑板。首先我们在内存环境中建立一个“虚拟“的黑板,然后在这块黑板上绘制复杂的图形,等图形全部绘 制完毕的时候,再一次性的把内存中绘制好的图形“拷贝”到另一块黑板(屏幕)上。采取这种方法可以提高绘图速度,极大的改善绘图效果。

    对于手机来说。具体的过程就是通过extends Canvas。然后获取bufferImage。再然后就getGraphics。最后就是在这个graphics中绘制图片等,再最后就是把这个绘制好的bufferImage绘制的屏幕上。

     说归说。具体还是要看代码的。里面的代码参照了一些开源的代码。

java 代码


 


  1. /******************************************************************** 

  2.  * 项目名称             :足球项目j2me客户端         

     

  3.  *  

  4.  * Copyright 2005-2006 Teesoo. All rights reserved 

  5.  ********************************************************************/  

  6. package org.wuhua.game;  

  7.   

  8. import javax.microedition.lcdui.Canvas;  

  9. import javax.microedition.lcdui.Graphics;  

  10. import javax.microedition.lcdui.Image;  

  11.   



  12.  

  13.   

  14. /** 

  15.  * 类名:GameCanvas.java 

     编写日期: 2006-11-29 

     程序功能描述:
     

  16.  * 实现双缓冲的Game画布。实现原理是创建一个BufferImage。然后绘制,最后显示出来。就这么简单。

     Demo: 

     Bug:
     

  17.  * 

     

  18.  *  

  19.  * 程序变更日期 :

     变更作者 :

     变更说明 :

     

  20.  *  

  21.  * @author wuhua 

     
     

  22.  */  

  23. public abstract class GameCanvas extends Canvas {  

  24.   

  25.     /** 

  26.      * 绘制缓冲的图片。用户绘制资源的时候都是操作这个图片来进行的 

  27.      */  

  28.     private Image bufferImage;  

  29.   

  30.     private int height;  

  31.   

  32.     private int width;  

  33.   

  34.     private int clipX, clipY, clipWidth, clipHeight;  

  35.   

  36.     private boolean setClip;  

  37.   

  38.     protected GameCanvas() {  

  39.   

  40.         super();  

  41.   

  42.         width = getWidth();  

  43.         height = getHeight();  

  44.   

  45.         this.bufferImage = Image.createImage(width, height);  

  46.   

  47.     }  

  48.   

  49.     protected void paint(Graphics g) {  

  50.         //如果要求绘制指定区域的话就需要这样了  

  51.         if (this.setClip) {  

  52.             g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);  

  53.             this.setClip = false;  

  54.         }  

  55.         g.drawImage(this.bufferImage, 00, Graphics.TOP | Graphics.LEFT);  

  56.   

  57.     }  

  58.   

  59.     public void flushGraphics(int x, int y, int width, int height) {  

  60.         this.setClip = true;  

  61.         this.clipX = x;  

  62.         this.clipY = y;  

  63.         this.clipWidth = width;  

  64.         this.clipHeight = height;  

  65.   

  66.         repaint();  

  67.         serviceRepaints();  

  68.     }  

  69.   

  70.     public void flushGraphics() {  

  71.         repaint();  

  72.         serviceRepaints();  

  73.     }  

  74.   

  75.     /** 

  76.      * 设计者主要是通过调用这个方法获取图片。然后就可以绘制了 

  77.      * @return 

  78.      */  

  79.     protected Graphics getGraphics() {  

  80.         return this.bufferImage.getGraphics();  

  81.     }  

  82.   

  83.     /** 

  84.      * 这个方法主要是处理Nokia平台,用户调用setFullScreenMode(boolean enable) 时重新按照新的w & h创建缓冲图片 

  85.      */  

  86.     protected final void sizeChanged(int w, int h) {  

  87.         if (h > height) {  

  88.             this.bufferImage = Image.createImage(w, h);  

  89.         }  

  90.     }  

  91. }  



文章来源: http://wuhua.javaeye.com/blog/36520

posted on 2006-12-30 08:42 路是爬出来的 阅读(62) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: