?? controlpanel.java
字號:
/**
* File: ControlPanel.java
* User: Administrator
* Describe: 俄羅斯方塊的 Java 實現
*/
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
/**
* 控制面板類,繼承自JPanel.
* 上邊安放預顯窗口、等級、得分、控制按鈕
* 主要用來控制游戲進程。
*/
public class ControlPanel extends JPanel
{
private JTextField tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),
tfScore = new JTextField("0");
private JButton btPlay = new JButton("Play"),//
btStop = new JButton("Stop"),
btTurnLevelUp = new JButton("Turn hard"),
btTurnLevelDown = new JButton("Turn easy");
public JButton btPause = new JButton("Pause");
private JPanel plTip = new JPanel(new BorderLayout());
private TipPanel plTipBlock = new TipPanel();//創建TipPanel對象plTipBlock
private JPanel plInfo = new JPanel(new GridLayout(4, 1));
private JPanel plButton = new JPanel(new GridLayout(5, 1));
private Timer timer;
private ErsBlocksGame game;//通過game對象,連接主類和控制面板類
//設置邊框樣式:
private Border border = new EtchedBorder(
EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));
/**
* 控制面板類的構造函數
* @param game ErsBlocksGame, ErsBoxesGame類的一個實例引用,
* 方便直接控制ErsBoxesGame類的行為。
*/
public ControlPanel(final ErsBlocksGame game) //傳遞一個主類的對象
{
setLayout(new GridLayout(3, 1, 0, 8));//三行一列框架
this.game = game;
plTip.add(new JLabel("Next block:"), BorderLayout.NORTH);
plTip.add(plTipBlock);//初始化預覽區域
plTip.setBorder(border);
plInfo.add(new JLabel("Level:"));
plInfo.add(tfLevel);
plInfo.add(new JLabel("Score:"));
plInfo.add(tfScore);
plInfo.setBorder(border);
tfLevel.setEditable(false);
tfScore.setEditable(false);
plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);
//添加三個面板
add(plTip);
add(plInfo);
add(plButton);
addKeyListener(new ControlKeyListener());//控制面板添加監聽器, 此參數繼承自keyListner
btPlay.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
game.playGame();//play按紐動作監聽器,調用主類的playGrame()方法
}
});
//暫停按紐添加動作事件監聽器
btPause.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (btPause.getText().equals(new String("Pause")))
{
game.pauseGame();//調用主類的pauseGame()方法
}
else
{
game.resumeGame();// 調用主類的resumeGame()方法
}
}
});
btStop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
game.stopGame();//調用主類的stopGame()方法
}
});
btTurnLevelUp.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try {
int level = Integer.parseInt(tfLevel.getText());//獲取控制面板上的游戲級別
if (level < ErsBlocksGame.MAX_LEVEL)//判斷是否小于最大級別
tfLevel.setText("" + (level + 1));//游戲級別加一級
} catch (NumberFormatException 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 = 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);
}
public void setPauseButtonLabel(boolean pause)
{
btPause.setText(pause ? "Pause" : "Continue");
}
public void setPause(boolean pause)
{
btPause.setEnabled(pause);
}
public void setStop(boolean stop)
{
btStop.setEnabled(stop);
}
public void setTurnLevelUp(boolean LevelUp)
{
btTurnLevelUp.setEnabled(LevelUp);
}
public void setTurnLeveldown(boolean Leveldown)
{
btTurnLevelDown.setEnabled(Leveldown);
}
/**
* 重置控制面板
*/
public void reset()
{
tfScore.setText("0");//成績為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;
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();
}
/**
* 覆蓋JComponent類的函數,畫組件。
* @param g 圖形設備環境
*/
public void paintComponent(Graphics g)
{
//System.out.println("paintComponent");
super.paintComponent(g);
if (!isTiled) fanning();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++)
{
for (int j = 0; j < boxes[i].length; j++)
{
Color color = (((key & style) != 0) ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,boxWidth, boxHeight, true);
key >>= 1;
}
}
}
/**
* 根據窗口的大小,自動調整方格的尺寸
*/
public void fanning()
{
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}
private class ControlKeyListener extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
if (!game.isPlaying()) return;
ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode())
{
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
case KeyEvent.VK_S:
block.moveDown();
break;
case KeyEvent.VK_A:
block.moveLeft();
break;
case KeyEvent.VK_D:
block.moveRight();
break;
case KeyEvent.VK_W:
block.turnNext();
break;
default:
break;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -