erFormatException e) {
}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level > 1)
tfLevel.setText("" + (level - 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
plTipBlock.fanning();
}
});
/**
*Timer类的对象可以以一定的时间间隔触发一个或多个事件
*建立一个定时器要创建一个Timer对象,
注册一个或多个动作监听器
*并用start方法启动定时器
*此处的时间间隔为500毫秒,显示玩家的得分,难度级别
*/
timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//显示玩家的得分
tfScore.setText("" + game.getScore());
//按得分晋升难度级别
int scoreForLevelUpdate =
game.getScoreForLevelUpdate();
//显示更新后的难度级别
if (scoreForLevelUpdate >= ErsBlocksGame.PER_LEVEL_SCORE
&;&; scoreForLevelUpdate > 0)
game.levelUpdate();
}
});
timer.start();//启动定时器
}
/**
* 设置预显窗口的样式,
* @param style int,对应ErsBlock类的STYLES中的28个值
*/
public void setTipStyle(int style) {
plTipBlock.setStyle(style);
}
/**
* 取得用户设置的游戏等级。
* @return int, 难度等级,1-ErsBlocksGame.MAX_LEVEL
*/
public int getLevel() {
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
} catch (NumberFormatException e) {
}
return level;
}
/**
* 让用户修改游戏难度等级。
* @param level 修改后的游戏难度等级
*/
public void setLevel(int level) {
if (level > 0 &;&; level < 11) tfLevel.setText("" + level);
}
/**
* 设置"开始"按钮的状态。
*/
public void setPlayButtonEnable(boolean enable) {
btPlay.setEnabled(enable);
}
//根据pause的值设置按钮的显示标签
public void setPauseButtonLabel(boolean pause) {
btPause.setText(pause ? "暂停" : "继续");
}
/**
* 重置控制面板,得分置为0
*/
public void reset() {
tfScore.setText("0");
plTipBlock.setStyle(0);
}
/**
* 重新计算TipPanel里的boxes[][]里的方块的大小
*/
public void fanning() {
plTipBlock.fanning();
}
/**
* 提示信息面板的实现细节类
*/
private class TipPanel extends JPanel {
//提示信息面板的前景和背景颜色
private Color backColor = Color.darkGray, frontColor = Color.lightGray;
//创建设定的行数和列数的俄罗斯方块
private ErsBox[][] boxes =
new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];
//块的形态、宽度和高度
private int style, boxWidth, boxHeight;
//isTiled是否平铺
private boolean isTiled = false;
/**
* 预提示窗口类构造函数
*/
public TipPanel() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++)
boxes[i][j] = new ErsBox(false);
}
}
/**
* 预提示窗口类构造函数
* @param backColor Color, 窗口的背景色
* @param frontColor Color, 窗口的前景色
*/
public TipPanel(Color backColor, Color frontColor) {
this();
this.backColor = backColor;
this.frontColor = frontColor;
}
/**
* 设置预显窗口的方块样式
* @param style int,对应ErsBlock类的STYLES中的28个值
*/
public void setStyle(int style) {
this.style = style;
repaint();
}
/*