www.pudn.com > scanmine2.0.rar > Ground.java
package cn.edu.hdc.ground;
import java.awt.Button;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import cn.edu.hdc.controller.Controller;
import cn.edu.hdc.mine.Mine;
import cn.edu.hdc.mine.MineFactory;
import cn.edu.hdc.util.Global;
@SuppressWarnings("serial")
public class Ground extends JPanel {
static int[][] ground = new int[Global.getMINE_W_NUM()][Global.getMINE_H_NUM()];
static JLabel[][] jL = new JLabel[Global.getMINE_W_NUM()][Global.getMINE_H_NUM()];
public static Mine[][] jB = new Mine[Global.getMINE_W_NUM()][Global.getMINE_H_NUM()];
private JPanel jp = new JPanel();
private static final JPanel jBp = new JPanel();
private static final JPanel jPp = new JPanel();
private static final JPanel jBpp = new JPanel();
private Button jBut = new Button();
private Controller controller;
public MenuBar menuBar = new MenuBar();
private static Menu menuGame = new Menu("游戏");
private Menu menuHelp = new Menu("帮助");
private MenuItem[] menuItems = {
new MenuItem("开局"),new MenuItem("初级"),
new MenuItem("中级"),new MenuItem("高级"),
new MenuItem("退出")
};
private void draw() {
for (int i = 0; i < Global.getMINE_W_NUM(); i++) {
for (int j = 0; j < Global.getMINE_H_NUM(); j++) {
jL[i][j] = new JLabel("");
jB[i][j] = new Mine();
jL[i][j].setBounds(i * Global.MINE_WIDTH + 3,j * Global.MINE_HIGTH + 3, Global.MINE_WIDTH,
Global.MINE_HIGTH);
jB[i][j].setBounds(i * Global.MINE_WIDTH + 3,j * Global.MINE_HIGTH + 3, Global.MINE_WIDTH,
Global.MINE_HIGTH);
}
}
}
private static void init() {
MineFactory.createMine();
for (int i = 0; i < Global.getMINE_W_NUM(); i++) {
for (int j = 0; j < Global.getMINE_H_NUM(); j++) {
if (i > 0) {
if (j > 0 && Global.MINE[i - 1][j - 1] == 1)
ground[i][j]++;
if (Global.MINE[i - 1][j] == 1) {
ground[i][j]++;
}
if (j < Global.getMINE_W_NUM() - 1 && Global.MINE[i - 1][j + 1] == 1)
ground[i][j]++;
}
if (j > 0 && Global.MINE[i][j - 1] == 1)
ground[i][j]++;
if (j < Global.getMINE_W_NUM() - 1 && Global.MINE[i][j + 1] == 1)
ground[i][j]++;
if (i < Global.getMINE_W_NUM() - 1) {
if (j > 0 && Global.MINE[i + 1][j - 1] == 1)
ground[i][j]++;
if (Global.MINE[i + 1][j] == 1) {
ground[i][j]++;
}
if (j < Global.getMINE_W_NUM() - 1 && Global.MINE[i + 1][j + 1] == 1)
ground[i][j]++;
}
}
}
}
private void initGround(boolean flag) {
for (int i = 0; i < Global.getMINE_W_NUM(); i++) {
for (int j = 0; j < Global.getMINE_H_NUM(); j++) {
if (Global.MINE[i][j] == 1) {
jL[i][j].setText(" M");
jB[i][j].setName("1"); // 设置为1表示当为1时有炸弹
} else if (ground[i][j] != 0) {
jL[i][j].setText(" " + ground[i][j]);
jB[i][j].setName("0"); // 设置为0表示在为0时有值
} else if (ground[i][j] == 0) {
jB[i][j].setName("" + i + "|" + j); // 当相应位位置没值时设置为相应的i,j值。
}
if(flag){
jB[i][j].addMouseListener(controller);
jBp.add(jL[i][j]);
jBp.add(jB[i][j]);
}
else{
jB[i][j].setVisible(true);
jB[i][j].setEnabled(true);
}
}
}
}
public Ground() {
draw();
controller = new Controller(this);
menuHelp.add("帮助");
menuHelp.addSeparator();
menuHelp.add("关于");
menuGame.setName("Game");
menuHelp.setName("Help");
for(int i=0;i