路是爬出來的

          關(guān)于j2me game雙緩沖實(shí)現(xiàn)探討

                雙緩沖技術(shù)的應(yīng)用很廣泛,設(shè)計(jì)游戲的時(shí)候更是需要它,

               在midp1.0中,api中并沒有g(shù)ame這個(gè)包,看到網(wǎng)上很多人在討論設(shè)計(jì)游戲的時(shí)候會(huì)出現(xiàn)圖片斷裂,屏幕閃爍等問題。

               我經(jīng)過這幾天的學(xué)習(xí)整理下自己的學(xué)習(xí)心得,用來拋磚,希望對(duì)此有研究高手們相互討論。讓我也學(xué)習(xí)學(xué)習(xí)。

              

               雙緩沖的原理可以這樣形象的理解:把電腦屏幕看作一塊黑板。首先我們?cè)趦?nèi)存環(huán)境中建立一個(gè)“虛擬“的黑板,然后在這塊黑板上繪制復(fù)雜的圖形,等圖形全部繪 制完畢的時(shí)候,再一次性的把內(nèi)存中繪制好的圖形“拷貝”到另一塊黑板(屏幕)上。采取這種方法可以提高繪圖速度,極大的改善繪圖效果。

              對(duì)于手機(jī)來說。具體的過程就是通過extends Canvas。然后獲取bufferImage。再然后就getGraphics。最后就是在這個(gè)graphics中繪制圖片等,再最后就是把這個(gè)繪制好的bufferImage繪制的屏幕上。

               說歸說。具體還是要看代碼的。里面的代碼參照了一些開源的代碼。

          java 代碼


           


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

          2.  * 項(xiàng)目名稱             :足球項(xiàng)目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.  * 實(shí)現(xiàn)雙緩沖的Game畫布。實(shí)現(xiàn)原理是創(chuàng)建一個(gè)BufferImage。然后繪制,最后顯示出來。就這么簡(jiǎn)單。

             Demo: 

             Bug:
             

          17.  * 

             

          18.  *  

          19.  * 程序變更日期 :

             變更作者 :

             變更說明 :

             

          20.  *  

          21.  * @author wuhua 

             
             

          22.  */  

          23. public abstract class GameCanvas extends Canvas {  

          24.   

          25.     /** 

          26.      * 繪制緩沖的圖片。用戶繪制資源的時(shí)候都是操作這個(gè)圖片來進(jìn)行的 

          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.         //如果要求繪制指定區(qū)域的話就需要這樣了  

          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.      * 設(shè)計(jì)者主要是通過調(diào)用這個(gè)方法獲取圖片。然后就可以繪制了 

          77.      * @return 

          78.      */  

          79.     protected Graphics getGraphics() {  

          80.         return this.bufferImage.getGraphics();  

          81.     }  

          82.   

          83.     /** 

          84.      * 這個(gè)方法主要是處理Nokia平臺(tái),用戶調(diào)用setFullScreenMode(boolean enable) 時(shí)重新按照新的w & h創(chuàng)建緩沖圖片 

          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 路是爬出來的 閱讀(301) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 邓州市| 绍兴县| 鲁山县| 大连市| 光山县| 体育| 正阳县| 普陀区| 梓潼县| 肃宁县| 龙岩市| 宜阳县| 内黄县| 涪陵区| 丰都县| 三亚市| 吴旗县| 龙山县| 错那县| 林州市| 牡丹江市| 高阳县| 丹寨县| 青田县| 海晏县| 大邑县| 绥中县| 乌鲁木齐市| 阿尔山市| 平陆县| 武冈市| 光泽县| 习水县| 容城县| 大丰市| 深圳市| 安岳县| 马公市| 乐亭县| 西城区| 灌云县|