📄 gamepanel.java
字号:
/**
* GamePanel.java
* Summary 游戏面板
* Created on 2002-3-2
* @author Dorian
*/
package com.Dorian.Tetris;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import com.Dorian.Tetris.square.*;
public class GamePanel extends JPanel{
public GamePanel() {
initComponent();
setupGame();
}
private void initComponent() {
labBackground = new JLabel();
labBackground.setIcon(getPaintIcon("Background.jpg"));
labBackground.setBounds(0,0,520,580);
cobMode = new JComboBox(strMode);
cobMode.setBounds(404,190,80,20);
cobMode.addActionListener(new ModeChoose());
cobSpeed = new JComboBox(strChallenge);
cobSpeed.setBounds(404,230,80,20);
cobSpeed.setEnabled(false);
cobSpeed.addActionListener(new SpeedChoose());
cobChallenge = new JComboBox(strChallenge);
cobChallenge.setBounds(404,270,80,20);
cobChallenge.setEnabled(false);
paintPanel = new PaintPanel();
paintPanel.setBounds(33,78,351,467);
random = new Random();
currentSquare = random.nextInt(8);
nextSquare = random.nextInt(8);
labNextSquare = new JLabel();
labNextSquare.setBounds(412,325,69,69);
cmdStart = new JButton();
cmdPause = new JButton();
cmdPause.setVisible(false);
cmdRestart = new JButton();
cmdRestart.setVisible(false);
cmdClose = new JButton();
cmdClose.setFocusable(false);
titleBar = new JLabel();
titleBar.setBounds(100,9,304,41);
titleBar.setIcon(getPaintIcon("Title.jpg"));
titleBar.setCursor(new Cursor(Cursor.MOVE_CURSOR));
labScore = new JLabel("得分:" + score);
labScore.setBounds(400,115,180,30);
labScore.setFont(new Font("宋体",Font.BOLD,16));
labScore.setForeground(Color.BLUE);
labMode = new JLabel("选择模式");
labMode.setBounds(402,155,80,30);
labMode.setFont(new Font("宋体",Font.BOLD,15));
labMode.setForeground(Color.BLUE);
labGameOver = new JLabel("Game Over");
labGameOver.setBounds(100,200,300,100);
labGameOver.setFont(new Font("宋体",Font.BOLD,48));
labGameOver.setForeground(Color.RED);
labGameOver.setVisible(false);
timer = new Timer(time,new GameTimer());
initSquare();
score = 0;
}
private void initSquare() {
currentSquare = nextSquare;
nextSquare = random.nextInt(8);
showNextSquare(nextSquare);
switch(currentSquare) {
case 0: square = new SquareO();PaintPanel.imgFore=PaintPanel.imgBlack;break;
case 1: square = new SquareI();PaintPanel.imgFore=PaintPanel.imgCyan;break;
case 2: square = new SquareL();PaintPanel.imgFore=PaintPanel.imgGlod;break;
case 3: square = new SquareN();PaintPanel.imgFore=PaintPanel.imgGreen;break;
case 4: square = new SquareO();PaintPanel.imgFore=PaintPanel.imgBlack;break;
case 5: square = new SquareQ();PaintPanel.imgFore=PaintPanel.imgRed;break;
case 6: square = new SquareS();PaintPanel.imgFore=PaintPanel.imgPurple;break;
case 7: square = new SquareT();PaintPanel.imgFore=PaintPanel.imgYellow;break;
default: square = new SquareO();PaintPanel.imgFore=PaintPanel.imgBlack;break;
}
if(!square.isEmpty()) { //初始化失败,游戏结束
timer.stop();
gameOver();
}
square.initSquare();
}
private void setupGame() {
this.setLayout(null);
add(labGameOver);
add(paintPanel);
add(titleBar);
add(labScore);
add(labMode);
add(cobMode);
add(cobSpeed);
add(cobChallenge);
add(labNextSquare);
add(initButton(cmdStart,404,396,84,28,"StartButton.jpg",
"StartButtonM.jpg",new CmdStartListener()));
add(initButton(cmdPause,404,434,84,28,"PauseButton.jpg",
"PauseButtonM.jpg",new CmdPauseListener()));
add(initButton(cmdRestart,404,472,84,28,"RestartButton.jpg",
"RestartButtonM.jpg",new cmdRestartListener()));
add(initButton(cmdClose,427,9,42,41,"CloseButton.jpg",
"CloseButtonH.jpg",new ExitGameListener(this)));
add(labBackground);
labBackground.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent key) {
control(key);
}
});
}
//初始化Button
protected JButton initButton(JButton button,int x,int y,
int width,int height,
String image1,String image2,
ActionListener listener) {
button.setBorder(null);
button.setBorderPainted(false);
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.setFocusPainted(false);
button.setIcon(getPaintIcon(image1));
button.setMargin(new Insets(0,0,0,0));
button.setRolloverIcon(getPaintIcon(image2));
button.setSelectedIcon(null);
button.addActionListener(listener);
button.setBounds(x,y,width,height);
return button;
}
public void exitGame(ActionEvent e) {
if(timer.isRunning()) {
timer.stop();
timer = null;
}
imageDispose();
System.exit(0);
}
public void gameOver() {
labGameOver.setVisible(true);
cmdStart.setVisible(false);
cmdPause.setVisible(false);
cmdRestart.setVisible(true);
cobMode.setEnabled(true);
cobSpeed.setEnabled(true);
cobChallenge.setEnabled(true);
cmdRestart.grabFocus();
}
public void lineOver() {
boolean isCan; //标志是否循环消一行
boolean isContinue=true; //标志是否还有待消的行
int line = SquarePanel.HEIGHT-1;
while(isContinue) {
isCan=true;
while(isCan) {
for(int i=0;i<SquarePanel.WIDTH;i++) {
if(SquarePanel.squareArray[i][line] != true)
isCan=false;
}
if(isCan) {
for(int i=0;i<SquarePanel.WIDTH;i++)
SquarePanel.squareArray[i][line] = false;
for(int j=line-1;j>=0;j--)
for(int i=0;i<SquarePanel.WIDTH;i++) {
if(SquarePanel.squareArray[i][j] == true) {
SquarePanel.squareArray[i][j+1] = true;
SquarePanel.squareArray[i][j] = false;
}
}
repaint();
score += 10;
}
}
line--;
if(line<1)
isContinue=false;
showScore();
}
}
protected class GameTimer implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(!square.isMoveDown()) {
if(square instanceof SquareO )
transparency();
lineOver();
initSquare();
}
square.moveDown();
repaint(33,78,351,467);
}
}
protected class CmdStartListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
timer.start();
cmdPause.setVisible(true);
cmdStart.setVisible(false);
cmdRestart.setVisible(false);
paintPanel.repaint();
labGameOver.setVisible(false);
cobMode.setEnabled(false);
cobSpeed.setEnabled(false);
cobChallenge.setEnabled(false);
labBackground.grabFocus();
}
}
protected class CmdPauseListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
timer.stop();
cmdStart.setVisible(true);
cmdRestart.setVisible(true);
cmdPause.setVisible(false);
}
}
protected class cmdRestartListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
cmdPause.setVisible(true);
cmdStart.setVisible(false);
cmdRestart.setVisible(false);
paintPanel.repaint();
labGameOver.setVisible(false);
for(int x=0;x<SquarePanel.WIDTH;x++)
for(int y=0;y<SquarePanel.HEIGHT;y++)
SquarePanel.squareArray[x][y] = false;
score = 0;
labScore.setText("得分:" + score);
initSquare();
cobMode.setEnabled(false);
cobSpeed.setEnabled(false);
cobChallenge.setEnabled(false);
timer.start();
labBackground.grabFocus();
}
}
public void control(KeyEvent key) {
switch(key.getKeyCode()) {
case KeyEvent.VK_UP:
square.change();
repaint(33,78,351,467);
repaint();
break;
case KeyEvent.VK_DOWN:
square.moveDown();
repaint(33,78,351,467);
break;
case KeyEvent.VK_LEFT:
square.moveLeft();
repaint(33,78,351,467);
break;
case KeyEvent.VK_RIGHT:
square.moveRight();
repaint(33,78,351,467);
break;
default:
break;
}
}
public void showInfo(Graphics g) {
}
public void transparency() {
int x = square.getLocalX();
int y = square.getLocalY();
int i = square.getLocalX();
for(int j=square.getLocalY();j<SquarePanel.HEIGHT;j++)
if(SquarePanel.squareArray[i][j] == false) {
SquarePanel.squareArray[x][y] = false;
SquarePanel.squareArray[i][j] = true;
x = i;
y = j;
}
}
public void showScore() {
labScore.setText("得分:" + score);
}
public void showNextSquare(int num) {
preBlack = getPaintIcon("PreBlack.jpg");
preCyan = getPaintIcon("PreCyan.jpg");
preGlod = getPaintIcon("PreGlod.jpg");
preGreen = getPaintIcon("PreGreen.jpg");
preRed = getPaintIcon("PreRed.jpg");
prePurple = getPaintIcon("PrePurple.jpg");
preYellow = getPaintIcon("PreYellow.jpg");
switch(num) {
case 0: preImage = preBlack;break;
case 1: preImage = preCyan;break;
case 2: preImage = preGlod;break;
case 3: preImage = preGreen;break;
case 4: preImage = preBlack;break;
case 5: preImage = preRed;break;
case 6: preImage = prePurple;break;
case 7: preImage = preYellow;break;
default:preImage = preBlack;break;
}
labNextSquare.setIcon(preImage);
}
protected class ModeChoose implements ActionListener {
public void actionPerformed(ActionEvent event) {
switch(cobMode.getSelectedIndex()) {
case 0:
cobSpeed.setEnabled(false);
cobChallenge.setEnabled(false);
time = 1000;
break;
case 1:
cobSpeed.setEnabled(true);
cobChallenge.setEnabled(false);
time = 1000;
break;
case 2:
cobSpeed.setEnabled(true);
cobChallenge.setEnabled(true);
time = 1000;
break;
default:break;
}
timer.setDelay(time);
}
}
protected class SpeedChoose implements ActionListener {
public void actionPerformed(ActionEvent event) {
switch(cobSpeed.getSelectedIndex()) {
case 0:time=1000;break;
case 1:time=800;break;
case 2:time=600;break;
case 3:time=400;break;
case 4:time=200;break;
default:break;
}
timer.setDelay(time);
}
}
public void imageDispose() {
PaintPanel.imgBack.flush(); //释放图片资源
PaintPanel.imgBlack.flush();
PaintPanel.imgCyan.flush();
PaintPanel.imgFore.flush();
PaintPanel.imgGlod.flush();
PaintPanel.imgGreen.flush();
PaintPanel.imgPink.flush();
PaintPanel.imgPurple.flush();
PaintPanel.imgRed.flush();
PaintPanel.imgYellow.flush();
}
private ImageIcon getPaintIcon(String strImage) {
ImageIcon icon = new ImageIcon("images/" + strImage);
return icon;
}
private Square square;
private PaintPanel paintPanel;
private Timer timer;
private JLabel labBackground;
private JButton cmdStart;
private JButton cmdPause;
private JButton cmdRestart;
private JButton cmdClose;
private JLabel titleBar;
private JLabel labScore;
private JLabel labMode;
private JLabel labGameOver;
private JLabel labNextSquare;
private JComboBox cobMode,cobSpeed,cobChallenge;
private int score;
private int time = 1000;
private Random random;
private int currentSquare;
private int nextSquare;
private ImageIcon preBlack,preCyan,preGlod,preGreen,
preRed,prePurple,preYellow,preImage;
private static final String[] strMode = {"easy","normal","hard"};
private static final String[] strChallenge = {"1","2","3","4","5"};
private final Font largefont = new Font("宋体", Font.BOLD, 28);
private final Font smallfont = new Font("宋体", Font.BOLD, 12);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -