📄 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 + -