📄 main.java
字号:
package com.bokee.nicend.boxgame.main;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.bokee.nicend.boxgame.game.BoxEvent;
import com.bokee.nicend.boxgame.game.BoxEventListener;
import com.bokee.nicend.boxgame.gui.GamePanel;
public class Main implements BoxEventListener {
private javax.swing.JLabel label = new javax.swing.JLabel("成绩: 0");
private int lines = 0;
// 游戏主面板
GamePanel panel = new GamePanel();
//使用机器人
private boolean useRoboot = false;
/**
* @param args
*/
public static void main(String[] args) {
try {
final Main main = new Main();
main.robot = new MyRobot(main.panel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (main.robot != null)
main.robot.stop();
System.exit(0);
}
});
main.panel.setBoxListener(main);
main.panel.setBoxCol(15);
main.panel.setBoxRow(20);
//main.panel.setBoxWidth(10);
//main.panel.setBoxHeight(10);
main.panel.setDelay(150);
main.panel.init();
// main.panel.setDelay(100);
// 预览面板
GamePanel view = new GamePanel();
// 预览面板设置
view.setBoxCol(4);
view.setBoxRow(4);
view.setBoxWidth(15);
view.setBoxHeight(15);
view.setBoxStartPoint(0, 0);
view.setView(true);
view.init();
// 为游戏面板设置预览面板
main.panel.setViewer(view);
// 事件设置
main.panel.setFocusable(true);
main.panel.requestFocus();
main.panel.addKeyListener(main.panel);
frame.setLayout(new java.awt.BorderLayout());
frame.add(main.panel, java.awt.BorderLayout.CENTER);
frame.add(view, java.awt.BorderLayout.EAST);
main.label.setHorizontalAlignment(javax.swing.JLabel.CENTER);
frame.add(main.label, java.awt.BorderLayout.NORTH);
frame.add(main.getButtonsPanel(), java.awt.BorderLayout.SOUTH);
frame.setSize(main.panel.getWidth() + view.getWidth(), main.panel.getHeight());
frame.pack();
frame.setVisible(true);
main.panel.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private JPanel getButtonsPanel() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
final JButton b = new JButton("暂停");
b.setActionCommand("STOP");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equalsIgnoreCase("STOP")) {
Main.this.panel.stop();
b.setText("恢复");
b.setActionCommand("START");
Main.this.panel.requestFocus();
} else {
Main.this.panel.start();
b.setText("暂停");
b.setActionCommand("STOP");
Main.this.panel.requestFocus();
}
}
});
JButton speed = new JButton("加速");
speed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (Main.this.panel.getDelay() - 10 > 0) {
Main.this.panel.setDelay(Main.this.panel.getDelay() - 10);
Main.this.panel.requestFocus();
}
}
});
JButton s = new JButton("减速");
s.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Main.this.panel.setDelay(Main.this.panel.getDelay() + 10);
Main.this.panel.requestFocus();
}
});
final JButton roboot = new JButton("开始使用机器人");
roboot.setActionCommand("START");
roboot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("START")) {
Main.this.useRoboot = true;
roboot.setActionCommand("STOP");
roboot.setText("停止使用机器人");
Main.this.panel.requestFocus();
} else {
Main.this.useRoboot = false;
roboot.setActionCommand("START");
roboot.setText("使用使用机器人");
Main.this.panel.requestFocus();
}
}
});
panel.add(b);
panel.add(speed);
panel.add(s);
panel.add(roboot);
return panel;
}
public void doDispone(BoxEvent event) {
switch (event.getLines()) {
case 1:
lines++;
break;
case 2:
lines += 3;
break;
case 3:
lines += 6;
break;
case 4:
lines += 10;
break;
}
label.setText("成绩: " + lines);
}
public void doGameOver(BoxEvent event) {
panel.stop();
javax.swing.JOptionPane.showMessageDialog(panel, "游戏结束\n分数:" + lines, "游戏结束", javax.swing.JOptionPane.INFORMATION_MESSAGE);
panel.start();
}
//
public void doNewBox(BoxEvent event) {
// 机器人使用函数要重新封装
if (robot != null && useRoboot)
robot.start(event.getBox());
}
// 机器人
Robot robot = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -