?? global.java
字號:
package cn.itcast.tetris.util;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
/**
*
*
* 工具類<BR>
* <BR>
* 此類中存放了其他類中用到的一些常量<BR>
* 并且支持配置文件<BR>
* 配置文件的路徑為游戲運行的目錄, 文件名為 tetris.ini<BR>
* <BR>
* 配置文件的寫法請參見字段的注釋<BR>
* <BR>
* 配置文件中設置項可以只寫需要配置的, 沒有寫的設置項默認為缺省值<BR>
* 各配置項的缺省值請參見字段的注釋<BR>
* <BR>
* 每個配置項都有設置值范圍, 超出范圍(無效)的設置值將不起作用<BR>
* 設置值的范圍請參見字段的注釋<BR>
*
* @version 1.0, 08/01/08
*
* @author 湯陽光
*
*/
public class Global {
private static Properties properties = new Properties();
/**
* 配置文件的路徑(默認為當前目錄下的 snake.ini文件)
*/
private static String CONFIG_FILE = "tetris.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>
* 缺省值為 15
*/
public static final int WIDTH;
/**
* 用格子表示的高度, (高度為多少個格子), 單位:格<BR>
* 對應的配置文件中的關鍵字為: height<BR>
* 范圍10 至 60<BR>
* 缺省值為20
*/
public static final int HEIGHT;
/**
* 圖形下落的初始速度 (單位: 毫秒/格)<BR>
* 對應的配置文件中的關鍵字為: speed<BR>
* 范圍10 至 無限大<BR>
* 缺省值為 200
*/
public static final int DEFAULT_SPEED;
/**
* 保存當前游戲中圖形的下落速度
*/
public static int CURRENT_SPEED;
/**
* 圖形快速下落的速度 (單位: 毫秒/格)<BR>
* 對應的配置文件中的關鍵字為: swift_speed<BR>
* 范圍0 至 無限大<BR>
* 缺省值為 15
*/
public static final int SWIFT_SPEED;
/**
* 每次加速或減速的幅度 (單位: 毫秒/格)<BR>
* 對應的配置文件的關鍵字為: speed_step<BR>
* 范圍1 至 無限大<BR>
* 缺省值為 25
*/
public static final int SPEED_STEP;
/**
* 消除滿行前暫停效果的時間(單位: 毫秒)<BR>
* 對應的配置文件的關鍵字為: stay_time<BR>
* 范圍0 至 無限大<BR>
* 缺省值為 200
*/
public static final int DEFAULT_STAY_TIME;
/**
* 消除滿行前暫停效果的時間
*/
public static int STAY_TIME;
private static Random random = new Random();
public static final String TITLE_LABEL_TEXT;
public static final String INFO_LABEL_TEXT;
private static final Color[] DEFAULT_COLORS = new Color[] {
new Color(0x990066), new Color(0x990099), new Color(0x330099),
new Color(0x663300), new Color(0x009966), new Color(0x003333) };
public static final List<Color> COMMON_COLORS;
/**
* 返回一個隨機的顏色
*
* @return
*/
public static Color getRandomColor() {
return DEFAULT_COLORS[random.nextInt(DEFAULT_COLORS.length)];
}
/**
* 默認的構造器, 私有的, 不能生成這個類的實例
*/
private Global() {
}
/**
* 初始化常量
*/
static {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(CONFIG_FILE);
properties.load(inputStream);
} catch (FileNotFoundException e) {
System.out.println("沒有配置文件");
} catch (IOException e) {
e.printStackTrace();
} 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 : 15;
HEIGHT = (temp = getIntValue("height")) != null && temp <= 60
&& temp >= 10 ? temp : 20;
DEFAULT_SPEED = CURRENT_SPEED = (temp = getIntValue("speed")) != null
&& temp >= 10 ? temp : 300;
SWIFT_SPEED = (temp = getIntValue("swift_speed")) != null && temp >= 0 ? temp
: 15;
SPEED_STEP = (temp = getIntValue("speed_step")) != null && temp >= 1 ? temp
: 25;
DEFAULT_STAY_TIME = STAY_TIME = (temp = getIntValue("stay_time")) != null
&& temp >= 0 ? temp : 200;
int defaultCellSize = (temp = getIntValue("cell_size")) != null
&& temp > 0 && temp <= 100 ? temp : 23;
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;
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;
COMMON_COLORS = loadColors();
}
/**
* 要考慮多種情況<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;
}
}
@SuppressWarnings("unchecked")
private static List<Color> loadColors() {
List<Color> l = new ArrayList<Color>(7);
for (int i = 0; i < 7; i++)
l.add(null);
Set set = properties.keySet();
Iterator iter = set.iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
if ("1".equals(key.trim()))
addColor(l, 0, getValue(key));
else if ("2".equals(key.trim()))
addColor(l, 1, getValue(key));
else if ("3".equals(key.trim()))
addColor(l, 2, getValue(key));
else if ("4".equals(key.trim()))
addColor(l, 3, getValue(key));
else if ("5".equals(key.trim()))
addColor(l, 4, getValue(key));
else if ("6".equals(key.trim()))
addColor(l, 5, getValue(key));
else if ("7".equals(key.trim()))
addColor(l, 6, getValue(key));
}
for (int i = 0; i < 7; i++) {
l.remove(null);
}
if (l.size() < 1) {
for (int i = 0; i < DEFAULT_COLORS.length; i++) {
l.add(DEFAULT_COLORS[i]);
}
} else {
if (l.size() != 7)
System.out.println("您一共設置了 " + l.size() + " 種有效顏色, 建議設置七種");
return l.subList(0, l.size() > 7 ? 7 : l.size());
}
return l;
}
@SuppressWarnings("unchecked")
private static void addColor(List l, int index, String str) {
str = str.trim();
if (!str.startsWith("0x") || str.length() < 3) {
System.out.println("顏色設置有誤,請檢查 : " + str + " (key)");
return;
}
try {
String strRGB = str.substring(2, str.length() >= 8 ? 8 : str
.length());
int rgb = Integer.valueOf(strRGB, 16);
Color c = new Color(rgb);
if (c != null) {
l.add(index, c);
}
} catch (Exception e) {
System.out.println("(e)顏色設置有誤,請檢查:" + str + "(key)");
e.printStackTrace();
return;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -