?? gamerecord.java
字號:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
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;
/**
* 游戲存儲RMS
* @author univasity
*
*/
public class GameRecord {
private final int MAXRECORD = 3; //最大存儲量
private RecordStore rs;
private DataInputStream dis;
private DataOutputStream dos;
private ByteArrayOutputStream baos;
private String rc_name;
GameRecord(String name){
try {
rc_name = name;
rs = RecordStore.openRecordStore(rc_name, true); //打開記錄倉儲
if(rs.getNumRecords()==0){ //如果為新倉儲,則初始化它
byte[] test = toByte("",0);
for(int i=0; i<MAXRECORD; i++)
rs.addRecord(test, 0, test.length);
}
release();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 添加記錄
* @param name 姓名
* @param score 分數
* @throws RecordStoreNotOpenException
* @throws RecordStoreFullException
* @throws RecordStoreException
* @throws IOException
*/
public void addRecord(String name,int score) throws RecordStoreNotOpenException, RecordStoreFullException, RecordStoreException, IOException{
rs = RecordStore.openRecordStore(rc_name, true); //打開記錄倉儲
if(rs!=null){
if(isNewRecord(score)){
byte[] rc_data = toByte(name,score); //把需要存入的數據轉換好
if(rs.getNumRecords()<MAXRECORD){
rs.addRecord(rc_data, 0, rc_data.length);
}else{
rs.setRecord(MAXRECORD, rc_data, 0, rc_data.length);
}
sortRecord(); //記錄排序
}
}
release();
}
/**
* 排序 - 由大到小整理記錄
* @throws RecordStoreException
* @throws InvalidRecordIDException
* @throws RecordStoreNotOpenException
*/
public void sortRecord() throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException {
for(int out=rs.getNumRecords(); out>1; out--){
for(int i=1; i<out; i++){
byte[] a = rs.getRecord(i);
byte[] b = rs.getRecord(i+1);
if(byteToInt(a)<byteToInt(b)){
rs.setRecord(i, b, 0, b.length);
rs.setRecord(i+1, a, 0, a.length);
}
}
}
}
/**
* 顯示記錄
* @param g 畫筆
* @param w 屏幕寬
* @param h 屏幕高
* @param x 顯示的首坐標x
* @param y 顯示的首坐標y
* @param x_border 名字與分數的間距
* @param y_border 行與行的間距
* @param color 字體顏色
* @param font 字體類型
* @throws RecordStoreNotOpenException
* @throws InvalidRecordIDException
* @throws RecordStoreException
* @throws IOException
*/
public void showRecord(Graphics g,int w,int h,int x,int y,int x_border,int y_border,int color,Font font) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException{
g.setClip(0, 0, w, h);
g.setColor(color);
if(font!=null)
g.setFont(font);
//信息
g.drawString("名字", x, y, 20);
g.drawString("分數", x+x_border, y, 20);
y+=h/16;
rs = RecordStore.openRecordStore(rc_name, false); //打開記錄倉儲
for(int i=1; i<=MAXRECORD; i++){
dis = new DataInputStream(new ByteArrayInputStream(rs.getRecord(i)));
g.drawString(dis.readUTF(), x, y+y_border*(i-1), 20);
g.drawString(""+dis.readInt(), x+x_border, y+y_border*(i-1), 20);
dis.close();
}
release();
}
/**
* 將數據轉換為字節數組
* @param str
* @return
*/
private byte[] toByte(String str,int n){
dos = new DataOutputStream(baos = new ByteArrayOutputStream());
try {
dos.writeUTF(str);
dos.writeInt(n);
dos.flush();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] temp = baos.toByteArray();
return temp;
}
/**
* 是否為新記錄
* @param score 新記錄
* @return
* @throws RecordStoreException
* @throws InvalidRecordIDException
* @throws RecordStoreNotOpenException
* @throws IOException
*/
public boolean isNewRecord(int score) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException{
rs = RecordStore.openRecordStore(rc_name, false); //打開記錄倉儲
int temp = byteToInt(rs.getRecord(rs.getNumRecords())); //讀取最后一條記錄
release(); //釋放資源
if(score>temp)
return true;
else
return false;
}
/**
* 字節數組到整數的轉換
* @param bt 需要轉換的數據
* @return
*/
private int byteToInt(byte[] bt){
dis = new DataInputStream(new ByteArrayInputStream(bt));
int n = -1;
try {
dis.readUTF();
n = dis.readInt();
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
return n;
}
/**
* 釋放資源
* @throws RecordStoreNotOpenException
* @throws RecordStoreException
* @throws IOException
*/
public void release() throws RecordStoreNotOpenException, RecordStoreException, IOException{
if(dis!=null)
dis.close();
if(dos!=null)
dos.close();
if(rs!=null)
rs.closeRecordStore();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -