⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bombpanel.java

📁 accp s1毕业项目 考试管理系统
💻 JAVA
字号:
package com.exam.ui.game;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BombPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private BombButton[][] buttons;
	private int[][] counts;
	private int bombNum = 0;
	private int row = 0;
	private int totalBomb = 0;
	private Bomb bomb;
	private Timer timer = null;
	private int time = 0;
	private int sum = 0;

	public BombPanel(int row, int bombNum, Bomb bomb) {
		this.row = row;
		this.bombNum = bombNum;
		this.totalBomb = bombNum;
		this.bomb = bomb;
	}

	public JPanel init() {
		timer = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				time++;
				if (time < 10) {
					bomb.txtTime.setText("00" + time);
				} else if (time < 100) {
					bomb.txtTime.setText("0" + time);
				} else {
					bomb.txtTime.setText("" + time);
				}
			}
		});

		counts = new int[row][row];
		GridLayout gl = new GridLayout(row, row, 1, 1);
		JPanel pnlMain = new JPanel(gl);
		buttons = new BombButton[row][row];
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				buttons[i][j] = new BombButton(i, j);
				buttons[i][j].setMargin(new Insets(0, 0, 0, 0));
				pnlMain.add(buttons[i][j]);
				final int x = i;
				final int y = j;
				buttons[i][j].addMouseListener(new MouseListener() {

					public void mouseClicked(MouseEvent e) {
						counts[x][y]++;
						clockStart();
						if (e.getButton() == MouseEvent.BUTTON1) {
							doLiftChick(x, y);
						} else if (e.getButton() == MouseEvent.BUTTON3) {
							doRightChick(x, y, counts[x][y]);
						}
					}

					public void mouseEntered(MouseEvent e) {
					}

					public void mouseExited(MouseEvent e) {
					}

					public void mousePressed(MouseEvent e) {
					}

					public void mouseReleased(MouseEvent e) {
					}

				});
			}
		}
		setBomb();
		roundBombCount();
		return pnlMain;
	}

	public void clockStart() {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				sum += counts[i][j];
			}
		}
		if (sum == 1) {
			timer.start();
		}
	}

	public void doLiftChick(int x, int y) {
		buttons[x][y].isLeft = true;
		if (buttons[x][y].isBomb == true) {
			// buttons[x][y].setIcon(isBombImg);// 设置为当前踩中的雷标记
			buttons[x][y].setText("●");
			timer.stop();
			JOptionPane.showMessageDialog(this, "您踩到雷了!", "提示", 2);
			showBomb();
		} else {
			buttons[x][y].setEnabled(false);
			if (buttons[x][y].roundBombCount == 0) {
				buttons[x][y].setText("");
				showNullButton(x, y);
			} else {
				buttons[x][y].setText("" + buttons[x][y].roundBombCount);
			}
		}
	}

	public void doRightChick(int x, int y, int count) {
		if (buttons[x][y].isEnabled() == true) {
			if (count % 2 == 0) {// 点击了右键双次
				bombNum++;
				buttons[x][y].isRight = false;
				// buttons[x][y].setIcon(null);// 还原雷标记
				buttons[x][y].setText("");
			} else {// 点击了右键单次
				bombNum--;
				buttons[x][y].isRight = true;
				// buttons[x][y].setIcon(isBombImg);// 设置雷标记
				buttons[x][y].setText("P");
			}
			if (bombNum < 10) {
				this.bomb.txtBombNum.setText("00" + bombNum);
			} else if (bombNum < 100) {
				this.bomb.txtBombNum.setText("0" + bombNum);
			} else {
				this.bomb.txtBombNum.setText("" + bombNum);
			}
			isWin();
		}
	}

	// 布雷
	public void setBomb() {
		for (int i = 0; i < totalBomb; i++) {
			int x = (int) (Math.random() * 1024) % row;
			int y = (int) (Math.random() * 1024) % row;
			if (buttons[x][y].isBomb == true) {
				i--;
			} else {
				buttons[x][y].isBomb = true;
			}
		}
	}

	// 计算周围的雷数
	public void roundBombCount() {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				int count = 0;
				if (buttons[i][j].isBomb != true) {
					if ((i - 1 >= 0) && (j - 1 >= 0)) {
						if (buttons[i - 1][j - 1].isBomb == true) {
							count++;
						}
					}
					if ((i - 1 >= 0)) {
						if (buttons[i - 1][j].isBomb == true) {
							count++;
						}
					}
					if ((i - 1 >= 0) && (j + 1 <= row - 1)) {
						if (buttons[i - 1][j + 1].isBomb == true) {
							count++;
						}
					}
					if ((j - 1 >= 0)) {
						if (buttons[i][j - 1].isBomb == true) {
							count++;
						}
					}
					if ((j + 1 <= row - 1)) {
						if (buttons[i][j + 1].isBomb == true) {
							count++;
						}
					}
					if ((i + 1 <= row - 1) && (j - 1 >= 0)) {
						if (buttons[i + 1][j - 1].isBomb == true) {
							count++;
						}
					}
					if ((i + 1 <= row - 1)) {
						if (buttons[i + 1][j].isBomb == true) {
							count++;
						}
					}
					if ((i + 1 <= row - 1) && (j + 1 <= row - 1)) {
						if (buttons[i + 1][j + 1].isBomb == true) {
							count++;
						}
					}
					buttons[i][j].roundBombCount = count;
				}
			}
		}
	}

	// 如果点击的按纽周围雷数为0,则自动显示周围8个格子,如果里面还有0继续显示
	public void showNullButton(int x, int y) {
		if ((x - 1 >= 0) && (y - 1 >= 0)) {
			if (buttons[x - 1][y - 1].isEnabled() == true
					&& buttons[x - 1][y - 1].isLeft == false
					&& buttons[x - 1][y - 1].isRight == false) {
				buttons[x - 1][y - 1].setEnabled(false);
				buttons[x - 1][y - 1].isLeft = true;
				if (buttons[x - 1][y - 1].roundBombCount == 0) {
					buttons[x - 1][y - 1].setText("");
					showNullButton(x - 1, y - 1);
				} else {
					buttons[x - 1][y - 1].setText(""
							+ buttons[x - 1][y - 1].roundBombCount);
				}
			}
		}
		if ((x - 1 >= 0)) {
			if (buttons[x - 1][y].isEnabled() == true
					&& buttons[x - 1][y].isLeft == false
					&& buttons[x - 1][y].isRight == false) {
				buttons[x - 1][y].isLeft = true;
				buttons[x - 1][y].setEnabled(false);
				if (buttons[x - 1][y].roundBombCount == 0) {
					buttons[x - 1][y].setText("");
					showNullButton(x - 1, y);
				} else {
					buttons[x - 1][y].setText(""
							+ buttons[x - 1][y].roundBombCount);

				}
			}
		}
		if ((x - 1 >= 0) && (y + 1 <= row - 1)) {
			if (buttons[x - 1][y + 1].isEnabled() == true
					&& buttons[x - 1][y + 1].isLeft == false
					&& buttons[x - 1][y + 1].isRight == false) {
				buttons[x - 1][y + 1].isLeft = true;
				buttons[x - 1][y + 1].setEnabled(false);
				if (buttons[x - 1][y + 1].roundBombCount == 0) {
					buttons[x - 1][y + 1].setText("");
					showNullButton(x - 1, y + 1);
				} else {
					buttons[x - 1][y + 1].setText(""
							+ buttons[x - 1][y + 1].roundBombCount);
				}
			}
		}
		if ((y - 1 >= 0)) {
			if (buttons[x][y - 1].isEnabled() == true
					&& buttons[x][y - 1].isLeft == false
					&& buttons[x][y - 1].isRight == false) {
				buttons[x][y - 1].isLeft = true;
				buttons[x][y - 1].setEnabled(false);
				if (buttons[x][y - 1].roundBombCount == 0) {
					buttons[x][y - 1].setText("");
					showNullButton(x, y - 1);
				} else {
					buttons[x][y - 1].setText(""
							+ buttons[x][y - 1].roundBombCount);
				}
			}
		}
		if ((y + 1 <= row - 1)) {
			if (buttons[x][y + 1].isEnabled() == true
					&& buttons[x][y + 1].isLeft == false
					&& buttons[x][y + 1].isRight == false) {
				buttons[x][y + 1].isLeft = true;
				buttons[x][y + 1].setEnabled(false);
				if (buttons[x][y + 1].roundBombCount == 0) {
					buttons[x][y + 1].setText("");
					showNullButton(x, y + 1);
				} else {
					buttons[x][y + 1].setText(""
							+ buttons[x][y + 1].roundBombCount);
				}
			}
		}
		if ((x + 1 <= row - 1) && (y - 1 >= 0)) {
			if (buttons[x + 1][y - 1].isEnabled() == true
					&& buttons[x + 1][y - 1].isLeft == false
					&& buttons[x + 1][y - 1].isRight == false) {
				buttons[x + 1][y - 1].isLeft = true;
				buttons[x + 1][y - 1].setEnabled(false);
				if (buttons[x + 1][y - 1].roundBombCount == 0) {
					buttons[x + 1][y - 1].setText("");
					showNullButton(x + 1, y - 1);
				} else {
					buttons[x + 1][y - 1].setText(""
							+ buttons[x + 1][y - 1].roundBombCount);
				}
			}
		}
		if ((x + 1 <= row - 1)) {
			if (buttons[x + 1][y].isEnabled() == true
					&& buttons[x + 1][y].isLeft == false
					&& buttons[x + 1][y].isRight == false) {
				buttons[x + 1][y].isLeft = true;
				buttons[x + 1][y].setEnabled(false);
				if (buttons[x + 1][y].roundBombCount == 0) {
					buttons[x + 1][y].setText("");
					showNullButton(x + 1, y);
				} else {
					buttons[x + 1][y].setText(""
							+ buttons[x + 1][y].roundBombCount);
				}
			}
		}
		if ((x + 1 <= row - 1) && (y + 1 <= row - 1)) {
			if (buttons[x + 1][y + 1].isEnabled() == true
					&& buttons[x + 1][y + 1].isLeft == false
					&& buttons[x + 1][y + 1].isRight == false) {
				buttons[x + 1][y + 1].isLeft = true;
				buttons[x + 1][y + 1].setEnabled(false);
				if (buttons[x + 1][y + 1].roundBombCount == 0) {
					buttons[x + 1][y + 1].setText("");
					showNullButton(x + 1, y + 1);
				} else {
					buttons[x + 1][y + 1].setText(""
							+ buttons[x + 1][y + 1].roundBombCount);
				}
			}
		}
	}

	// 判断是否过关
	public void isWin() {
		int count = 0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				if (buttons[i][j].isBomb == true
						&& buttons[i][j].isRight == true) {
					count++;
				}
			}
		}
		if (count == totalBomb && bombNum ==0) {
			timer.stop();
			JOptionPane.showMessageDialog(this, "恭喜你过关了!\n用时" + time + "秒",
					"提示", 2);
			reStart();
		}
	}

	// 显示雷的位置
	public void showBomb() {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				if (buttons[i][j].isBomb == true) {
					if (buttons[i][j].isLeft == false
							&& buttons[i][j].isRight == false) {
						// buttons[i][j].setIcon(bombImg);// 设置雷
						buttons[i][j].setText("●");
					}
				} else {
					// 如果不是雷,但被右键点击了,就应该显示错误,为什么没显示出来?
					if (buttons[i][j].isRight == true) {
						buttons[i][j].setText("X");
						buttons[i][j].setForeground(Color.red);
					}
				}
			}
		}
	}

	public void reStart() {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				buttons[i][j].isBomb = false;
				buttons[i][j].setText("");
				buttons[i][j].setIcon(null);
				buttons[i][j].isRight = false;
				buttons[i][j].isLeft = false;
				buttons[i][j].roundBombCount = 0;
				buttons[i][j].setEnabled(true);
				buttons[i][j].setForeground(Color.black);
				counts[i][j] = 0;
			}
		}
		this.setBombNum(totalBomb);
		this.bomb.txtBombNum.setText("0" + totalBomb);
		this.bomb.txtTime.setText("000");
		timer.stop();
		sum = 0;
		time = 0;
		setBomb();
		roundBombCount();
	}

	public int getBombNum() {
		return bombNum;
	}

	public int getRow() {
		return row;
	}

	public void setBombNum(int bombNum) {
		this.bombNum = bombNum;
	}

	public void setRow(int row) {
		this.row = row;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -