📄 svpoint.java
字号:
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
class rmsComparator implements RecordComparator{
public int compare(byte[] rec1, byte[] rec2){
int p1=0;
int p2=0;
try{
svPoint.deserialize(rec1);
p1=svPoint.point;
svPoint.deserialize(rec2);
p2=svPoint.point;
}catch(Exception e){}
if (p1==p2){
return RecordComparator.EQUIVALENT;
} else if(p1>p2){
return RecordComparator.PRECEDES;
} else{
return RecordComparator.FOLLOWS;
}
}
}
//Download by http://www.codefans.net
public class svPoint implements CommandListener{
public static int gate=0;
public static int point2=0;
public static String playName=null;
public static int point=0;
public static byte[] data=null;
public static String[] plays=null;
public static int[] points=null;
public static boolean inputing=false;
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 svPoint(){
frm.append(tf);
frm.addCommand(addCommand);
frm.addCommand(exitCommand);
frm.setCommandListener(this);
}
public static boolean isExistRs(String rsName){
boolean b=true;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore(rsName,false);
} catch(RecordStoreNotFoundException e){
b=false;
}catch(Exception e){
}
if (b){
try{
rs1.closeRecordStore();
}catch(Exception e){
}
}
rs1=null;
return b;
}
public static int getMaxPoint(){
int r=0;
int id=0;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("point",false);
}catch(Exception e){
}
if (rs1!=null){
RecordEnumeration re;
try{
re=rs1.enumerateRecords(null,new rmsComparator(),false);
if(re.hasNextElement()){
id=re.nextRecordId();
data=rs1.getRecord(id);
deserialize(data);
}
re.destroy();
}catch(Exception e){
}
r=point;
re=null;
try{
rs1.closeRecordStore();
}catch(Exception e){
}
rs1=null;
}
return r;
}
public static int getMinRecId(){
int r=0;
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("point",false);
}catch(Exception e){}
RecordEnumeration re;
try{
re=rs1.enumerateRecords(null,new rmsComparator(),false);
while(re.hasNextElement()){
r=re.nextRecordId();
}
re.destroy();
}catch(Exception e){}
re=null;
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
return r;
}
public static void saveRec(String nam,int pot){
int min=getMinRecId();
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("point",false);
} catch(Exception e){}
if (rs1!=null){
try{
data=serialize(nam,pot);
rs1.setRecord(min,data,0,data.length);
Man.MaxPoint=pot;
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
}
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 boolean isInput(int pt){
boolean b=false;
RecordStore rs1=null;
if (!isExistRs("point")){
if (pt>0){
b=true;
}
} else if(pt>getMaxPoint()){
b=true;
}
return b;
}
//在得分肯定必须输入的情况下
public static void savePoint(String pName,int pt){
RecordStore rs1=null;
try{
data=serialize(pName,pt);
}catch(Exception e){}
if (!isExistRs("point")){
try{
rs1=RecordStore.openRecordStore("point",true);
rs1.addRecord(data,0,data.length);
}catch(Exception e){}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
} else{//最大分小于得分的情况下
int recNum=0;
try{
rs1=RecordStore.openRecordStore("point",true);
recNum=rs1.getNumRecords();
}catch(Exception e){}
if (recNum<5){
try{
rs1.addRecord(data,0,data.length);
}catch(Exception e){}
} else{
int minId=getMinRecId();
try{
rs1.setRecord(minId,data,0,data.length);
}catch(Exception e){}
}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
}//*/
//public void Save(int pt){
// GhostCanvas.disp.setCurrent(frm);
//}
public void commandAction(Command c,Displayable d){
if(c==addCommand){
String pname=tf.getString();
int pt=Man.point;
savePoint(pname,pt);
inputing=false;
GhostMIDlet.mainMIDlet.disp.setCurrent(GhostMIDlet.mainMIDlet.ghostCanvas);
}else{
inputing=false;
GhostMIDlet.mainMIDlet.disp.setCurrent(GhostMIDlet.mainMIDlet.ghostCanvas);
}
}
public static void getPointList(){
RecordStore rs1=null;
int t=0;
try{
rs1=RecordStore.openRecordStore("point",false);
t=rs1.getNumRecords();
}catch(Exception e){}
plays=new String[t];
points=new int[t];
try{
RecordEnumeration re=rs1.enumerateRecords(null,new rmsComparator(),false);
int i=0;
while(re.hasNextElement()){
data=re.nextRecord();
deserialize(data);
plays[i]=playName;
points[i]=point;
i++;
}
re.destroy();
}catch(Exception e){}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
public static byte[] serialize2(int gate,int pt) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(gate);
dos.writeInt(pt);
baos.close();
dos.close();
return baos.toByteArray();
}
public static void deserialize2(byte[] data1) throws IOException{
ByteArrayInputStream bais = new ByteArrayInputStream(data1);
DataInputStream dis = new DataInputStream(bais);
gate=dis.readInt();
point2=dis.readInt();
bais.close();
dis.close();
}
public static void saveTollGate(int ga,int pt){
RecordStore rs1=null;
if(!svPoint.isExistRs("gate")){
try{
rs1=RecordStore.openRecordStore("gate",true);
}catch(Exception e){}
try{
byte[] tdata=svPoint.serialize2(ga,pt);
rs1.addRecord(tdata,0,tdata.length);
tdata=null;
}catch(Exception e){}
try{
rs1.closeRecordStore();
}catch(Exception e){}
} else{
try{
rs1=RecordStore.openRecordStore("gate",false);
}catch(Exception e){}
try{
byte[] tdata=svPoint.serialize2(ga,pt);
RecordEnumeration re=rs1.enumerateRecords(null,new rmsComparator(),false);
if (re.hasNextElement()){
int rid=re.nextRecordId();
rs1.setRecord(rid,tdata,0,tdata.length);
}else {
rs1.addRecord(tdata,0,tdata.length);
tdata=null;
}
re.destroy();
tdata=null;
}catch(Exception e){}
try{
rs1.closeRecordStore();
}catch(Exception e){}
}
rs1=null;
}
public static void readTollGate(){
gate=0;
point2=0;
if(svPoint.isExistRs("gate")){
RecordStore rs1=null;
try{
rs1=RecordStore.openRecordStore("gate",false);
}catch(Exception e){}
try{
RecordEnumeration re=rs1.enumerateRecords(null,new rmsComparator(),false);
if (re.hasNextElement()){
int rid=re.nextRecordId();
byte[] tdata=rs1.getRecord(rid);
svPoint.deserialize2(tdata);
tdata=null;
}
re.destroy();
}catch(Exception e){}
try{
rs1.closeRecordStore();
}catch(Exception e){}
rs1=null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -