日期:2014-05-20  浏览次数:20880 次

java 做的扫雷代码 贴出来大家提提建议
Java code

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);

        System.out.println("in init");
        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]); // 从第二个元素开始赋值!!
            }
            System.out.println("再设定 " + num + "个元素!");

        }
        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);
        // this.add(arg0, arg1)

        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设置背景图片!!)

            System.out.println("设置了" + count++ + "个 行为 " + hang + " 列为 " + lie);

        }

    }

    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);// 调用函数初始化炸弹!!

        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);
        // 创建面板
        if (sp == null) {
            System.out.println("南部面板没有创建成功!程序返回!");
            return;
        }
        JPanel southJpanel = sp.jp;
        sp.time.setText("" + SetBound.number * 6);
        sp.bound.setText("" + SetBound.number);
        this.add(southJpanel, BorderLayout.SOUTH);

        int extraheight;
        int extrawidth;
        extrawidth = (Integer) (width * 30 / 6);
        extraheight = (Integer) (height * 10 / 6);

        ((FlowLayout) southJpanel.getLayout()).setHgap(extrawidth);
        ((FlowLayout) southJpanel.getLayout()).setVgap(extraheight);

        this.setBounds(100, 100, 44 * sf.setwidth, 44 * sf.setheight + 30);
        this.setVisible(true);
        PlayAudio.playstart();

    }

    public int seekAllBound(int height, int width) {
        System.out.println("进入seekAllBound!");
        int num = 0;
        // 判断上一行三个元素是否为炸弹!!
        if (height - 1 >= 1) {
            if (width - 1 >= 1) {
                if (jb[height - 1][width - 1].haveimage)
                    num++;
            }
            if (jb[height - 1][width].haveimage) {
                num++;
            }
            if (width + 1 <= this.sf.setwidth) {
                if (jb[height - 1][width + 1].haveimage) {
                    num++;
                }
            }
        }

        // 判断左右两个元素是否为炸弹!
        if (width - 1 >= 1) {
            if (this.jb[height][width - 1].haveimage) {
                num++;
            }

        }

        if (width + 1 <= this.sf.setwidth) {
            if (this.jb[height][width + 1].haveimage) {
                num++;
            }
        }
        // 判断下一行三个元素是否是炸弹
        if (height + 1 <= this.sf.setheight) {
            if (width - 1 >= 1) {
                if (this.jb[height + 1][width - 1].haveimage) {
                    num++;
                }
            }
            if (this.jb[height + 1][width].haveimage) {
                num++;
            }
            if (width + 1 <= this.sf.setwidth) {
                System.out.println(this.jb[height + 1][width + 1].hang);
                if (this.jb[height + 1][width + 1].haveimage) {
                    num++;
                }
            }
        }
        return num;// 返回周围炸弹的数量!

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == newgame) {
            this.select(this.sf.setheight, this.sf.setwidth);
        }
        if (e.getSource() == level) {

            if (Gui.sf == null) {
                System.out.println("null!");
                sf = new SetFrame(this);
            } else {
                if (!Gui.sf.isVisible()) {
                    Gui.sf.setVisible(true);
                }
            }

        }
        if (e.getSource() == recordinfo) {

        }

        if (e.getSource() == appearance) { // 更改按钮的外观
            if (this.jb == null) {
                return;
            }
            if (!MyJButton.changeColor) {
                MyJButton.color = new Color(150, 150, 250);

            } else {
                MyJButton.color = new Color(100, 200, 100);
            }
            MyJButton.changeColor = !MyJButton.changeColor;

            this.setBackground(MyJButton.color);

            for (int i = 1; i <= this.sf.setheight; i++) {

                for (int j = 1; j <= this.sf.setwidth; j++) {

                    jb[i][j].setBackground(MyJButton.color);
                }
            }
        }
        if (e.getSource() == exit) {
            this.dispose();
        }
        if (e.getSource() == about) {

            JOptionPane.showMessageDialog(this,
                    "QQ : 928229202\n Date:2011/09/01", "关于扫雷",
                    JOptionPane.INFORMATION_MESSAGE, null);

        }
        if (e.getSource() == method) {
            JOptionPane
                    .showMessageDialog(
                            this,
                            "鼠标右键标示雷 标示后的雷再次鼠标点击是安全的\n被标示后的雷,再次鼠标右击则处于危险的\n危险中的雷左击如果是雷则游戏结束\n游戏以点开所有的非雷标志胜利\n注意有时间限制哦!");
        }

    }

    public static void main(String[] args) {
        Gui gui = new Gui();

    }

}