📄 highscorerms.java
字号:
/*
* HighScoreRms.java
* Download by http://www.codefans.net
* Created on 2005年12月19日, 下午2:09
*
* To change this template, choose Tools | Options and locate the
template under
* the Source Creation and Management node. Right-click the template
and choose
* Open. You can then make changes to the template in the Source
Editor.
*/
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
/**
*
* @author shide8848
*/
class HighScoreComparator implements RecordComparator{
public int compare(byte[] rec1, byte[] rec2){
int p1=0;
int p2=0;
try{
HighScoreRms.deserialize(rec1);
p1=HighScoreRms.point;
HighScoreRms.deserialize(rec2);
p2=HighScoreRms.point;
}catch(Exception e){}
if (p1==p2){
return RecordComparator.EQUIVALENT;
} else if(p1>p2){
return RecordComparator.PRECEDES;
} else{
return RecordComparator.FOLLOWS;
}
}
}
public class HighScoreRms implements CommandListener{
public static String playName="";
public static int point=0;
public static String[] plays=null;
public static int[] points=null;
public static byte[] data=null;
Command addCommand =new Command("保存",Command.OK,0);
Command exitCommand =new Command("不保存",Command.OK,1);
TextField tf=new TextField("姓名","",15,TextField.ANY);
Form frm=new Form("恭喜!您的得分已破记录.");
public static boolean isInputing=false;
/** Creates a new instance of HighScoreRms */
public HighScoreRms() {
frm.append(tf);
frm.addCommand(addCommand);
frm.addCommand(exitCommand);
frm.setCommandListener(this);
}
public static byte[] serialize(String pName,int pt) throws
IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(pName);
dos.writeInt(pt);
baos.close();
dos.close();
return baos.toByteArray();
}
public static void deserialize(byte[] data1) throws IOException{
ByteArrayInputStream bais = new ByteArrayInputStream(data1);
DataInputStream dis = new DataInputStream(bais);
playName=dis.readUTF();
point=dis.readInt();
bais.close();
dis.close();
}
public static int getMaxPoint(){
int r=0;
int id=0;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
}catch(Exception e){
}
if (rs1!=null){
RecordEnumeration re=null;
try{
re=rs1.enumerateRecords(null,new HighScoreComparator(),false);
if(re.hasNextElement()){
id=re.nextRecordId();
data=rs1.getRecord(id);
deserialize(data);
}
}catch(Exception e){
}
if(re!=null){
re.destroy();
}
r=point;
re=null;
try{
rs1.closeRecordStore();
}catch(Exception e){
}
rs1=null;
}
return r;
}
//返回值-1代表没有此记录或记数个数少于5
public static int getMinRecId(){
int r=-1;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
}catch(Exception e){}
RecordEnumeration re=null;
if (rs1!=null){
try{
r=rs1.getNumRecords();
}catch(Exception e){}
if (r<5){
r=-1;
}else{
try{
re=rs1.enumerateRecords(null,new HighScoreComparator(),false);
while(re.hasNextElement()){
r=re.nextRecordId();
}
}catch(Exception e){}
if(re!=null){
re.destroy();
}
re=null;
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
return r;
}
public static void saveRec(String nam,int pot){
try{
data=HighScoreRms.serialize(nam,pot);
}catch(Exception e){}
int min=getMinRecId();
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
} catch(Exception e){}
if (rs1!=null){
if (min==-1){
try{
rs1.addRecord(data,0,data.length);
}catch(Exception e){}
}else{
try{
rs1.setRecord(min,data,0,data.length);
}catch(Exception e){}
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
}
//返回值代表已经存储的记录个数
public static int setHighScoreList(){
int r=0;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("highscore",true);
r=rs1.getNumRecords();
}catch(Exception e){}
if (r>0){
plays=new String[r];
points=new int[r];
try{
RecordEnumeration re=rs1.enumerateRecords(null,new HighScoreComparator(),false);
int i=0;
while(re.hasNextElement()){
data=re.nextRecord();
deserialize(data);
plays[i]=playName;
points[i]=point;
i++;
}
if(re!=null){
re.destroy();
}
}catch(Exception e){}
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
return r;
}
public void commandAction(Command c,Displayable d){
if(c==addCommand){
String pname=tf.getString();
saveRec(pname,GameCanvas.totalCursor);
}
isInputing=false;
PlumberMidlet.mainMidlet.disp.setCurrent(PlumberMidlet.mainMidlet.mainCanvas);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -