📄 mineprops.java
字号:
/*
* MineProps.java 1.0 2003-06-25
*
* Copyleft (c) 2003 RatKing.
*/
package jmine;
import java.io.*;
import java.util.Properties;
/**
* 保存程序运行时设置的各种参数和属性
*
* @author <a href="ratking@ynet.com">RatKing</a>
* @version 1.0
*/
public class MineProps {
private static final int TOTAL_LIST = 3;
private static final String FILE_NAME = "JMine.properties";
public static final String WINDOZ = "windoz";
public static final String METAL = "metal";
public static final String MOTIF = "motif";
private Properties appProps = new Properties();
// 将保存的属性
private int[] time = new int[] {999, 999, 999}; // 秒数
private String[] name = new String[] {"佚名", "佚名", "佚名"}; // 姓名
public String lookAndFeel = WINDOZ; // 外观感觉
public int gameLevel = 0; // 游戏等级JMinePanel.LOW_LEVEL
public boolean isMark = true; // 是否作标记
public boolean isColor = true; // 是否多颜色
public boolean isSound = true; // 是否有声音
public MineProps() {
}
/**
* 从文件中载入属性
*/
public void load() {
InputStream in = null;
try {
in = new FileInputStream(FILE_NAME);
appProps.load(in);
} catch(IOException e) {
System.err.println(e);
} catch(Exception e2) {
e2.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e2) {
}
}
}
// 取出属性
for (int i = 0; i < TOTAL_LIST; i++) {
name[i] = appProps.getProperty("name" + i, "佚名");
time[i] = getInt(appProps.getProperty("time" + i, "999"), 999);
}
lookAndFeel = appProps.getProperty("lookAndFeel", WINDOZ);
gameLevel = getInt(appProps.getProperty("gameLevel", "0"), MinePanel.LOW_LEVEL);
isMark = getBoolean(appProps.getProperty("mark", "true"));
isColor = getBoolean(appProps.getProperty("color", "true"));
isSound = getBoolean(appProps.getProperty("sound", "true"));
} // load()
/**
* 保存属性到文件中
*/
public void save() {
OutputStream out = null;
String comment = "--- JMine's properties file(generated by JMine) ---";
// 更新appProps
for (int i = 0; i < TOTAL_LIST; i++) {
appProps.setProperty("name" + i, name[i]);
appProps.setProperty("time" + i, String.valueOf(time[i]));
}
appProps.setProperty("lookAndFeel", lookAndFeel);
appProps.setProperty("gameLevel", String.valueOf(gameLevel));
appProps.setProperty("mark", String.valueOf(isMark));
appProps.setProperty("color", String.valueOf(isColor));
appProps.setProperty("sound", String.valueOf(isSound));
try {
out = new FileOutputStream(FILE_NAME);
appProps.store(out, comment);
} catch(IOException e) {
System.err.println(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e2) {
}
}
}
} // save()
public String getName(int gameLevel) {
if (gameLevel < 0 || gameLevel >= TOTAL_LIST)
return "佚名";
return name[gameLevel];
}
public int getTime(int gameLevel) {
if (gameLevel < 0 || gameLevel >= TOTAL_LIST)
return 999;
return time[gameLevel];
}
/**
* 根据给定的姓名和时间,更新英雄榜。
* @return 当前姓名时间所排的名次(1~3)。当未进入英雄榜时返回0。
*/
public void setRecord(int gameLevel, String theName, int theTime) {
if (gameLevel < 0 || gameLevel >= TOTAL_LIST)
return;
name[gameLevel] = theName;
time[gameLevel] = theTime;
}
/**
* 是否此成绩打破当前级别的记录
* @param gameLevel 游戏级别
* @param theTime 完成任务用的时间
*/
public boolean isRecordBreaker(int gameLevel, int theTime) {
if (gameLevel < 0 || gameLevel >= TOTAL_LIST || theTime < 0 || theTime > 999)
return false;
if (theTime <= time[gameLevel])
return true;
else
return false;
}
/**
* 返回字符串所对应的整数。
* 如果字符串不是合法的整数,就返回默认值default
*/
private int getInt(String strNumber, int defaultInt) {
int n = defaultInt;
try {
n = Integer.parseInt(strNumber);
} catch (NumberFormatException e) {
e.printStackTrace();
}
return n;
}
/**
* 返回字符串所对应的布尔值。
* 如果字符串是"true"(不区分大小写),就返回true;否则返回false
*/
private boolean getBoolean(String strBoolean) {
boolean b = false;
if (Boolean.TRUE.equals(new Boolean(strBoolean))) {
b = true;
}
return b;
}
/**
* 重置英雄榜
*/
public void resetTopList() {
for (int i = 0; i < TOTAL_LIST; i++) {
setRecord(i, "佚名", 999);
}
//save();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -