自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用NetBeans平臺(tái)開發(fā)J2ME游戲?qū)嵗v解

開發(fā) 后端
本文詳細(xì)介紹了用NetBeans平臺(tái)開發(fā)J2ME游戲?qū)嵗?,首先必須安裝NetBeans IDE 4.0 和 NetBeans Mobility Pack 4.0然后才能繼續(xù)開發(fā)。

 1. 必須先安裝 NetBeans IDE 4.0 和 NetBeans Mobility Pack 4.0,然后才能開始進(jìn)行 J2ME MIDP 開發(fā)。有關(guān)下載和安裝完整環(huán)境的說明,請(qǐng)參見 J2ME MIDP 開發(fā)下載頁面http://www.netbeans.org/kb/articles/mobility_zh_CN.html。

2. 創(chuàng)建 MIDP 應(yīng)用程序 創(chuàng)建新的 J2ME MIDP 項(xiàng)目
  
2. 創(chuàng)建新的移動(dòng)應(yīng)用程序:
  
(1).選擇“文件”>“新建項(xiàng)目”(Ctrl-Shift-N)。在“類別”下選擇“移動(dòng)”。在“項(xiàng)目”下選擇“移動(dòng)應(yīng)用程序”,然后單擊“ 下一步”。
  
(2). 在“項(xiàng)目名稱”下輸入 HuaRongDao。將項(xiàng)目主目錄更改為您系統(tǒng)上的任何目錄。從現(xiàn)在起,我們將該目錄稱為 $PROJECTHOME。
  
(3). 不要選中“創(chuàng)建 HelloMIDlet”復(fù)選框。單擊“下一步”。  選CLDC1.0 MIDP1.0,
  
(4). 將 J2ME Wireless Toolkit 作為選定的目標(biāo)平臺(tái)。
  
(5). 單擊“完成”。IDE 將創(chuàng)建 $PROJECTHOME./HuaRongDao 項(xiàng)目文件夾。該項(xiàng)目文件夾包含所有的源和項(xiàng)目元數(shù)據(jù),如項(xiàng)目 Ant 腳本。此時(shí)將在“項(xiàng)目”窗口中打開 HuaRongDao 項(xiàng)目。
  
(6). 現(xiàn)在,我們來添加一個(gè)MIDlet, 右鍵單擊項(xiàng)目,選新建MIDlet,名字為HuaRongDao,不要寫package.點(diǎn)確定生成, 然后在生成后的 代碼里加入CommandListener支持,代碼框架如下:
  
  

  1. /* * HuaRongDaoMidlet.java *  
  2.   * Created on 2008年7月1日, 下午8:18  
  3.   */    
  4.   import javax.microedition.midlet.*;    
  5.   import javax.microedition.lcdui.*;    
  6.   /**    
  7.   *    
  8.   * @author lin    
  9.   * @version    
  10.   */  
  11.     
  12.   public class HuaRongDaoMidlet extends MIDlet implements CommandListener{    
  13.   public void startApp() {   
  14.   }  
  15.   public void pauseApp() {  
  16.   }  
  17.   public void destroyApp(boolean unconditional) {   
  18.   }   
  19.   public void commandAction(Command c, Displayable d) {   
  20.   } 

3. 開始編碼
  
(1)加入退出按鈕,這里,我們用TextBox這種高級(jí)UI來做例子:

  1. public class HuaRongDaoMidlet extends MIDlet implements CommandListener{    
  2.   private Display display;    
  3.   private final static Command CMD_EXIT = new Command("退出", Command.EXIT, 1);    
  4.   public HuaRongDaoMidlet(){    
  5.   display = Display.getDisplay(this);    
  6.   }    
  7.   public void startApp() {    
  8.   TextBox t = new TextBox("TextBox的第一個(gè)參數(shù)","TextBox的第二個(gè)參數(shù)",256,0);    
  9.   t.addCommand(CMD_EXIT);    
  10.   t.setCommandListener(this);    
  11.   display.setCurrent(mainList);    
  12.   }    
  13.   ......    
  14.   public void commandAction(Command c, Displayable d) {    
  15.   if (c == CMD_EXIT) {    
  16.   destroyApp(false);  
  17.   notifyDestroyed();    
  18.   }    
  19.   }    
  20.   } 

注意:A.關(guān)于j2me的api函數(shù),可以在WTK的docs目錄當(dāng)中查到。
  
B.我們使用的是MIDP1.0的函數(shù),2.0支持游戲函數(shù),但是大部分原先的手機(jī)都不支持。
  
C.TextBox是可輸入框,有標(biāo)題,缺省內(nèi)容和內(nèi)容長度等參數(shù)。
  
(2)創(chuàng)建一個(gè)處理圖片的類Images, 處理圖片的方式在2.0當(dāng)中有了很大的改進(jìn),可以直接從一張圖片中按照坐標(biāo)取一部分,但是1.0還沒有這個(gè)功能,所以我們使用Image數(shù)組來實(shí)現(xiàn)。
  
首先,我們先來顯示一個(gè)圖片,來熟悉一下有關(guān)image的操作。首先,加入一個(gè)Image和包含它的ImageItem,因?yàn)镮mage本身不能顯示,
  
