📄 optionsform.java
字号:
package unicoco;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class OptionsForm extends Form implements CommandListener,
ItemStateListener {
private boolean nameChanged;
static OptionsForm instance;
public void itemStateChanged(Item item) {
if (item == get(0)) {
nameChanged=true;
}
}
MusicItem mi;
public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("ClearData")) {
writeWFData(rs, -1, true);
((StringItem) get(1)).setText(String.valueOf(0));
((StringItem) get(2)).setText(String.valueOf(0));
} else if (c.getLabel().equals("Back")) {
if(nameChanged){
// System.out.println("changed name");
writeUserName(rs, ((TextField)get(0)).getString(), false);
nameChanged=false;
}
try {
rs.closeRecordStore();
} catch (RecordStoreNotOpenException e) {
// e.printStackTrace();
} catch (RecordStoreException e) {
// e.printStackTrace();
}
if (mi.changed) {
byte[] musicTemp = { mi.selection };
try {
RecordStore rs = RecordStore.openRecordStore("UserInf", false);
OptionsForm.setMusic(rs, false, musicTemp);
MIDGame.game.music = mi.selection;
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
MIDGame.display.setCurrent(MainMenu.getInstance());
}
}
private final static void shortToBytes(short res, byte[] des) {
des[0] = (byte) (0x00ff & (res >> 8));
des[1] = (byte) (0x00ff & (res));
}
private final static short bytesToshort(byte[] res) {
short result = 0;
result = (short) (res[0] & 0x00ff);
result = (short) (((result << 8) & 0xff00) + (res[1] & 0x00ff));
return result;
}
public final static void readWFData(RecordStore rs, short[] wfs) {
byte[] result = null;
try {
result = rs.getRecord(2);
wfs[0] = bytesToshort(result);
result = rs.getRecord(3);
wfs[1] = bytesToshort(result);
} catch (RecordStoreNotOpenException e) {
// e.printStackTrace();
} catch (InvalidRecordIDException e) {
// e.printStackTrace();
} catch (RecordStoreException e) {
// e.printStackTrace();
}
}
//Record ID 1=name 2=wins 3=fails
public final static void writeWFData(RecordStore rs, int recordID,
boolean clear) {
byte[] result = null;
short win = 0, fail = 0;
try {
if (recordID == 0) {
result = new byte[2];
shortToBytes(win, result);
rs.addRecord(result, 0, result.length);
shortToBytes(fail, result);
rs.addRecord(result, 0, result.length);
return;
} else if (!clear) {
result = rs.getRecord(recordID);
short tempResult = bytesToshort(result);
if (tempResult < Short.MAX_VALUE) {
tempResult++;
}
shortToBytes(tempResult, result);
rs.setRecord(recordID, result, 0, result.length);
} else {
result = new byte[2];
shortToBytes((short) 0, result);
rs.setRecord(2, result, 0, result.length);
rs.setRecord(3, result, 0, result.length);
}
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
public final static String readUserName(RecordStore rs) {
byte[] tempResult = null;
String result = null;
try {
tempResult = rs.getRecord(1);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated czatch block
// e.printStackTrace();
}
ByteArrayInputStream bis = new ByteArrayInputStream(tempResult);
DataInputStream dis = new DataInputStream(bis);
try {
result = dis.readUTF();
dis.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
return result;
}
final public static void writeUserName(RecordStore rs, String newName,
boolean first) {
byte[] baUserName = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
dos.writeUTF(newName);
baUserName = bos.toByteArray();
dos.close();
bos.close();
if (first) {
rs.addRecord(baUserName, 0, baUserName.length);
} else {
rs.setRecord(1, baUserName, 0, baUserName.length);
}
} catch (Exception e) {
// e.printStackTrace();
}
}
RecordStore rs;
public OptionsForm() {
super("Options");
setItemStateListener(this);
setCommandListener(this);
addCommand(new Command("Back", Command.CANCEL, 1));
addCommand(new Command("ClearData", Command.SCREEN, 2));
TextField userName = new TextField("UserName : ", "", 8, TextField.ANY);
userName.setLayout(Item.LAYOUT_CENTER | Item.LAYOUT_NEWLINE_AFTER);
StringItem win = new StringItem("Win : ", "", Item.HYPERLINK);
StringItem fail = new StringItem("Fail : ", "", Item.BUTTON);
mi=new MusicItem();
try {
rs = RecordStore.openRecordStore("UserInf", true,
RecordStore.AUTHMODE_PRIVATE, false);
} catch (RecordStoreFullException e) {
// rs = null;
// e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// rs = null;
// e.printStackTrace();
} catch (RecordStoreException e) {
// rs = null;
// e.printStackTrace();
}
//try {
/*
*转到MIDGame中去执行
* if (rs.getNumRecords() == 0) {
//建立Record
writeUserName(rs, "Player", true);
writeWFData(rs, 0, false);
}
*
*/
userName.setString(readUserName(rs));
short[] winAndFail = new short[2];
readWFData(rs, winAndFail);
win.setText(String.valueOf(winAndFail[0]));
fail.setText(String.valueOf(winAndFail[1]));
append(userName);
//win.setLayout(Item.LAYOUT_CENTER |Item.LAYOUT_NEWLINE_AFTER);
append(win);
append(fail);
mi.setLayout(Item.LAYOUT_CENTER |Item.LAYOUT_NEWLINE_BEFORE);
append(mi);
//} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//}
}
protected void showNotify1(){
try {
rs = RecordStore.openRecordStore("UserInf", true,
RecordStore.AUTHMODE_PRIVATE, false);
} catch (RecordStoreFullException e) {
// e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// e.printStackTrace();
} catch (RecordStoreException e) {
// e.printStackTrace();
}
((TextField)get(0)).setString(readUserName(rs));
//System.out.println(readUserName(rs));
short[] winAndFail = new short[2];
readWFData(rs, winAndFail);
((StringItem)get(1)).setText(String.valueOf(winAndFail[0]));
((StringItem)get(2)).setText(String.valueOf(winAndFail[1]));
}
// 0=off 1=on
public static void setMusic(RecordStore rs,boolean first,byte[] isOpen){
if(first){
byte[] on={1};
try {
rs.addRecord(on, 0, on.length);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
return;
}
try {
rs.setRecord(4,isOpen,0,isOpen.length);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
}
public final static Displayable getInstance(){
if(OptionsForm.instance==null){
OptionsForm.instance=new OptionsForm();
}
instance.showNotify1();
return OptionsForm.instance;
}
public final static byte[] getMusicState(RecordStore rs){
byte[] temp=null;
try {
temp=rs.getRecord(4);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (InvalidRecordIDException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
return temp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -