package cn.edu.tlu;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.MenuBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import cn.edu.tlu.common.Utils;
/**
* 扫雷主窗体
* @author G
*
*/
public class MyFrame extends JFrame implements ActionListener,Runnable,MouseListener{
private JLabel mainLab; // 主窗体
private JLabel bar; // 图标
private int mineNum; // 地雷计数器
private int startMineNum; // 初始地雷数
private JLabel timeLab,mineLab; // 时间 和 地雷
private JButton flush,min,close;
private JButton style1,style2,style3;
private long start;
private int row, col; // 行数列数
private Mine[][] mines; // 地雷按钮数组
private JPanel jp; // 放地雷的面板
private int left,up; // 面板在窗体上的位置
public static final int MINE_SIZE = 22;
// 存放不同风格的图片
private ImageIcon[] mainLabs = {new ImageIcon("./img/back.png"),new ImageIcon("./img/style2/back.png"),new ImageIcon("./img/style3/back.png")};
private ImageIcon[] style1s = {new ImageIcon("./img/style1.png"),new ImageIcon("./img/style2/style1.png"),new ImageIcon("./img/style3/style1.png")};
private ImageIcon[] style2s = {new ImageIcon("./img/style2.png"),new ImageIcon("./img/style2/style2.png"),new ImageIcon("./img/style3/style2.png")};
private ImageIcon[] style3s = {new ImageIcon("./img/style3.png"),new ImageIcon("./img/style2/style3.png"),new ImageIcon("./img/style3/style3.png")};
private ImageIcon[] btn_backs = {new ImageIcon("./img/but_back.png"),new ImageIcon("./img/style2/but_back.png"),new ImageIcon("./img/style3/but_back.png")};
private ImageIcon[] btn_opens = {new ImageIcon("./img/btn_open.png"),new ImageIcon("./img/style2/btn_open.png"),new ImageIcon("./img/style3/btn_open.png")};
private ImageIcon[] btn_flags = {new ImageIcon("./img/hongqi.png"),new ImageIcon("./img/style2/hongqi.png"),new ImageIcon("./img/style3/hongqi.png")};
private int index = 0;
/* 添加菜单项 **/
private JMenuBar menuBar;
private JMenu menu;
private JCheckBoxMenuItem easy,normal,hard;
public MyFrame(int startMineNum ,int row, int col ,int left ,int up) {
// 初始化
mainLab = new JLabel(mainLabs[index]);
style1 = new JButton(style1s[index]);
style2 = new JButton(style2s[index]);
style3 = new JButton(style3s[index]);
bar = new JLabel(new ImageIcon("./img/bar.png"));
flush = new JButton(new ImageIcon("./img/flush.png"));
min = new JButton(new ImageIcon("./img/min.png"));
close = new JButton(new ImageIcon("./img/close.png"));
this.startMineNum = startMineNum; // 初始雷数量
mineNum = startMineNum; // 雷计数器
timeLab = new JLabel("00:00");
mineLab = new JLabel(mineNum+"");
this.row = row; // 19
this.col = col; // 15
this.left = left; // 85
this.up = up; // 100
mines = new Mine[row][col];
// 设置面板对象
jp = getMinePanel();
// 设置菜单栏
menuBar = getMyMenuBar();
// 组装窗体
init();
// 注册监听器
addEventHandler();
// 窗体添加鼠标拖拽功能
Utils.setFrameDrag(this);
Utils.initFrame(this);
}
/** 生成菜单栏 **/
private JMenuBar getMyMenuBar() {
JMenuBar m = new JMenuBar();
m.setOpaque(false); // 不透明
m.setBorderPainted(false); // 不绘制边框
menu = new JMenu();
menu.setIcon(new ImageIcon("./img/bar.png"));
m.add(menu);
easy = new JCheckBoxMenuItem("初级",new ImageIcon("./img/1.gif"));
normal = new JCheckBoxMenuItem("中级",new ImageIcon("./img/2.gif"));
hard = new JCheckBoxMenuItem("高级",new ImageIcon("./img/3.gif"));
menu.add(easy);
menu.add(normal);
menu.add(hard);
return m;
}
/** 生成地雷面板 **/
private JPanel getMinePanel() {
JPanel jp = new JPanel();
// 设置网格布局管理器
jp.setLayout(new GridLayout(row, col));
// 创建地雷按钮,添加到数组,面板中
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
mines[i][j] = new Mine(i, j);
mines[i][j].setIcon(btn_backs[index]);
mines[i][j].addMouseListener(this);
jp.add(mines[i][j]);
}
}
// 随机设置地雷
layMines();
// 设置每个按钮的周围地雷数
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (mines[i][j].getValue()==0) {
mines[i][j].setValue(calMineNum(i,j));
}
}
}
// 测试
for (int j = 0; j < row; j++) {
for (int k = 0; k < col; k++) {
System.out.print(mines[j][k].getValue()>=0?" "+mines[j][k].getValue():" "+mines[j][k].getValue());
}
System.out.println();
}
return jp;
}
/** 放地雷 **/
private void layMines() {
for (int i = 0; i < mineNum; i++) {
int ii = (int)(Math.random() * row);
int jj = (int)(Math.random() * col);
if (mines[ii][jj].getValue() == -1) {
i--;
continue;
}
mines[ii][jj].setValue(-1);
}
}
/** 计算周围的地雷数量 **/
private int calMineNum(int i,int j) {
int num = 0;
for (int k = i-1; k <= i+1; k++) {
for (int k2 = j-1; k2 <= j+1; k2++) {
if ( k>=0 && k2>=0 && k
资源文件列表:
SmallGame-master.zip 大约有814个文件