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