饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機開發網

             :: 首頁 :: 聯系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks

                雙緩沖技術的應用很廣泛,設計游戲的時候更是需要它,

               在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. }  






          爬蟲工作室 -- 專業的手機軟件開發工作室
          3G視線 -- 專注手機軟件開發
          posted on 2006-12-30 09:24 3G工作室 閱讀(89) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 哈巴河县| 光泽县| 安丘市| 隆安县| 阜康市| 济宁市| 崇信县| 花莲县| 襄垣县| 麟游县| 名山县| 云安县| 临清市| 沿河| 玉龙| 邹城市| 布尔津县| 扶绥县| 普陀区| 黄平县| 大连市| 成武县| 仁布县| 南京市| 眉山市| 德钦县| 鄂伦春自治旗| 罗城| 苏尼特左旗| 湘乡市| 新化县| 钦州市| 宝丰县| 鄢陵县| 新安县| 沈阳市| 晋中市| 贵德县| 陆河县| 东平县| 唐山市|