必須包在ImageItem中,然后創(chuàng)建一個(gè)Form,把ImageItem加到Form中,最后在屏幕上顯示這個(gè)Form。
  
  

  1. public void startApp() {    
  2.   Image a;    
  3.   ImageItem i;   
  4.   Form props = new Form("測試頁");   
  5.   try    
  6.   {    
  7.   a = Image.createImage("/Duke.png");  
  8.     i = new ImageItem("java吉祥物",a,ImageItem.LAYOUT_DEFAULT,"圖片無法顯示");    
  9.   props.append(i);    
  10.   }    
  11.   catch (IOException e)    
  12.   {    
  13.   a = null;    
  14.   }    
  15.   props.addCommand(CMD_EXIT);    
  16.   props.setCommandListener(this);    
  17.   display.setCurrent(props);  
  18.   } 

編譯運(yùn)行一下,發(fā)現(xiàn)沒有圖片,說明或者是指定的圖片位置不對(duì)或者是系統(tǒng)沒有找到,其中,createImage()中的文件路徑是關(guān)于項(xiàng)目根目錄/res/的,沒有錯(cuò),因此是系統(tǒng)沒有找到res目錄。 File|"HuaRongDao"property,選擇Libraries and Resources,把res的完全路徑加進(jìn)去,再編譯就可以了。
  
