路是爬出來的

          關于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. }  


          posted on 2006-12-30 09:24 路是爬出來的 閱讀(297) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 天等县| 莆田市| 周宁县| 汉中市| 石屏县| 蓬莱市| 聂拉木县| 玉环县| 乌拉特中旗| 从江县| 天峻县| 咸宁市| 黑水县| 丹江口市| 旬阳县| 开封市| 手游| 河池市| 灵武市| 宿松县| 沙坪坝区| 鹤壁市| 仲巴县| 辛集市| 上虞市| 兴业县| 达拉特旗| 鹤壁市| 桂林市| 黔西县| 嘉义县| 和顺县| 东港市| 鸡东县| 舒兰市| 彩票| 保亭| 金堂县| 望城县| 安义县| 吕梁市|