//Gui类用JButton(MyJButton)数组存储全部按钮
//设计MyJButton类,继承JButton类,加上数字显示,背景图片,在鼠标响应时做出一系列的自变化
//界面初始化(刚刚进入游戏界面),实现对MyJButton类的背景数字或图片(地雷)的设定!//对鼠标左右键的监听方法!(左键 调用MyJButton的init方法 (设置背景透明显示数字) 右键 设置背景雷)递归算法 显示数字!!
//创建Gui对象时首先用SetBound类的静态方法负责随机生成炸弹数列(炸弹按钮上的数字为0 默认!!) 然后随机用数列二层循环生成MyJButton类数组;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class Gui extends JFrame implements ActionListener {
static SouthPanel sp;
static SetFrame sf;
JMenuItem newgame;
JMenuItem level;
JMenuItem appearance;
JMenuItem recordinfo;
JMenuItem exit;
JMenuItem about;
JMenuItem method;
MyJButton jb[][];
public static HashSet hs;// 用于保存生成的随机炸弹数列
public static boolean lasted = false; // 标志变量 标记是否重新开始新的游戏
JPanel main;
Gui() {
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
sp = new SouthPanel();
MyMouseListenerAdapter.noBoundnum = 0; // 每次雷区初始化都重新设定noBoundnum的数值为0
// (该变量用于判断扫雷是否成功)
JMenuBar jmenubar = new JMenuBar();
JMenu game = new JMenu("游戏");
JMenu help = new JMenu("帮助");
newgame = new JMenuItem("新游戏");
newgame.addActionListener(this);
game.add(new JSeparator());
level = new JMenuItem("级别");
level.addActionListener(this);
recordinfo = new JMenuItem("统计信息");
recordinfo.addActionListener(this);
appearance = new JMenuItem("更改外观");
appearance.addActionListener(this);
exit = new JMenuItem("退出");
exit.addActionListener(this);
about = new JMenuItem("关于扫雷");
about.addActionListener(this);
method = new JMenuItem("玩法");
method.addActionListener(this);
game.add(newgame);
game.add(level);
game.add(recordinfo);
game.add(appearance);
game.add(exit);
help.add(about);
help.add(method);
jmenubar.add(game);
jmenubar.add(help);
this.setJMenuBar(jmenubar);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb = new MyJButton[sf.setheight + 1][sf.setwidth + 1];
// 初始化小 默认为简单模式
// ....................初始化炸弹...................................
initBound(Gui.lasted);// 调用函数初始化炸弹!!
main = new JPanel();
main.setBackground(Color.CYAN);
int num = 0;
main.setLayout(new GridLayout(sf.setheight, sf.setwidth));
for (int i = 1; i <= sf.setheight; i++) {
for (int j = 1; j <= sf.setwidth; j++) {
if (jb[i][j] == null) {
jb[i][j] = new MyJButton(i, j);
num++;
}
jb[i][j].addMouseListener(new MyMouseListenerAdapter(this));
main.add(jb[i][j]); // 从第二个元素开始赋值!!
}
}
for (int i = 1; i <= sf.setheight; i++) {
for (int j = 1; j <= sf.setwidth; j++) {
if (!jb[i][j].haveimage) {
jb[i][j].num = seekAllBound(i, j);
}
}
}
this.add(main, BorderLayout.CENTER);
sp.time.setText("" + SetBound.number * 6 + "");
sp.bound.setText("" + SetBound.number + "");
// 创建面板
this.add(sp.jp, BorderLayout.SOUTH);
this.setBounds(100, 100, 44 * sf.setwidth, 44 * sf.setheight + 30);
this.setVisible(true);
PlayAudio.playstart();
}
public void initBound(boolean lasted) {
if (!lasted) {
hs = SetBound.initBound(this.sf.setwidth, this.sf.setheight);
}
Iterator it = hs.iterator();
int count = 1;
while (it.hasNext()) {
int location = (Integer) it.next();
int hang;
int lie;
if (location % this.sf.setwidth != 0) {
hang = location / this.sf.setwidth + 1;
lie = location % this.sf.setwidth;
} else {
hang = location / this.sf.setwidth;
lie = this.sf.setwidth;
}
this.jb[hang][lie] = new MyJButton(hang, lie);
this.jb[hang][lie].haveimage = true;
// 设置初始炸弹!!(给某些MJButton设置背景图片!!)
}
}
public void select(int height, int width) {
// 设置标志 MyMouseListenerAdapter的over为false!!
MyMouseListenerAdapter.over = false;
if (MyMouseListenerAdapter.timer != null) {
MyMouseListenerAdapter.timer.cancel();
MyMouseListenerAdapter.timer = null; // 取消正在进行的倒计时!
}
Random ran = new Random();
this.getContentPane().removeAll(); // 清空上次的面板!!
this.sf.setwidth = width;
this.sf.setheight = height;
jb = new MyJButton[height + 1][width + 1];
initBound(lasted);// 调用函数初始化炸弹!!
mai