package com.game.mine;
import java.util.Map;
import java.util.HashMap;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
/**
* 功能:游戏面板<br>
* 作者:我是小木鱼(Lag)<br>
*/
public class GamePanel extends JPanel implements MouseListener,ActionListener
{
private static final long serialVersionUID = -3395178511152885550L;
/** 游戏逻辑 */
private GameLogic gameLogic;
/** 初级难度(9×9,10个地雷) */
final int EASY = 1;
/** 中级难度(16×16,40个地雷) */
final int NORMAL = 2;
/** 高级难度(30×16,99个地雷) */
final int HARD = 3;
/** 上次游戏难度 */
private int oldLevel = -1;
/** 本次游戏难度 */
private int newLevel = 0;
/** 网格行数 */
int gridRows;
/** 网格列数 */
int gridColumns;
/** 网格尺寸 */
final int gridSize = 17;
/** 网格宽度 */
private int gridsWidth;
/** 网格高度 */
private int gridsHeight;
/** 地雷数 */
int mineNum;
/** 提示区面板 */
private JPanel panelTip = new JPanel();
/** 地雷区面板 */
private JPanel panelMine = new JPanel();
/** 笑脸按钮 */
private JButton buttonFace = new JButton();
/** 地雷数提示标签 */
JLabel labelMineTip[] = new JLabel[3];
/** 时间提示标签 */
JLabel labelTimeTip[] = new JLabel[3];
/** 地雷按钮数组 */
JButton[][] buttonMine = new JButton[16][30];
/**
* 地雷信息数组<br>
* number->地雷数:-1-地雷,0到8-周围地雷数<br>
* flag->地雷状态:0-未打开,1-已打开,2-插小旗,3-插问号<br>
*/
@SuppressWarnings("unchecked") //数组不支持泛型
Map<String,Integer>[][] mapMine = new Map[16][30];
/** 笑脸图片 */
private ImageIcon[] imageIconFace = new ImageIcon[2];
/** 数字提示图片 */
ImageIcon[] imageIconNumberTip = new ImageIcon[10];
/** 数字图片 */
ImageIcon[] imageIconNumber = new ImageIcon[9];
/** 空白图片 */
ImageIcon imageIconBlank = new ImageIcon();
/** 方格图片 */
ImageIcon imageIconCell = new ImageIcon();
/** 红旗图片 */
ImageIcon imageIconFlag = new ImageIcon();
/** 问号图片 */
ImageIcon imageIconQuestion = new ImageIcon();
/** 地雷图片 */
ImageIcon imageIconMine = new ImageIcon();
/** 爆炸图片 */
ImageIcon imageIconBomb = new ImageIcon();
/** 找雷错误图片 */
ImageIcon imageIconWrongMine = new ImageIcon();
/** 时间提示数字 */
int timeTip = 0;
/** 扫雷提示数字 */
int mineTip = 0;
/** 计时器 */
Timer timer = new Timer(1000,this);
/** 游戏是否开始(true-开始,false-未开始) */
boolean isStart = false;
/** 游戏是否结束(true-结束,false-未结束) */
boolean isGameOver = true;
/**
* 功能:构造函数<br>
*/
public GamePanel()
{
//主面板初始化
this.setBackground(Color.LIGHT_GRAY);
this.setBorder(BorderFactory.createRaisedBevelBorder()); //边框凸起
this.setLayout(null); //自由布局
//提示区面板初始化
this.panelTip.setBackground(Color.LIGHT_GRAY);
this.panelTip.setBorder(BorderFactory.createLoweredBevelBorder()); //边框下凹
this.panelTip.setLayout(null);
this.add(this.panelTip);
//地雷区面板初始化
this.panelMine.setBackground(Color.LIGHT_GRAY);
this.panelMine.setBorder(BorderFactory.createLoweredBevelBorder()); //边框下凹
this.add(this.panelMine);
//地雷数提示标签初始化
for(int i=0;i<this.labelMineTip.length;i++)
{
this.labelMineTip[i] = new JLabel();
}
//笑脸按钮初始化
this.buttonFace.addActionListener(this); //事件监听只能添加一次,若多次添加会造成监听事件重复执行。
//时间提示标签初始化
for(int i=0;i<this.labelTimeTip.length;i++)
{
this.labelTimeTip[i] = new JLabel();
}
//地雷按钮初始化
for(int row=0;row<this.buttonMine.length;row++)
{
for(int column=0;column<this.buttonMine[0].length;column++)
{
this.buttonMine[row][column] = new JButton();
this.buttonMine[row][column].addMouseListener(this);
this.buttonMine[row][column].setName(row+"_"+column); //用名字来区分行列坐标
}
}
//地雷信息初始化
for(int row=0;row<this.mapMine.length;row++)
{
for(int column=0;column<this.mapMine[0].length;column++)
{
this.mapMine[row][column] = new HashMap<String,Integer>();
this.mapMine[row][column].put("number",0); //0个雷
this.mapMine[row][column].put("flag",0); //未打开
}
}
//加载图片
this.loadImage();
//游戏逻辑
this.gameLogic = new GameLogic(this);
//设置游戏难度并调整各组件大小
this.setLevel(this.EASY);
}
/**
* 功能:加载图片<br>
* 备注:考虑Jar包问题,所以图片路径用URL格式<br>
*/
private void loadImage()
{
try
{
//笑脸图片
this.imageIconFace[0] = new ImageIcon(this.getClass().getResource("/resource/mine/smile.gif"));
this.imageIconFace[1] = new ImageIcon(this.getClass().getResource("/resource/mine/ooo.gif"));
//数字提示图片
for(int i=0;i<this.imageIconNumberTip.length;i++)
{
this.imageIconNumberTip[i] = new ImageIcon(this.getClass().getResource("/resource/mine/c"+i+".gif"));
}
//数字图片
for(int i=0;i<this.imageIconNumber.length;i++)
{
this.imageIconNumber[i] = new ImageIcon(this.getClass().getResource("/resource/mine/"+i+".gif"));
}
//空白图片
this.imageIconBlank = new ImageIcon(this.getClass().getResource("/resource/mine/blank.gif"));
//方格图片
this.imageIconCell = new ImageIcon(this.getClass().getResource("/resource/mine/cell.gif"));
//红旗图片
this.imageIconFlag = new ImageIcon(this.getClass().getResource("/resource/mine/flag.gif"));
//问号图片
this.imageIconQuestion = new ImageIcon(this.getClass().getResource("/resource/mine/question.gif"));
//地雷图片
this.imageIconMine = new ImageIcon(this.getClass().getResource("/resource/mine/mine.gif"));
//爆炸图片
this.imageIconBomb = new ImageIcon(this.getClass().getResource("/resource/mine/bomb.gif"));
//找雷错误图片
this.imageIconWrongMine = new ImageIcon(this.getClass().getResource("/resource/mine/wrongmine.gif"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 功能:初始化游戏<br>
*/
private void initGame()
{
//重新设置参数
this.mineTip = this.mineNum;
this.gameLogic.setNumberTip(this.mineNum,0);
this.timeTip = 0;
this.gameLogic.setNumberTip(0,1);
this.isGameOver = false;
this.isStart = false;
this.timer.stop();
//重置所有的信息
this.gameLogic.resetAll();
//随机生雷并记录周围地雷数
this.gameLogic.randomMine();
}
/**
* 功能:设置游戏难度级别<br>
*/
public void setLevel(int _level)
{
//判断难度是否有变化
if(this.oldLevel == _level){return;}
//记录难度
this.oldLevel = _level;
this.newLevel = _level;
//开始调试
if(this.newLevel == this.EASY)
{
this.gridRows = 9;
this.gridColumns = 9;
this.mineNum = 10;
}
else if(this.newLevel == this.NORMAL)
{
this.gridRows = 16;
this.gridColumns = 16;
this.mineNum = 40;
}
else if(this.newLevel == this.HARD)