好了,測試成功了,現(xiàn)在可以開始編寫Images類,如下:
  

  1.   import javax.microedition.lcdui.*;    
  2.   import javax.microedition.midlet.*;    
  3.   /**    
  4.   *    
  5.   * @author lin    
  6.   */    
  7.   public class Images {//保存常量    
  8.   //繪圖位置常量    
  9.   public static final int UNIT = 20;//方塊的單位長度    
  10.   public static final int LEFT = 20;//畫圖的左邊界頂點(diǎn)    
  11.   public static final int TOP = 22;//畫圖的上邊界頂點(diǎn)    
  12.   //地圖位置常量    
  13.   public static final int WIDTH = 4;//地圖的寬度   
  14.   public static final int HEIGHT = 5;//地圖的高度    
  15.   //地圖標(biāo)記常量    
  16.   public static final byte CAOCAO = (byte) 'a'; //曹操的地圖標(biāo)記    
  17.   public static final byte MACHAO = (byte) 'b';//馬超的地圖標(biāo)記    
  18.   public static final byte HUANGZHONG = (byte) 'c';//黃忠的地圖標(biāo)記    
  19.   public static final byte GUANYU = (byte) 'd';//關(guān)羽的地圖標(biāo)記    
  20.   public static final byte ZHANGFEI = (byte) 'e';//張飛的地圖標(biāo)記    
  21.   public static final byte ZHAOYUN = (byte) 'f';//趙云的地圖標(biāo)記    
  22.   public static final byte ZU = (byte) 'g';//卒的地圖標(biāo)記   
  23.   public static final byte BLANK = (byte) 'h';//空白的地圖標(biāo)記    
  24.   public static final byte CURSOR = (byte) 'i';//光標(biāo)的地圖標(biāo)記    
  25.   //地圖組合標(biāo)記常量   
  26.   public static final byte DLEFT = (byte) '1'; //組合圖形左邊標(biāo)記    
  27.   public static final byte DUP = (byte) '2'; //組合圖形上邊標(biāo)記    
  28.   public static final byte DLEFTUP = (byte) '3'; //組合圖形左上標(biāo)記   
  29.   //圖片常量    
  30.   //public static Image image_base;//基本圖片    
  31.   public static Image image_Zhaoyun;//趙云的圖片    
  32.   public static Image image_Caocao;//曹操的圖片  
  33.     
  34.   public static Image image_Huangzhong;//黃忠的圖片  
  35.     
  36.   public static Image image_Machao;//馬超的圖片  
  37.     
  38.   public static Image image_Guanyu;//關(guān)羽的圖片  
  39.     
  40.   public static Image image_Zhangfei;//張飛的圖片  
  41.     
  42.   public static Image image_Zu;//卒的圖片  
  43.     
  44.   public static Image image_Blank;//空白的圖片  
  45.     
  46.   public static Image image_Frame;//游戲框架的圖片  
  47.     
  48.   public Images() {//構(gòu)造函數(shù)  
  49.     
  50.   }  
  51.     
  52.   public static boolean init() {//初始化游戲中用到的圖片  
  53.     
  54.   try {  
  55.     
  56.   /*     以下的實(shí)現(xiàn)都是基于MIDP2.0的,我們在程序中采用的是基于MIDP1.0的實(shí)現(xiàn)  
  57.     
  58.   image_base = Image.createImage("/huarongroad/BITBACK.png");  
  59.     
  60.   image_Frame = Image.createImage(image_base, 126, 0, 145, 177,Sprite.TRANS_NONE);  
  61.     
  62.   //Sprite類是用來翻轉(zhuǎn)圖片的,是MIDP2.0新新增加的支持游戲的特性  
  63.     
  64.   image_Zhaoyun = Image.createImage(image_base, 0, 0, UNIT, 2 * UNIT,Sprite.TRANS_NONE);  
  65.     
  66.   image_Caocao = Image.createImage(image_base, UNIT, 0, 2 * UNIT,2 * UNIT, Sprite.TRANS_NONE);  
  67.     
  68.   image_Huangzhong = Image.createImage(image_base, 3 * UNIT, 0, UNIT,2 * UNIT,Sprite.TRANS_NONE);  
  69.     
  70.   image_Machao = Image.createImage(image_base, 0, 2 * UNIT, UNIT,2 * UNIT,Sprite.TRANS_NONE);  
  71.     
  72.   image_Guanyu = Image.createImage(image_base, UNIT, 2 * UNIT,2 * UNIT, UNIT,Sprite.TRANS_NONE);  
  73.     
  74.   image_Zhangfei = Image.createImage(image_base, 3 * UNIT, 2 * UNIT,UNIT, 2 * UNIT,Sprite.TRANS_NONE);  
  75.     
  76.   image_Zu = Image.createImage(image_base, 0, 4 * UNIT, UNIT, UNIT,Sprite.TRANS_NONE);  
  77.     
  78.   image_Blank = Image.createImage(image_base, 1 * UNIT, 4 * UNIT,UNIT,UNIT,Sprite.TRANS_NONE); 

(3).建立Draw類用來顯示圖形:
  
  

  1. public class Draw {  
  2.     
  3.   /** Creates a new instance of Draw */  
  4.     
  5.   public Draw(Canvas canvas) {  
  6.     
  7.   }  
  8.     
  9.   public static boolean paint(Graphics g, byte img, int x, int y) {  
  10.     
  11.   //在地圖的x,y點(diǎn)繪制img指定的圖片  
  12.     
  13.   try {  
  14.     
  15.   paint(g, img, x, y, Images.UNIT);//把地圖x,y點(diǎn)轉(zhuǎn)化成畫布的絕對(duì)坐標(biāo),繪圖  
  16.     
  17.   return true;  
  18.     
  19.   }  
  20.     
  21.   catch (Exception ex) {  
  22.     
  23.   return false;  
  24.     
  25.   }  
  26.     
  27.   }  
  28.     
  29.   public static boolean paint(Graphics g, byte img, int x, int y, int unit) {  
  30.     
  31.   try {  
  32.     
  33.   switch (img) {  
  34.     
  35.   case Images.CAOCAO://畫曹操  
  36.     
  37.   //變成絕對(duì)坐標(biāo),并做調(diào)整  
  38.     
  39.   g.drawImage(Images.image_Caocao, Images.LEFT + x * unit,  
  40.     
  41.   Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);  
  42.     
  43.   break;  
  44.     
  45.   case Images.GUANYU://畫關(guān)羽  
  46.     
  47.   g.drawImage(Images.image_Guanyu, Images.LEFT + x * unit,  
  48.     
  49.   Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);  
  50.     
  51.   break;  
  52.     
  53.   case Images.HUANGZHONG://畫黃忠  
  54.     
  55.   g.drawImage(Images.image_Huangzhong, Images.LEFT + x * unit,  
  56.     
  57.   Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);  
  58.     
  59.   break;  
  60.     
  61.   case Images.MACHAO://畫馬超  
  62.     
  63.   g.drawImage(Images.image_Machao, Images.LEFT + x * unit,  
  64.     
  65.   Images.TOP + y * unit, Graphics.TOP | Graphics.LEFT);  
  66.     
  67.   break;  
  68.     
  69.   case Images.ZHANGFEI://畫張飛  
  70.     
  71.   g.drawImage(Images.image_Zhangfei, Images.LEFT + x * unit,  
  72.     
  73.   Images.TOP + y * unit,Graphics.TOP | Graphics.LEFT);  
  74.     
  75.   break;  
  76.     
  77.   case Images.ZHAOYUN://畫趙云  
  78.     
  79.   g.drawImage(Images.image_Zhaoyun, Images.LEFT + x * unit,  
  80.     
  81.   Images.TOP + y * unit,  
  82.     
  83.   Graphics.TOP | Graphics.LEFT);  
  84.     
  85.   break;  
  86.     
  87.   case Images.ZU://畫卒  
  88.     
  89.   g.drawImage(Images.image_Zu, Images.LEFT + x * unit,  
  90.     
  91.   Images.TOP + y * unit, Graphics.TOP | Graphics.LEFT);  
  92.     
  93.   break;  
  94.     
  95.   case Images.BLANK://畫空白  
  96.     
  97.   g.drawImage(Images.image_Blank, Images.LEFT + x * unit,  
  98.     
  99.   Images.TOP + y * unit, Graphics.TOP | Graphics.LEFT);  
  100.     
  101.   break;  
  102.     
  103.   case Images.CURSOR://畫光標(biāo)  
  104.     
  105.   g.drawRect(Images.LEFT + x * unit,  
  106.     
  107.   Images.TOP + y * unit,Images.UNIT,Images.UNIT);  
  108.     
  109.   break;  
  110.     
  111.   }  
  112.     
  113.   return true;  
  114.     
  115.   }catch (Exception ex) {  
  116.     
  117.   return false;  
  118.     
  119.   }  
  120.     
  121.   }  
  122.     
  123.   } 

  
  (4)建立Map類來讀取布局信息:
  
  
  

  1. package HuaRongDao;  
  2.     
  3.   import java.io.InputStream;  
  4.     
  5.   import javax.microedition.lcdui.*;  
  6.     
  7.   /**  
  8.     
  9.   *  
  10.     
  11.   * @author lin  
  12.     
  13.   */  
  14.     
  15.   public class Map {  
  16.     
  17.   //處理游戲的地圖,負(fù)責(zé)從外部文件加載地圖數(shù)據(jù),存放地圖數(shù)據(jù),并按照地圖數(shù)據(jù)繪制地圖  
  18.     
  19.   public byte Grid[][];//存放地圖數(shù)據(jù)  
  20.     
  21.   public Map() {//構(gòu)造函數(shù),負(fù)責(zé)初始化地圖數(shù)據(jù)的存儲(chǔ)結(jié)構(gòu)  
  22.     
  23.   this.Grid = new byte[Images.HEIGHT][Images.WIDTH];  
  24.     
  25.   //用二維數(shù)組存放地圖數(shù)據(jù),注意第一維是豎直坐標(biāo),第二維是水平坐標(biāo)  
  26.     
  27.   }  
  28.     
  29.   public int[] read_map(int i) {  
  30.     
  31.   //從外部文件加載地圖數(shù)據(jù),并存放在存儲(chǔ)結(jié)構(gòu)中,返回值是光標(biāo)點(diǎn)的位置  
  32.     
  33.   //參數(shù)是加載地圖文件的等級(jí)  
  34.     
  35.   int[] a = new int[2];//光標(biāo)點(diǎn)的位置,0是水平位置,1是豎直位置  
  36.     
  37.   try {  
  38.     
  39.   InputStream is = getClass().getResourceAsStream("/levels/level".concat(String.valueOf(i)));  
  40.     
  41.   if (is != null) {  
  42.     
  43.   for (int k = 0; k < Images.HEIGHT; k++) {  
  44.     
  45.   for (int j = 0; j < Images.WIDTH; j++) {  
  46.     
  47.   this.Grid[k][j] = (byte) is.read();  
  48.     
  49.   if ( this.Grid[k][j] == Images.CURSOR ) {  
  50.     
  51.   //判斷出光標(biāo)所在位置  
  52.     
  53.   a[0] = j;//光標(biāo)水平位置  
  54.     
  55.   a[1] = k;//光標(biāo)豎直位置  
  56.     
  57.   this.Grid[k][j] = Images.BLANK;//將光標(biāo)位置設(shè)成空白背景  
  58.     
  59.   }  
  60.     
  61.   }  
  62.     
  63.   is.read();//讀取回車(13),忽略掉  
  64.     
  65.   is.read();//讀取換行(10),忽略掉  
  66.     
  67.   }  
  68.     
  69.   is.close();  
  70.     
  71.   }else {  
  72.     
  73.   //讀取文件失敗  
  74.     
  75.   a[0] = -1;  
  76.     
  77.   a[1] = -1;  
  78.     
  79.   }  
  80.     
  81.   }catch (Exception ex) {  
  82.     
  83.   //打開文件失敗  
  84.     
  85.   a[0] = -1;  
  86.     
  87.   a[1] = -1;  
  88.     
  89.   }  
  90.     
  91.   return a;  
  92.     
  93.   }  
  94.     
  95.   public boolean draw_map(Graphics g) {  
  96.     
  97.   //調(diào)用Draw類的靜態(tài)方法,繪制地圖  
  98.     
  99.   try {  
  100.     
  101.   for (int i = 0; i < Images.HEIGHT; i++) {  
  102.     
  103.   for (int j = 0; j < Images.WIDTH; j++) {  
  104.     
  105.   Draw.paint(g, this.Grid[i][j], j, i);//繪制地圖  
  106.     
  107.   }  
  108.     
  109.   }  
  110.     
  111.   return true;  
  112.     
  113.   }catch (Exception ex) {  
  114.     
  115.   return false;  
  116.     
  117.   }  
  118.     
  119.   }  
  120.     
  121.   } 

  
  注意這里的讀文件操作的文件位置同樣是相對(duì)于res文件夾的。
  
  (5) 建立主邏輯控制:
  
  

  1. package HuaRongDao;  
  2.     
  3.   /**  
  4.     
  5.   *  
  6.     
  7.   * @author lin  
  8.     
  9.   */  
  10.     
  11.   import javax.microedition.lcdui.*;  
  12.     
  13.   public class ControlLogic extends Canvas implements CommandListener {  
  14.     
  15.   private int[] loc = new int[2]; //光標(biāo)的當(dāng)前位置,0是水平位置,1是豎直位置  
  16.     
  17.   private int[] SelectArea = new int[4];//被選定的區(qū)域,即要移動(dòng)的區(qū)域  
  18.     
  19.   private int[] MoveArea = new int[4];//要移動(dòng)到的區(qū)域  
  20.     
  21.   private Map MyMap = new Map();//地圖類  
  22.     
  23.   private boolean selected;//是否已經(jīng)選中要移動(dòng)區(qū)域的標(biāo)志  
  24.     
  25.   private int level;//當(dāng)前的關(guān)  
  26.     
  27.   public ControlLogic() {//構(gòu)造函數(shù)  
  28.     
  29.   try {  
  30.     
  31.   jbInit();//JBuilder定義的初始化函數(shù)  
  32.     
  33.   }catch (Exception e) {  
  34.     
  35.   e.printStackTrace();  
  36.     
  37.   }  
  38.     
  39.   }  
  40.     
  41.   private void Init_game(){  
  42.     
  43.   //初始化游戲,讀取地圖,設(shè)置選擇區(qū)域,清空要移動(dòng)到的區(qū)域  
  44.     
  45.   this.loc = MyMap.read_map(this.level);//讀取地圖文件,并返回光標(biāo)的初始位置  
  46.     
  47.   //0為水平位置,1為豎直位置  
  48.     
  49.   this.SelectArea[0] = this.loc[0];//初始化選中的區(qū)域  
  50.     
  51.   this.SelectArea[1] = this.loc[1];  
  52.     
  53.   this.SelectArea[2] = 1;  
  54.     
  55.   this.SelectArea[3] = 1;  
  56.     
  57.   this.MoveArea[0] = -1;//初始化要移動(dòng)到的區(qū)域  
  58.     
  59.   this.MoveArea[1] = -1;  
  60.     
  61.   this.MoveArea[2] = 0;  
  62.     
  63.   this.MoveArea[3] = 0;  
  64.     
  65.   }  
  66.     
  67.   private void jbInit() throws Exception {//JBuilder定義的初始化函數(shù)  
  68.     
  69.   //初始化實(shí)例變量  
  70.     
  71.   this.selected = false;//設(shè)置沒有被選中的要移動(dòng)區(qū)域  
  72.     
  73.   this.level = 1;  
  74.     
  75.   Images.init();//初始化圖片常量  
  76.     
  77.   Init_game();//初始化游戲,讀取地圖,設(shè)置選擇區(qū)域,清空要移動(dòng)到的區(qū)域  
  78.     
  79.   //setCommandListener(this);//添加命令監(jiān)聽,這是Displayable的實(shí)例方法  
  80.     
  81.   //addCommand(new Command("", Command.EXIT, 1));//添加“退出”按鈕  
  82.     
  83.   }  
  84.     
  85.   public void commandAction(Command command, Displayable displayable) {  
  86.     
  87.   //命令處理函數(shù)  
  88.     
  89.   if (command.getCommandType() == Command.EXIT) {//處理“退出”  
  90.     
  91.   //HuaRongDaoMidlet.quitApp();  
  92.     
  93.   }  
  94.     
  95.   }  
  96.     
  97.   protected void paint(Graphics g) {  
  98.     
  99.   //畫圖函數(shù),用于繪制用戶畫面,即顯示圖片,勾畫選中區(qū)域和要移動(dòng)到的區(qū)域  
  100.     
  101.   try {  
  102.     
  103.   g.drawImage(Images.image_Frame, 0, 0,Graphics.TOP | Graphics.LEFT);//畫背景  
  104.     
  105.   MyMap.draw_map(g);//按照地圖內(nèi)容畫圖  
  106.     
  107.   if ( this.selected )  
  108.     
  109.   g.setColor(0,255, 

4.改進(jìn)程序
  
(1)記錄歷史步驟,以便可以悔棋:
 
記錄歷史步驟的方法是實(shí)現(xiàn)一個(gè)History類,這個(gè)類實(shí)際上是一個(gè)Vector的封裝,用來保存每一步的走法,走法被定義為一個(gè)包含5個(gè)元素的數(shù)組,分別是
  
  X,Y,width,height,direction.
 
這里需要注意的是,Java當(dāng)中實(shí)際上是沒有局部變量的,每一個(gè)局部變量都需要new出來,所以在使用Vector的addElement()函數(shù)時(shí),由于它是傳引用,我們必須要新創(chuàng)建一個(gè)element,而不能使用全局的,因?yàn)槿绻褂萌值?,下一次addElement時(shí),會(huì)因?yàn)樵撟兞俗兞康闹凳沟脛偛偶拥絍ector中的值也改變了。
  
  

  1. import java.util.Vector;  
  2.     
  3.   /**  
  4.     
  5.   *  
  6.     
  7.   * @author lin  
  8.     
  9.   */  
  10.     
  11.   public class History {  
  12.     
  13.   private static Vector steps = new Vector();  
  14.     
  15.   /** Creates a new instance of History */  
  16.     
  17.   public History() {  
  18.     
  19.   clear();  
  20.     
  21.   }  
  22.     
  23.   public static void addStep(Object step){  
  24.     
  25.   steps.addElement(step);  
  26.     
  27.   }  
  28.     
  29.   public static void removeLastStep(){  
  30.     
  31.   steps.removeElement(steps.lastElement());  
  32.     
  33.   }  
  34.     
  35.   public static Object getLastStep(){  
  36.     
  37.   return steps.lastElement();  
  38.     
  39.   }  
  40.     
  41.   public static Object getStepAt(int index){  
  42.     
  43.   return steps.elementAt(index);  
  44.     
  45.   }  
  46.     
  47.   public static int getSize(){  
  48.     
  49.   return steps.size();  
  50.     
  51.   }  
  52.     
  53.   private void clear(){  
  54.     
  55.   if (!steps.isEmpty())  
  56.     
  57.   steps.removeAllElements();  
  58.     
  59.   }  
  60.     
  61.   }  
  62.     
  63.   在每一步移動(dòng)結(jié)束后,記錄這一步的信息:  
  64.     
  65.   ContorlLogic.java: Move()  
  66.     
  67.   ......  
  68.     
  69.   moves++;// 增加移動(dòng)的步驟  
  70.     
  71.   byte[] step = new byte[5]; //五個(gè)參數(shù)分別為,前四個(gè)和SelectArea一樣,最后一個(gè)表示上1,下2,左3,右4。  
  72.     
  73.   //將此次移動(dòng)記錄到歷史記錄當(dāng)中;  
  74.     
  75.   step[0]= this.SelectArea[0];  
  76.     
  77.   step[1]= this.SelectArea[1];  
  78.     
  79.   step[2]= this.SelectArea[2];  
  80.     
  81.   step[3]= this.SelectArea[3];  
  82.     
  83.   step[4]= this.getMoveDirection();  
  84.     
  85.   history.addStep(step);  
  86.     
  87.   ...... 

  
  增加一個(gè)悔棋的按鈕,增加一個(gè)unMove()函數(shù):
  
  

  1. public void unMove(){  
  2.     
  3.   if ( moves == 0 )  
  4.     
  5.   return;  
  6.     
  7.   byte[] step = new byte[5]; //五個(gè)參數(shù)分別為,前四個(gè)和SelectArea一樣,最后一個(gè)表示上1,下2,左3,右4。  
  8.     
  9.   step = (byte []) history.getLastStep();//取得上一步移動(dòng)  
  10.     
  11.   history.removeLastStep();//減少一步;  
  12.     
  13.   moves--;  
  14.     
  15.   for (int i0; i< 4;i++){  
  16.     
  17.   this.MoveArea[i] = step[i];//重設(shè)MoveArea  
  18.     
  19.   this.SelectArea[i] = step[i];//重設(shè)SelectArea  
  20.     
  21.   }  
  22.     
  23.   if (step[4] == 1){  
  24.     
  25.   this.SelectArea[1] = (byte) (step[1]-1);  
  26.     
  27.   this.loc[1]++;  
  28.     
  29.   }  
  30.     
  31.   else if (step[4] == 2){  
  32.     
  33.   this.SelectArea[1] = (byte) (step[1]+1);  
  34.     
  35.   this.loc[1]--;  
  36.     
  37.   }  
  38.     
  39.   else if (step[4] == 3){  
  40.     
  41.   this.SelectArea[0] = (byte) (step[0]-1);  
  42.     
  43.   this.loc[0]++;  
  44.     
  45.   }  
  46.     
  47.   else if (step[4] == 4){  
  48.     
  49.   this.SelectArea[0] = (byte) (step[0]+1);  
  50.     
  51.   this.loc[0]--;  
  52.     
  53.   }  
  54.     
  55.   //移動(dòng)回來.  
  56.     
  57.   byte[][] temp = new byte[this.SelectArea[3]][this.SelectArea[2]];  
  58.     
  59.   //復(fù)制要移動(dòng)的區(qū)域,因?yàn)檫@塊區(qū)域可能會(huì)被覆蓋掉  
  60.     
  61.   for (int i = 0; i < this.SelectArea[2]; i++) {  
  62.     
  63.   for (int j = 0; j < this.SelectArea[3]; j++) {  
  64.     
  65.   temp[j][i] = this.MyMap.Grid[this.SelectArea[1] +j][this.SelectArea[0] + i];  
  66.     
  67.   }  
  68.     
  69.   }  
  70.     
  71.   //將要移動(dòng)的區(qū)域移動(dòng)到剛選中的區(qū)域(即要移動(dòng)到的區(qū)域)  
  72.     
  73.   for (int i = 0; i < this.SelectArea[2]; i++) {  
  74.     
  75.   for (int j = 0; j < this.SelectArea[3]; j++) {  
  76.     
  77.   this.MyMap.Grid[this.MoveArea[1] + j][this.MoveArea[0] + i] = temp[j][i];  
  78.     
  79.   }  
  80.     
  81.   }  
  82.     
  83.   //將要移動(dòng)的區(qū)域中無用內(nèi)容置成空白  
  84.     
  85.   for (int i = 0; i < this.SelectArea[3]; i++) {  
  86.     
  87.   for (int j = 0; j < this.SelectArea[2]; j++) {  
  88.     
  89.   if (!isInRange2(this.SelectArea[0] + j,this.SelectArea[1] + i)) {  
  90.     
  91.   //該點(diǎn)是不在要移動(dòng)到的區(qū)域之內(nèi),需置空  
  92.     
  93.   this.MyMap.Grid[this.SelectArea[1] + i][this.SelectArea[0] + j] = Images.BLANK;  
  94.     
  95.   }  
  96.     
  97.   }  
  98.     
  99.   }  
  100.     
  101.   //交換SelectArea和MoveArea  
  102.     
  103.   byte tempbyte;  
  104.     
  105.   tempbyteSelectArea[0];  
  106.     
  107.   SelectArea[0]=MoveArea[0];  
  108.     
  109.   MoveArea[0]=tempbyte;  
  110.     
  111.   tempbyteSelectArea[1];  
  112.     
  113.   SelectArea[1]=MoveArea[1];  
  114.     
  115.   MoveArea[1]=tempbyte;  
  116.     
  117.   this.selected = false;  
  118.     
  119.   repaint();  
  120.     
  121.   }  
  122.     
  123.   增加處理悔棋的按鈕:  
  124.     
  125.   HuaRongDaoMidlet.java:  
  126.     
  127.   private final static Command CMD_UNDO = new Command("上一步", Command.SCREEN, 1);  
  128.     
  129.   ......  
  130.     
  131.   else if (c == CMD_UNDO) {//處理“上一步”  
  132.     
  133.   logic.unMove();  
  134.     
  135.   }  
  136.     
  137.   ...... 

 
注意:A.在NetBeans當(dāng)中,有許多方便的按鈕,當(dāng)編輯代碼的時(shí)候,代碼編輯區(qū)上面的最右邊有兩個(gè)注釋和反注釋的按鈕,和VS的功能一樣,只是沒有
  
/* */形式的注釋,還有縮進(jìn)反縮進(jìn)等按鈕,編輯很方便,而且當(dāng)函數(shù)參數(shù)輸入完成后,直接按";"就可以自動(dòng)在行尾加入分號(hào)。同樣,可以
  
加入標(biāo)簽: BookMark,使得快速回到上一個(gè)位置成為可能。
  
B.NetBeans把搜索也加到這個(gè)工具欄里面,可以搜索,標(biāo)記,非常方便。
  
(2).改變移動(dòng)方式,程序提供的移動(dòng)方塊的方式非常難操作,我希望能夠點(diǎn)一下方塊他就智能地自己尋找能夠移動(dòng)的位置。這里還有一點(diǎn)需要注意,就是不能繞彎,也就是A-B-A-B這樣來回走,如果還有其他走法,因此算法中加入了許多判斷,但是比原來的代碼要簡單清晰易懂,操作也比原來簡單多了。
  
代碼如下:
  
  

  1. public class ControlLogic extends Canvas implements CommandListener {  
  2.     
  3.   public static final byte DIRECTION_UP  = (byte) '1'; //方向常量  
  4.     
  5.   public static final byte DIRECTION_DOWN = (byte) '2'; //方向常量  
  6.     
  7.   public static final byte DIRECTION_LEFT = (byte) '3'; //方向常量  
  8.     
  9.   public static final byte DIRECTION_RIGHT = (byte) '4'; //方向常量  
  10.     
  11.   private byte[] currentCursor = new byte[4]; //當(dāng)前光標(biāo)所在位置,四個(gè)參數(shù)分別是X,Y,width,height.  
  12.     
  13.   private byte[] nextCursor  = new byte[4]; //要移動(dòng)到的位置的光標(biāo)區(qū)域,參數(shù)同上.  
  14.     
  15.   private Map MyMap = new Map();//地圖類  
  16.     
  17.   private int level;//當(dāng)前的關(guān)  
  18.     
  19.   public int moves=0;//所用的步數(shù).  
  20.     
  21.   private History history = new History();  
  22.     
  23.   public boolean isWin=false;  
  24.     
  25.   public ControlLogic(int gameLevel) {//構(gòu)造函數(shù)  
  26.     
  27.   try {  
  28.     
  29.   this.level = gameLevel;  
  30.     
  31.   isWin=false;  
  32.     
  33.   nbInit();//NetBeans定義的初始化函數(shù)  
  34.     
  35.   }catch (Exception e) {  
  36.     
  37.   e.printStackTrace();  
  38.     
  39.   }  
  40.     
  41.   }  
  42.     
  43.   private void Init_game(){  
  44.     
  45.   //初始化游戲,讀取地圖,設(shè)置選擇區(qū)域,清空要移動(dòng)到的區(qū)域  
  46.     
  47.   this.currentCursor = MyMap.read_map(this.level);//讀取地圖文件,并返回光標(biāo)的初始位置  
  48.     
  49.   //0為水平位置,1為豎直位置, 2為寬,3為高.  
  50.     
  51.   nextCursor[0]=currentCursor[0]; //初始化要移動(dòng)到的區(qū)域  
  52.     
  53.   nextCursor[1]=currentCursor[1];  
  54.     
  55.   nextCursor[2]=currentCursor[2];  
  56.     
  57.   nextCursor[3]=currentCursor[3];  
  58.     
  59.   }  
  60.     
  61.   private void nbInit() throws Exception {//NetBeans定義的初始化函數(shù)  
  62.     
  63.   //初始化實(shí)例變量  
  64.     
  65.   Images.init();//初始化圖片常量  
  66.     
  67.   Init_game();//初始化游戲,讀取地圖,設(shè)置選擇區(qū)域,清空要移 

【編輯推薦】

  1. NetBeans 6.0模塊快速入門教程
  2. Netbeans 6.0發(fā)布,支持Ruby、移動(dòng)開發(fā)和集成的剖析器
  3. NetBeans 6.0預(yù)覽版發(fā)布 Sun再引驚呼
  4. NetBeans成為Ruby開發(fā)者的新伙伴(3)
  5. 八大技術(shù)牛人點(diǎn)評(píng)NetBeans 6.5
責(zé)任編輯:張燕妮 來源: 百度空間
相關(guān)推薦

2010-09-30 11:32:08

NetBeansJ2ME

2009-06-15 15:35:00

netbeansj2me

2010-09-29 09:19:39

J2ME開發(fā)工具

2009-06-11 09:12:36

NetBeansJ2ME

2010-09-29 08:57:04

J2ME前景

2010-09-30 13:48:10

J2ME游戲引擎

2009-10-19 13:59:39

J2ME編程開發(fā)平臺(tái)

2009-06-23 11:30:16

RMSJ2ME

2010-09-29 12:45:50

J2ME

2010-09-29 14:18:36

J2ME SDK

2010-09-30 13:11:59

J2MECanvas

2010-09-29 15:35:04

Item類J2ME

2010-09-29 09:13:48

J2ME開發(fā)環(huán)境

2010-10-09 15:40:19

CookieJ2ME

2010-09-30 13:06:33

Myeclipse J

2009-07-14 18:03:43

Myeclipse J

2010-09-29 09:54:09

J2ME應(yīng)用程序

2010-10-09 16:13:10

J2ME應(yīng)用程序

2011-05-12 14:34:55

cookieJ2ME

2010-09-29 09:28:59

J2ME開發(fā)環(huán)境
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)