Options.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.rms.RecordStore;
  3. import javax.microedition.rms.RecordStoreException;
  4. public class Options
  5.     implements CommandListener, ItemStateListener
  6. {
  7.     private FiveMIDlet midFive;
  8.     private Form frmOptions;
  9.     private Command cmdOk;
  10.     private Command cmdCancel;
  11.     private Gauge ggSize;
  12.     private ChoiceGroup cgFirst;
  13.     private ChoiceGroup cgDegree;
  14.     private int borderSize;
  15.     private boolean computerFirst;
  16.     private int degree;
  17.     public Options(FiveMIDlet m)
  18.     {
  19.         borderSize = 13;
  20.         computerFirst = true;
  21.         degree = 1;
  22.         midFive = m;
  23.         load();
  24.         frmOptions = new Form("游戏设置");
  25.         ggSize = new Gauge("棋盘大小: " + borderSize + " X " + borderSize, true, 10, borderSize - 9);
  26.         frmOptions.append(ggSize);
  27.         cgFirst = new ChoiceGroup(null, 2);
  28.         cgFirst.append("对方先行", null);
  29.         cgFirst.setSelectedIndex(0, computerFirst);
  30.         frmOptions.append(cgFirst);
  31.         cgDegree = new ChoiceGroup("难度等级:", 1);
  32.         cgDegree.append("简单", null);
  33.         cgDegree.append("中等", null);
  34.         cgDegree.append("较难", null);
  35.         cgDegree.setSelectedIndex(degree - 1, true);
  36.         frmOptions.append(cgDegree);
  37.         frmOptions.setItemStateListener(this);
  38.         cmdOk = new Command("确定", 4, 2);
  39.         cmdCancel = new Command("取消", 3, 1);
  40.         frmOptions.addCommand(cmdOk);
  41.         frmOptions.addCommand(cmdCancel);
  42.         frmOptions.setCommandListener(this);
  43.     }
  44.     public int borderSize()
  45.     {
  46.         return borderSize;
  47.     }
  48.     public boolean computerFirst()
  49.     {
  50.         return computerFirst;
  51.     }
  52.     public int degree()
  53.     {
  54.         return degree;
  55.     }
  56.     public Form frmOptions()
  57.     {
  58.         return frmOptions;
  59.     }
  60.     public void itemStateChanged(Item item)
  61.     {
  62.         if(item == ggSize)
  63.         {
  64.             int bs = ggSize.getValue() + 9;
  65.             ggSize.setLabel("棋盘大小: " + bs + " X " + bs);
  66.         }
  67.     }
  68.     public void commandAction(Command c, Displayable s)
  69.     {
  70.         if(c == cmdOk)
  71.         {
  72.             borderSize = ggSize.getValue() + 9;
  73.             if(borderSize > 19)
  74.                 borderSize = 19;
  75.             if(borderSize < 9)
  76.                 borderSize = 9;
  77.             computerFirst = cgFirst.isSelected(0);
  78.             degree = cgDegree.getSelectedIndex() + 1;
  79.             save();
  80.             midFive.backHome();
  81.         } else
  82.         if(c == cmdCancel)
  83.             midFive.backHome();
  84.     }
  85.     private void load()
  86.     {
  87.         try
  88.         {
  89.             RecordStore rs = RecordStore.openRecordStore("Options", false);
  90.             if(rs.getNumRecords() > 0)
  91.             {
  92.                 byte bs[] = rs.getRecord(1);
  93.                 if(bs.length >= 3)
  94.                 {
  95.                     borderSize = bs[0];
  96.                     if(borderSize < 9)
  97.                         borderSize = 9;
  98.                     if(borderSize > 19)
  99.                         borderSize = 19;
  100.                     computerFirst = bs[1] == 1;
  101.                     degree = bs[2];
  102.                     if(degree < 1)
  103.                         degree = 1;
  104.                     if(degree > 3)
  105.                         degree = 3;
  106.                 }
  107.             }
  108.             rs.closeRecordStore();
  109.         }
  110.         catch(RecordStoreException _ex) { }
  111.     }
  112.     private void save()
  113.     {
  114.         try
  115.         {
  116.             RecordStore rs = RecordStore.openRecordStore("Options", true);
  117.             byte bs[] = new byte[3];
  118.             bs[0] = (byte)borderSize;
  119.             bs[1] = (byte)(computerFirst ? 1 : 0);
  120.             bs[2] = (byte)degree;
  121.             if(rs.getNumRecords() > 0)
  122.                 rs.setRecord(1, bs, 0, 3);
  123.             else
  124.                 rs.addRecord(bs, 0, 3);
  125.             rs.closeRecordStore();
  126.         }
  127.         catch(RecordStoreException _ex) { }
  128.     }
  129. }