成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

用NetBeans平臺開發J2ME游戲實例講解

開發 后端
本文詳細介紹了用NetBeans平臺開發J2ME游戲實例,首先必須安裝NetBeans IDE 4.0 和 NetBeans Mobility Pack 4.0然后才能繼續開發。

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

2. 創建 MIDP 應用程序 創建新的 J2ME MIDP 項目
  
2. 創建新的移動應用程序:
  
(1).選擇“文件”>“新建項目”(Ctrl-Shift-N)。在“類別”下選擇“移動”。在“項目”下選擇“移動應用程序”,然后單擊“ 下一步”。
  
(2). 在“項目名稱”下輸入 HuaRongDao。將項目主目錄更改為您系統上的任何目錄。從現在起,我們將該目錄稱為 $PROJECTHOME。
  
(3). 不要選中“創建 HelloMIDlet”復選框。單擊“下一步”。  選CLDC1.0 MIDP1.0,
  
(4). 將 J2ME Wireless Toolkit 作為選定的目標平臺。
  
(5). 單擊“完成”。IDE 將創建 $PROJECTHOME./HuaRongDao 項目文件夾。該項目文件夾包含所有的源和項目元數據,如項目 Ant 腳本。此時將在“項目”窗口中打開 HuaRongDao 項目。
  
(6). 現在,我們來添加一個MIDlet, 右鍵單擊項目,選新建MIDlet,名字為HuaRongDao,不要寫package.點確定生成, 然后在生成后的 代碼里加入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這種高級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的第一個參數","TextBox的第二個參數",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.關于j2me的api函數,可以在WTK的docs目錄當中查到。
  
B.我們使用的是MIDP1.0的函數,2.0支持游戲函數,但是大部分原先的手機都不支持。
  
C.TextBox是可輸入框,有標題,缺省內容和內容長度等參數。
  
(2)創建一個處理圖片的類Images, 處理圖片的方式在2.0當中有了很大的改進,可以直接從一張圖片中按照坐標取一部分,但是1.0還沒有這個功能,所以我們使用Image數組來實現。
  
首先,我們先來顯示一個圖片,來熟悉一下有關image的操作。首先,加入一個Image和包含它的ImageItem,因為Image本身不能顯示,
  
必須包在ImageItem中,然后創建一個Form,把ImageItem加到Form中,最后在屏幕上顯示這個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.   } 

編譯運行一下,發現沒有圖片,說明或者是指定的圖片位置不對或者是系統沒有找到,其中,createImage()中的文件路徑是關于項目根目錄/res/的,沒有錯,因此是系統沒有找到res目錄。 File|"HuaRongDao"property,選擇Libraries and Resources,把res的完全路徑加進去,再編譯就可以了。
  
好了,測試成功了,現在可以開始編寫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;//畫圖的左邊界頂點    
  11.   public static final int TOP = 22;//畫圖的上邊界頂點    
  12.   //地圖位置常量    
  13.   public static final int WIDTH = 4;//地圖的寬度   
  14.   public static final int HEIGHT = 5;//地圖的高度    
  15.   //地圖標記常量    
  16.   public static final byte CAOCAO = (byte) 'a'; //曹操的地圖標記    
  17.   public static final byte MACHAO = (byte) 'b';//馬超的地圖標記    
  18.   public static final byte HUANGZHONG = (byte) 'c';//黃忠的地圖標記    
  19.   public static final byte GUANYU = (byte) 'd';//關羽的地圖標記    
  20.   public static final byte ZHANGFEI = (byte) 'e';//張飛的地圖標記    
  21.   public static final byte ZHAOYUN = (byte) 'f';//趙云的地圖標記    
  22.   public static final byte ZU = (byte) 'g';//卒的地圖標記   
  23.   public static final byte BLANK = (byte) 'h';//空白的地圖標記    
  24.   public static final byte CURSOR = (byte) 'i';//光標的地圖標記    
  25.   //地圖組合標記常量   
  26.   public static final byte DLEFT = (byte) '1'; //組合圖形左邊標記    
  27.   public static final byte DUP = (byte) '2'; //組合圖形上邊標記    
  28.   public static final byte DLEFTUP = (byte) '3'; //組合圖形左上標記   
  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;//關羽的圖片  
  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() {//構造函數  
  49.     
  50.   }  
  51.     
  52.   public static boolean init() {//初始化游戲中用到的圖片  
  53.     
  54.   try {  
  55.     
  56.   /*     以下的實現都是基于MIDP2.0的,我們在程序中采用的是基于MIDP1.0的實現  
  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類是用來翻轉圖片的,是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點繪制img指定的圖片  
  12.     
  13.   try {  
  14.     
  15.   paint(g, img, x, y, Images.UNIT);//把地圖x,y點轉化成畫布的絕對坐標,繪圖  
  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.   //變成絕對坐標,并做調整  
  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://畫關羽  
  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://畫光標  
  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.   //處理游戲的地圖,負責從外部文件加載地圖數據,存放地圖數據,并按照地圖數據繪制地圖  
  18.     
  19.   public byte Grid[][];//存放地圖數據  
  20.     
  21.   public Map() {//構造函數,負責初始化地圖數據的存儲結構  
  22.     
  23.   this.Grid = new byte[Images.HEIGHT][Images.WIDTH];  
  24.     
  25.   //用二維數組存放地圖數據,注意第一維是豎直坐標,第二維是水平坐標  
  26.     
  27.   }  
  28.     
  29.   public int[] read_map(int i) {  
  30.     
  31.   //從外部文件加載地圖數據,并存放在存儲結構中,返回值是光標點的位置  
  32.     
  33.   //參數是加載地圖文件的等級  
  34.     
  35.   int[] a = new int[2];//光標點的位置,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.   //判斷出光標所在位置  
  52.     
  53.   a[0] = j;//光標水平位置  
  54.     
  55.   a[1] = k;//光標豎直位置  
  56.     
  57.   this.Grid[k][j] = Images.BLANK;//將光標位置設成空白背景  
  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.   //調用Draw類的靜態方法,繪制地圖  
  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.   } 

  
  注意這里的讀文件操作的文件位置同樣是相對于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]; //光標的當前位置,0是水平位置,1是豎直位置  
  16.     
  17.   private int[] SelectArea = new int[4];//被選定的區域,即要移動的區域  
  18.     
  19.   private int[] MoveArea = new int[4];//要移動到的區域  
  20.     
  21.   private Map MyMap = new Map();//地圖類  
  22.     
  23.   private boolean selected;//是否已經選中要移動區域的標志  
  24.     
  25.   private int level;//當前的關  
  26.     
  27.   public ControlLogic() {//構造函數  
  28.     
  29.   try {  
  30.     
  31.   jbInit();//JBuilder定義的初始化函數  
  32.     
  33.   }catch (Exception e) {  
  34.     
  35.   e.printStackTrace();  
  36.     
  37.   }  
  38.     
  39.   }  
  40.     
  41.   private void Init_game(){  
  42.     
  43.   //初始化游戲,讀取地圖,設置選擇區域,清空要移動到的區域  
  44.     
  45.   this.loc = MyMap.read_map(this.level);//讀取地圖文件,并返回光標的初始位置  
  46.     
  47.   //0為水平位置,1為豎直位置  
  48.     
  49.   this.SelectArea[0] = this.loc[0];//初始化選中的區域  
  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;//初始化要移動到的區域  
  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定義的初始化函數  
  68.     
  69.   //初始化實例變量  
  70.     
  71.   this.selected = false;//設置沒有被選中的要移動區域  
  72.     
  73.   this.level = 1;  
  74.     
  75.   Images.init();//初始化圖片常量  
  76.     
  77.   Init_game();//初始化游戲,讀取地圖,設置選擇區域,清空要移動到的區域  
  78.     
  79.   //setCommandListener(this);//添加命令監聽,這是Displayable的實例方法  
  80.     
  81.   //addCommand(new Command("", Command.EXIT, 1));//添加“退出”按鈕  
  82.     
  83.   }  
  84.     
  85.   public void commandAction(Command command, Displayable displayable) {  
  86.     
  87.   //命令處理函數  
  88.     
  89.   if (command.getCommandType() == Command.EXIT) {//處理“退出”  
  90.     
  91.   //HuaRongDaoMidlet.quitApp();  
  92.     
  93.   }  
  94.     
  95.   }  
  96.     
  97.   protected void paint(Graphics g) {  
  98.     
  99.   //畫圖函數,用于繪制用戶畫面,即顯示圖片,勾畫選中區域和要移動到的區域  
  100.     
  101.   try {  
  102.     
  103.   g.drawImage(Images.image_Frame, 0, 0,Graphics.TOP | Graphics.LEFT);//畫背景  
  104.     
  105.   MyMap.draw_map(g);//按照地圖內容畫圖  
  106.     
  107.   if ( this.selected )  
  108.     
  109.   g.setColor(0,255, 

4.改進程序
  
(1)記錄歷史步驟,以便可以悔棋:
 
記錄歷史步驟的方法是實現一個History類,這個類實際上是一個Vector的封裝,用來保存每一步的走法,走法被定義為一個包含5個元素的數組,分別是
  
  X,Y,width,height,direction.
 
這里需要注意的是,Java當中實際上是沒有局部變量的,每一個局部變量都需要new出來,所以在使用Vector的addElement()函數時,由于它是傳引用,我們必須要新創建一個element,而不能使用全局的,因為如果使用全局的,下一次addElement時,會因為該變了變量的值使得剛才加到Vector中的值也改變了。
  
  

  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.   在每一步移動結束后,記錄這一步的信息:  
  64.     
  65.   ContorlLogic.java: Move()  
  66.     
  67.   ......  
  68.     
  69.   moves++;// 增加移動的步驟  
  70.     
  71.   byte[] step = new byte[5]; //五個參數分別為,前四個和SelectArea一樣,最后一個表示上1,下2,左3,右4。  
  72.     
  73.   //將此次移動記錄到歷史記錄當中;  
  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.   ...... 

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

  1. public void unMove(){  
  2.     
  3.   if ( moves == 0 )  
  4.     
  5.   return;  
  6.     
  7.   byte[] step = new byte[5]; //五個參數分別為,前四個和SelectArea一樣,最后一個表示上1,下2,左3,右4。  
  8.     
  9.   step = (byte []) history.getLastStep();//取得上一步移動  
  10.     
  11.   history.removeLastStep();//減少一步;  
  12.     
  13.   moves--;  
  14.     
  15.   for (int i0; i< 4;i++){  
  16.     
  17.   this.MoveArea[i] = step[i];//重設MoveArea  
  18.     
  19.   this.SelectArea[i] = step[i];//重設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.   //移動回來.  
  56.     
  57.   byte[][] temp = new byte[this.SelectArea[3]][this.SelectArea[2]];  
  58.     
  59.   //復制要移動的區域,因為這塊區域可能會被覆蓋掉  
  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.   //將要移動的區域移動到剛選中的區域(即要移動到的區域)  
  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.   //將要移動的區域中無用內容置成空白  
  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.   //該點是不在要移動到的區域之內,需置空  
  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當中,有許多方便的按鈕,當編輯代碼的時候,代碼編輯區上面的最右邊有兩個注釋和反注釋的按鈕,和VS的功能一樣,只是沒有
  
/* */形式的注釋,還有縮進反縮進等按鈕,編輯很方便,而且當函數參數輸入完成后,直接按";"就可以自動在行尾加入分號。同樣,可以
  
加入標簽: BookMark,使得快速回到上一個位置成為可能。
  
B.NetBeans把搜索也加到這個工具欄里面,可以搜索,標記,非常方便。
  
(2).改變移動方式,程序提供的移動方塊的方式非常難操作,我希望能夠點一下方塊他就智能地自己尋找能夠移動的位置。這里還有一點需要注意,就是不能繞彎,也就是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]; //當前光標所在位置,四個參數分別是X,Y,width,height.  
  12.     
  13.   private byte[] nextCursor  = new byte[4]; //要移動到的位置的光標區域,參數同上.  
  14.     
  15.   private Map MyMap = new Map();//地圖類  
  16.     
  17.   private int level;//當前的關  
  18.     
  19.   public int moves=0;//所用的步數.  
  20.     
  21.   private History history = new History();  
  22.     
  23.   public boolean isWin=false;  
  24.     
  25.   public ControlLogic(int gameLevel) {//構造函數  
  26.     
  27.   try {  
  28.     
  29.   this.level = gameLevel;  
  30.     
  31.   isWin=false;  
  32.     
  33.   nbInit();//NetBeans定義的初始化函數  
  34.     
  35.   }catch (Exception e) {  
  36.     
  37.   e.printStackTrace();  
  38.     
  39.   }  
  40.     
  41.   }  
  42.     
  43.   private void Init_game(){  
  44.     
  45.   //初始化游戲,讀取地圖,設置選擇區域,清空要移動到的區域  
  46.     
  47.   this.currentCursor = MyMap.read_map(this.level);//讀取地圖文件,并返回光標的初始位置  
  48.     
  49.   //0為水平位置,1為豎直位置, 2為寬,3為高.  
  50.     
  51.   nextCursor[0]=currentCursor[0]; //初始化要移動到的區域  
  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定義的初始化函數  
  62.     
  63.   //初始化實例變量  
  64.     
  65.   Images.init();//初始化圖片常量  
  66.     
  67.   Init_game();//初始化游戲,讀取地圖,設置選擇區域,清空要移 

【編輯推薦】

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

2010-09-30 11:32:08

NetBeansJ2ME

2009-06-15 15:35:00

netbeansj2me

2010-09-29 09:19:39

J2ME開發工具

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編程開發平臺

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開發環境

2010-10-09 15:40:19

CookieJ2ME

2010-09-29 09:28:59

J2ME開發環境

2010-09-29 09:54:09

J2ME應用程序

2010-09-30 13:06:33

Myeclipse J

2010-10-09 16:13:10

J2ME應用程序

2009-07-14 18:03:43

Myeclipse J

2011-05-12 14:34:55

cookieJ2ME
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精品视频一二区 | 中文字幕一区二区三区不卡在线 | 国产美女永久免费无遮挡 | 碰碰视频 | 999视频| 欧美日韩国产三级 | 毛片网站在线观看视频 | 很很干很很日 | 男人的天堂亚洲 | 亚州精品天堂中文字幕 | 亚洲欧美日韩精品久久亚洲区 | 欧美精品在线播放 | 4h影视| 日韩精品视频中文字幕 | 一级欧美 | 91在线一区 | 欧美综合久久久 | av网站观看 | 亚洲成人一区二区 | 久久精品亚洲国产 | 亚洲精品一区二区三区中文字幕 | 99精品电影 | 亚洲午夜精品一区二区三区他趣 | 久久福利电影 | 国产精品久久久久久吹潮日韩动画 | 国产精品亚洲成在人线 | 麻豆av网站 | 午夜视频免费在线 | 成人一区二区在线 | 国产一区二区三区四区三区四 | 成人欧美一区二区三区在线播放 | 亚洲欧美日韩国产综合 | 久久精品中文 | 久久久久久久久一区 | 成人在线精品视频 | 国产九九九九 | 人人射人人 | 夜夜夜久久久 | 99精品视频免费观看 | 成人在线观看黄 | 国产精品成人在线播放 |