?? global.java
字號:
package cn.itcast.snake.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
* 工具類<BR>
* <BR>
* 此類中存放了其他類中用到的一些常量<BR>
* 并且支持配置文件<BR>
* 配置文件的路徑為游戲運行的目錄, 文件名為 snake.ini<BR>
* <BR>
* 配置文件的寫法請參見字段的注釋<BR>
* <BR>
* 配置文件中設置項可以只寫需要配置的, 沒有寫的設置項默認為缺省值<BR>
* 各配置項的缺省值請參見字段的注釋<BR>
* <BR>
* 每個配置項都有設置值范圍, 超出范圍(無效)的設置值將不起作用<BR>
* 設置值的范圍請參見字段的注釋<BR>
*
* @version 1.0, 01/01/08
*
* @author 湯陽光
*
*/
public class Global {
private static Properties properties = new Properties();
/**
* 配置文件的路徑(默認為當前目錄下的 snake.ini文件)
*/
private static String CONFIG_FILE = "snake.ini";
/**
* 一個格子的寬度, 單位:像素 <BR>
* 對應的配置文件中的關鍵字為: cell_width, 或用 cell_size指定<BR>
* 范圍1 至 100<BR>
* 缺省值為 23
*/
public static final int CELL_WIDTH;
/**
* 一個格子的高度, 單位:像素 <BR>
* 對應的配置文件中的關鍵字為: cell_width, 或用 cell_size指定<BR>
* 范圍1 至 100<BR>
* 缺省值為 23
*/
public static final int CELL_HEIGHT;
/**
* 用格子表示的寬度, (寬度為多少個格子) 單位:格 <BR>
* 對應的配置文件中的關鍵字為: width<BR>
* 范圍10 至 80<BR>
* 缺省值為 30
*/
public static final int WIDTH;
/**
* 用格子表示的高度, (高度為多少個格子), 單位:格)<BR>
* 對應的配置文件中的關鍵字為: height<BR>
* 范圍10 至 60<BR>
* 缺省值為20
*/
public static final int HEIGHT;
/**
* 顯示的像素寬度 (格子寬度度 * 每一格的寬度)
*/
public static final int CANVAS_WIDTH;
/**
* 顯示的像素高度 (格子高度 * 每一格的高度)
*/
public static final int CANVAS_HEIGHT;
/**
* 蛇的初始長度, 對應的配置文件中的關鍵字為: init_length<BR>
* 范圍2 至 最大寬度<BR>
* (單位:格) 缺省值為 2
*/
public static final int INIT_LENGTH;
/**
* 蛇的初始速度 (單位: 毫秒/格)<BR>
* 對應的配置文件中的關鍵字為: speed<BR>
* 范圍10 至 無限大<BR>
* 缺省值為 200
*/
public static final int SPEED;
/**
* 蛇每次加速或減速的幅度 (單位: 毫秒/格)<BR>
* 對應的配置文件的關鍵字為: speed_step<BR>
* 范圍1 至 無限大<BR>
* 缺省值為 25
*/
public static final int SPEED_STEP;
public static final String TITLE_LABEL_TEXT;
public static final String INFO_LABEL_TEXT;
/**
* 默認的構造器, 私有的, 不能生成這個類的實例
*/
private Global() {
}
/**
* 初始化常量
*/
static {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(CONFIG_FILE);
properties.load(inputStream);
} catch (Exception e) {
System.out.println("沒有配置文件");
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Integer temp = null;
/* 沒有設置或設置的無效時要有一個默認值 */
WIDTH = (temp = getIntValue("width")) != null && temp <= 80
&& temp >= 10 ? temp : 35;
HEIGHT = (temp = getIntValue("height")) != null && temp <= 60
&& temp >= 10 ? temp : 20;
INIT_LENGTH = (temp = getIntValue("init_length")) != null && temp > 1
&& temp < WIDTH ? temp : 2;
SPEED = (temp = getIntValue("speed")) != null && temp >= 10 ? temp
: 200;
SPEED_STEP = (temp = getIntValue("speed_step")) != null && temp >= 1 ? temp
: 25;
int defaultCellSize = (temp = getIntValue("cell_size")) != null
&& temp > 0 && temp <= 100 ? temp : 20;
CELL_WIDTH = (temp = getIntValue("cell_width")) != null && temp > 0
&& temp <= 100 ? temp : defaultCellSize;
CELL_HEIGHT = (temp = getIntValue("cell_height")) != null && temp > 0
&& temp <= 100 ? temp : defaultCellSize;
CANVAS_WIDTH = WIDTH * CELL_WIDTH;
CANVAS_HEIGHT = HEIGHT * CELL_HEIGHT;
String tempStr = null;
TITLE_LABEL_TEXT = (tempStr = getValue("title")) == null ? "說明:"
: tempStr;
INFO_LABEL_TEXT = (tempStr = getValue("info")) == null ? "方向鍵控制方向, 回車鍵暫停/繼續\nPAGE UP, PAGE DOWN 加速或減速\n\n更多請看 www.itcast.cn "
: tempStr;
}
/**
* 要考慮多種情況<BR>
* 1. 沒有這個key和value<BR>
* 2 有key 沒有 value
*/
private static Integer getIntValue(String key) {
if (key == null)
throw new RuntimeException("key 不能為空");
try {
return new Integer(properties.getProperty(key));
} catch (NumberFormatException e) {
return null;
}
}
private static String getValue(String key) {
try {
return new String(properties.getProperty(key).getBytes("iso8859-1"));
} catch (Exception e) {
return null;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -