📄 mainframe.java
字号:
package tetris;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class MainFrame extends JFrame {
JPanel contentPane;//窗体内容网格
JMenuBar jMenuBar1 = new JMenuBar();//菜单条
JMenu jMenu1 = new JMenu();//文件菜单
JMenu jMenu2 = new JMenu();//选项菜单
JMenu jMenu3 = new JMenu();//初始化水平菜单
JMenuItem jMenuItem1 = new JMenuItem();//游戏开始菜单项
JMenuItem jMenuItem2 = new JMenuItem();//游戏暂停菜单项
JMenuItem jMenuItem3 = new JMenuItem();//游戏退出菜单项
//10个初始级别设置菜单项
JRadioButtonMenuItem jRadioButtonMenuItem1 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem2 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem3 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem4 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem5 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem6 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem7 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem8 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem9 = new JRadioButtonMenuItem();
JRadioButtonMenuItem jRadioButtonMenuItem10 = new JRadioButtonMenuItem();
ButtonGroup buttonGroup = new ButtonGroup();//各个初始级别菜单项的按钮组
JPanel jPanel1 = new JPanel();//游戏状态显示区面板
JPanel jPanel2 = new JPanel();//游戏区面板
JPanel jPanel3 = new JPanel();//下一个方块提示区面板
JLabel jLabel1 = new JLabel();//得分提示标签
JLabel jLabel2 = new JLabel();//得分标签
JLabel jLabel3 = new JLabel();//当前等级提示标签
JLabel jLabel4 = new JLabel();//当前等级标签
JLabel jLabel5 = new JLabel();//消除行数提示标签
JLabel jLabel6 = new JLabel();//消除行数标签
JLabel jLabel7 = new JLabel();//下一个方块提示标签
GridLayout gridLayout1 = new GridLayout();//游戏区布局
GridLayout gridLayout2 = new GridLayout();//下一个方块提示区布局
XYLayout xYLayout1 = new XYLayout();//主窗体布局
XYLayout xYLayout2 = new XYLayout();//游戏状态显示区布局
JButton playBlocks[][] ;//整个游戏区方块
JButton nextBlocks[][];//下一个方块提示区方块
int flagBlocks[][];//整个游戏区方块状态标志
long speed=500;//当前游戏速度
long initSpeed=500;//初始游戏速度
boolean isEnd=false;//游戏结束标志
boolean isPause=false;//游戏暂停标志
long score = 0;//得分
int totalLines = 0;//消除总行数
int currentLines = 0;//当前级别下消除行数
int level = 1;//当前级别
int initLevel = 1;//初始化级别
int nextBlockType=0;//下一个方块类型
Block block;//当前方块
TetrisThread thread = new TetrisThread();//游戏主线程
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jMenuBar1.setFont(new java.awt.Font("DialogInput", 0, 11));//设置组件的字体
contentPane.setLayout(xYLayout1);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(387, 498));
this.setTitle("俄罗斯方块");
this.addKeyListener(new MainFrame_this_keyAdapter(this));
jMenu1.setFont(new java.awt.Font("DialogInput", 0, 12));
jMenu1.setText("文件");
jMenuItem1.setFont(new java.awt.Font("DialogInput", 0, 12));
jMenuItem1.setText("开始");
jMenuItem1.addActionListener(new MainFrame_jMenuItem1_actionAdapter(this));
jMenuItem1.addMouseListener(new MainFrame_jMenuItem1_mouseAdapter(this));
jMenuItem2.setFont(new java.awt.Font("DialogInput", 0, 12));
jMenuItem2.setText("暂停");
jMenuItem2.addActionListener(new MainFrame_jMenuItem2_actionAdapter(this));
jMenuItem3.setFont(new java.awt.Font("DialogInput", 0, 12));
jMenuItem3.setToolTipText("");
jMenuItem3.setText("退出");
jMenu2.setFont(new java.awt.Font("DialogInput", 0, 12));
jMenu2.setText("选项");
jMenu3.setFont(new java.awt.Font("DialogInput", 0, 12));
jMenu3.setToolTipText("");
jMenu3.setText("初始水平");
jPanel1.setBorder(BorderFactory.createEtchedBorder());//设置游戏状态区面版的边框,产生浮雕效果
jPanel1.setLayout(xYLayout2);
jPanel1.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel2.setAlignmentX((float) 0.0);//设置垂直对齐方式
jPanel2.setAlignmentY((float) 0.0);//设置水平对齐方式
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setDebugGraphicsOptions(0);//启用或禁用与组件或其某个子组件内执行的每个图形操作有关的诊断信息,0则不会更改该调试选项
jPanel2.setLayout(gridLayout1);
jPanel2.addKeyListener(new MainFrame_this_keyAdapter(this));
jLabel1.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel1.setText("得分:");
jLabel2.setText("0");
jLabel2.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel2.setHorizontalTextPosition(SwingConstants.TRAILING);
jLabel3.setText("等级:");
jLabel3.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setText("0");
jLabel4.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel5.setText("行数:");
jLabel5.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel6.setText("0");
jLabel6.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel7.setText("下一个:");
jLabel7.setFont(new java.awt.Font("DialogInput", 0, 12));
jPanel3.setBorder(BorderFactory.createEtchedBorder());
jPanel3.setDebugGraphicsOptions(0);
jPanel3.setLayout(gridLayout2);
jPanel3.addKeyListener(new MainFrame_this_keyAdapter(this));
gridLayout1.setColumns(10);//将此布局中的列数设置为指定值
gridLayout1.setHgap(0);// 将组件之间的水平间距设置为指定值
gridLayout1.setRows(20);//将此布局中的行数设置为指定值
gridLayout2.setColumns(4);
gridLayout2.setRows(4);
gridLayout2.setVgap(0);//将组件之间的垂直间距设置为指定值
contentPane.setMinimumSize(new Dimension(483, 422));//将此组件的最低大小设置为一个常量值
contentPane.setPreferredSize(new Dimension(483, 422));//设置此组件的首选大小
jRadioButtonMenuItem1.setText("1");
jRadioButtonMenuItem1.addActionListener(new MainFrame_jRadioButtonMenuItem1_actionAdapter(this));
jRadioButtonMenuItem2.setText("2");
jRadioButtonMenuItem2.addActionListener(new MainFrame_jRadioButtonMenuItem2_actionAdapter(this));
jRadioButtonMenuItem3.setText("3");
jRadioButtonMenuItem3.addActionListener(new MainFrame_jRadioButtonMenuItem3_actionAdapter(this));
jRadioButtonMenuItem4.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem4.setText("4");
jRadioButtonMenuItem4.addActionListener(new MainFrame_jRadioButtonMenuItem4_actionAdapter(this));
jRadioButtonMenuItem5.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem5.setText("5");
jRadioButtonMenuItem5.addActionListener(new MainFrame_jRadioButtonMenuItem5_actionAdapter(this));
jRadioButtonMenuItem6.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem6.setText("6");
jRadioButtonMenuItem6.addActionListener(new MainFrame_jRadioButtonMenuItem6_actionAdapter(this));
jRadioButtonMenuItem7.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem7.setText("7");
jRadioButtonMenuItem7.addActionListener(new MainFrame_jRadioButtonMenuItem7_actionAdapter(this));
jRadioButtonMenuItem8.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem8.setText("8");
jRadioButtonMenuItem8.addActionListener(new MainFrame_jRadioButtonMenuItem8_actionAdapter(this));
jRadioButtonMenuItem9.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem9.setText("9");
jRadioButtonMenuItem9.addActionListener(new MainFrame_jRadioButtonMenuItem9_actionAdapter(this));
jRadioButtonMenuItem10.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButtonMenuItem10.setText("10");
jRadioButtonMenuItem10.addActionListener(new MainFrame_jRadioButtonMenuItem10_actionAdapter(this));
buttonGroup.add(jRadioButtonMenuItem1);
buttonGroup.add(jRadioButtonMenuItem2);
buttonGroup.add(jRadioButtonMenuItem3);
buttonGroup.add(jRadioButtonMenuItem4);
buttonGroup.add(jRadioButtonMenuItem5);
buttonGroup.add(jRadioButtonMenuItem6);
buttonGroup.add(jRadioButtonMenuItem7);
buttonGroup.add(jRadioButtonMenuItem8);
buttonGroup.add(jRadioButtonMenuItem9);
buttonGroup.add(jRadioButtonMenuItem10);
jRadioButtonMenuItem1.setSelected(true);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
jMenu2.add(jMenu3);
jMenu3.add(jRadioButtonMenuItem1);
jMenu3.add(jRadioButtonMenuItem2);
jMenu3.add(jRadioButtonMenuItem3);
jMenu3.add(jRadioButtonMenuItem4);
jMenu3.add(jRadioButtonMenuItem5);
jMenu3.add(jRadioButtonMenuItem6);
jMenu3.add(jRadioButtonMenuItem7);
jMenu3.add(jRadioButtonMenuItem8);
jMenu3.add(jRadioButtonMenuItem9);
jMenu3.add(jRadioButtonMenuItem10);
jPanel1.add(jLabel3, new XYConstraints(13, 65, -1, -1));
jPanel1.add(jLabel4, new XYConstraints(14, 87, -1, -1));
jPanel1.add(jLabel1, new XYConstraints(11, 15, -1, -1));
jPanel1.add(jLabel2, new XYConstraints(12, 38, -1, -1));
jPanel1.add(jLabel5, new XYConstraints(11, 111, -1, -1));
jPanel1.add(jLabel6, new XYConstraints(14, 135, -1, -1));
jPanel1.add(jLabel7, new XYConstraints(11, 157, -1, -1));
jPanel1.add(jPanel3, new XYConstraints(11, 182, 60, 61));
contentPane.add(jPanel2, new XYConstraints(149, 16, 214, 406));
contentPane.add(jPanel1, new XYConstraints(28, 16, 88, 255));
//创建并初始化游戏区方块数组
playBlocks = new JButton[20][10];
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 10; j++) {
playBlocks[i][j] = new JButton();
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
jPanel2.add(playBlocks[i][j]);
}
}
//创建并初始化游戏区方块标志数组
flagBlocks = new int[20][10];
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 10; j++) {
flagBlocks[i][j] = 0;
}
}
//创建并初始化方块提示区方块数组
nextBlocks = new JButton[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
nextBlocks[i][j] = new JButton();
nextBlocks[i][j].setBackground(Color.lightGray);
nextBlocks[i][j].setVisible(false);
jPanel3.add(nextBlocks[i][j]);
}
}
}//方法jbInit结束
void showNextBlock(int type){
//清空下一个方块提示区
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
nextBlocks[i][j].setBackground(Color.lightGray);
nextBlocks[i][j].setVisible(false);
}
}
//根据方块类型在提示区显示相应的方块
switch(type){
case 0:
nextBlocks[1][0].setBackground(Color.green);
nextBlocks[1][0].setVisible(true);
nextBlocks[1][1].setBackground(Color.green);
nextBlocks[1][1].setVisible(true);
nextBlocks[2][1].setBackground(Color.green);
nextBlocks[2][1].setVisible(true);
nextBlocks[2][2].setBackground(Color.green);
nextBlocks[2][2].setVisible(true);
break;
case 1:
nextBlocks[1][1].setBackground(Color.blue);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.blue);
nextBlocks[1][2].setVisible(true);
nextBlocks[2][1].setBackground(Color.blue);
nextBlocks[2][1].setVisible(true);
nextBlocks[2][0].setBackground(Color.blue);
nextBlocks[2][0].setVisible(true);
break;
case 2:
nextBlocks[1][1].setBackground(Color.cyan);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.cyan);
nextBlocks[1][2].setVisible(true);
nextBlocks[2][1].setBackground(Color.cyan);
nextBlocks[2][1].setVisible(true);
nextBlocks[2][2].setBackground(Color.cyan);
nextBlocks[2][2].setVisible(true);
break;
case 3:
nextBlocks[1][0].setBackground(Color.yellow);
nextBlocks[1][0].setVisible(true);
nextBlocks[1][1].setBackground(Color.yellow);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.yellow);
nextBlocks[1][2].setVisible(true);
nextBlocks[2][2].setBackground(Color.yellow);
nextBlocks[2][2].setVisible(true);
break;
case 4:
nextBlocks[1][1].setBackground(Color.magenta);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.magenta);
nextBlocks[1][2].setVisible(true);
nextBlocks[1][0].setBackground(Color.magenta);
nextBlocks[1][0].setVisible(true);
nextBlocks[2][0].setBackground(Color.magenta);
nextBlocks[2][0].setVisible(true);
break;
case 5:
nextBlocks[1][1].setBackground(Color.darkGray);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.darkGray);
nextBlocks[1][2].setVisible(true);
nextBlocks[1][0].setBackground(Color.darkGray);
nextBlocks[1][0].setVisible(true);
nextBlocks[2][1].setBackground(Color.darkGray);
nextBlocks[2][1].setVisible(true);
break;
case 6:
nextBlocks[1][1].setBackground(Color.red);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.red);
nextBlocks[1][2].setVisible(true);
nextBlocks[1][3].setBackground(Color.red);
nextBlocks[1][3].setVisible(true);
nextBlocks[1][0].setBackground(Color.red);
nextBlocks[1][0].setVisible(true);
break;
}
}
public void start() {
//初始化系统参数
score = 0;
level = initLevel;
speed = initSpeed;
totalLines = 0;
currentLines = 0;
isPause = false;
isEnd = false; //
jMenuItem2.setText("暂停");
//初始化游戏区
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 10; j++) {
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
flagBlocks[i][j]=0;
}
}
//随机获取方块类型
int blockType = (int) (Math.random() * 1000%7);
//设置方块类型
switch(blockType){
case 0:
block = new BlockOne();
break;
case 1:
block = new BlockTwo();
break;
case 2:
block = new BlockThree();
break;
case 3:
block = new BlockFour();
break;
case 4:
block = new BlockFive();
break;
case 5:
block = new BlockSix();
break;
case 6:
block = new BlockSeven();
break;
default:block = new BlockOne();
}
//随机获取下一个方块类型
nextBlockType=(int) (Math.random() * 1000%7);
//显示游戏状态显示区
showNextBlock(nextBlockType);
jLabel2.setText(Long.toString(score));
jLabel4.setText(Integer.toString(level));
jLabel6.setText(Integer.toString(totalLines));
try {
//启动游戏线程
thread.start();
}
catch (IllegalThreadStateException illegalThreadStateException) {}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
abstract class Block{
final int NORMAL=0,LEFT=1,RIGHT=2,TURN=3,DOWN=4;//方块动作常量
int direction =0;//当前方块动作
int rows[]=new int [4];//小方块行号
int cols[] = new int [4];//小方块列号
Color color = Color.lightGray;//方块的颜色
boolean isToBottom = false;//方块到底标志
boolean isToTop = false;//方块到顶部标志
int status=1;//方块翻转当前状态
//方块翻转方法
abstract void turn();
//获取方块动作方法
public int getDirection(){
return direction;
}
//设置方块动作方法
public void setDirection(int d){
direction = d;
}
//获取方块到底标志方法
public boolean getIsBottom(){
return isToBottom;
}
//获取方块是否到顶部方法
public boolean getIsTop(){
return isToTop;
}
//清除方块方法
public void clear(){
for(int i=0;i<4;i++)
{
playBlocks[rows[i]][cols[i]].setBackground(Color.lightGray) ;
playBlocks[rows[i]][cols[i]].setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -