Options.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- import javax.microedition.rms.RecordStore;
- import javax.microedition.rms.RecordStoreException;
- public class Options
- implements CommandListener, ItemStateListener
- {
- private FiveMIDlet midFive;
- private Form frmOptions;
- private Command cmdOk;
- private Command cmdCancel;
- private Gauge ggSize;
- private ChoiceGroup cgFirst;
- private ChoiceGroup cgDegree;
- private int borderSize;
- private boolean computerFirst;
- private int degree;
- public Options(FiveMIDlet m)
- {
- borderSize = 13;
- computerFirst = true;
- degree = 1;
- midFive = m;
- load();
- frmOptions = new Form("游戏设置");
- ggSize = new Gauge("棋盘大小: " + borderSize + " X " + borderSize, true, 10, borderSize - 9);
- frmOptions.append(ggSize);
- cgFirst = new ChoiceGroup(null, 2);
- cgFirst.append("对方先行", null);
- cgFirst.setSelectedIndex(0, computerFirst);
- frmOptions.append(cgFirst);
- cgDegree = new ChoiceGroup("难度等级:", 1);
- cgDegree.append("简单", null);
- cgDegree.append("中等", null);
- cgDegree.append("较难", null);
- cgDegree.setSelectedIndex(degree - 1, true);
- frmOptions.append(cgDegree);
- frmOptions.setItemStateListener(this);
- cmdOk = new Command("确定", 4, 2);
- cmdCancel = new Command("取消", 3, 1);
- frmOptions.addCommand(cmdOk);
- frmOptions.addCommand(cmdCancel);
- frmOptions.setCommandListener(this);
- }
- public int borderSize()
- {
- return borderSize;
- }
- public boolean computerFirst()
- {
- return computerFirst;
- }
- public int degree()
- {
- return degree;
- }
- public Form frmOptions()
- {
- return frmOptions;
- }
- public void itemStateChanged(Item item)
- {
- if(item == ggSize)
- {
- int bs = ggSize.getValue() + 9;
- ggSize.setLabel("棋盘大小: " + bs + " X " + bs);
- }
- }
- public void commandAction(Command c, Displayable s)
- {
- if(c == cmdOk)
- {
- borderSize = ggSize.getValue() + 9;
- if(borderSize > 19)
- borderSize = 19;
- if(borderSize < 9)
- borderSize = 9;
- computerFirst = cgFirst.isSelected(0);
- degree = cgDegree.getSelectedIndex() + 1;
- save();
- midFive.backHome();
- } else
- if(c == cmdCancel)
- midFive.backHome();
- }
- private void load()
- {
- try
- {
- RecordStore rs = RecordStore.openRecordStore("Options", false);
- if(rs.getNumRecords() > 0)
- {
- byte bs[] = rs.getRecord(1);
- if(bs.length >= 3)
- {
- borderSize = bs[0];
- if(borderSize < 9)
- borderSize = 9;
- if(borderSize > 19)
- borderSize = 19;
- computerFirst = bs[1] == 1;
- degree = bs[2];
- if(degree < 1)
- degree = 1;
- if(degree > 3)
- degree = 3;
- }
- }
- rs.closeRecordStore();
- }
- catch(RecordStoreException _ex) { }
- }
- private void save()
- {
- try
- {
- RecordStore rs = RecordStore.openRecordStore("Options", true);
- byte bs[] = new byte[3];
- bs[0] = (byte)borderSize;
- bs[1] = (byte)(computerFirst ? 1 : 0);
- bs[2] = (byte)degree;
- if(rs.getNumRecords() > 0)
- rs.setRecord(1, bs, 0, 3);
- else
- rs.addRecord(bs, 0, 3);
- rs.closeRecordStore();
- }
- catch(RecordStoreException _ex) { }
- }
- }