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

📄 minelabel.java

📁 java版的扫雷,功能比较齐全
💻 JAVA
字号:
package clearminegame;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

public class MineLabel extends JLabel {

    public static final int SIZE = 20;//大小
    //特殊字符
    public static final String TEXT_BLANK = "";//空白
    public static final String TEXT_WARN = "!";//警告
    public static final String TEXT_UNKNOWN = "?";//未知
    public static final String TEXT_MINE = "*";   //雷
    public static final String TEXT_ERROR = "×";//错误
    //颜色
    public static final Color COLOR_BLANK = null;
    public static final Color COLOR_WARN = Color.RED;
    public static final Color COLOR_UNKNOWN = new Color(0, 128, 128);
    public static final Color COLOR_MINE = Color.BLACK;
    public static final Color COLOR_MINE_BG = Color.RED;//雷的背景色
    public static final Color COLOR_ERROR = Color.RED;
    //边框
    public static final Border BORDER_INIT = BorderFactory.createRaisedBevelBorder();//初始边框
    public static final Border BORDER_CLICKED = BorderFactory.createLineBorder(Color.LIGHT_GRAY);//点击之后的边框
    //
    private MinePanel pnlMine;//游戏面板
    private int rowIndex;//行位置
    private int columnIndex;//列位置
    private int value;//值
    private boolean clicked;//是否已点击
    private boolean mine;//是否为雷
    //getters/setters
    public boolean isWarn() {
        return getText().equals(TEXT_WARN);
    }

    public int getValue() {
        return value;
    }

    public boolean isMine() {
        return mine;
    }

    public boolean isClicked() {
        return clicked;
    }

    public void setMine(boolean mine) {
        this.mine = mine;
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    //默认构造方法
    public MineLabel() {
    }

    //主要构造方法
    public MineLabel(MinePanel pnlMine, int rowIndex, int columnIndex) {
        this.pnlMine = pnlMine;
        this.rowIndex = rowIndex;
        this.columnIndex = columnIndex;
        //水平对齐方式
        setHorizontalAlignment(CENTER);
        //不透明
        setOpaque(true);
        //将字体设为粗体
        setFont(new Font(null, Font.BOLD, SIZE - 8 ));
        //注册鼠标事件
        this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                onMousePressed(e);
            }
        });
        //
        init();
    }
    //
    public void addValue() {
        this.value ++;
    }
    //恢复初始状态
    public void init()
    {
        //边框
        setBorder(BORDER_INIT);
        //
        value = 0;
        setClicked(false);
        setMine(false);
        setText(TEXT_BLANK);
        setBackground(null);
    }

    void click() {
        //如果文本长度不为0(被标记,比如警告及疑问)则不作处理
        if (getText().length() > 0) {
            return;
        }
        //改变状态为点击过的状态
        clicked = true;
        setBorder(BORDER_CLICKED);
        //如果为雷则结束游戏,否则提示雷数
        if (isMine()) {
            setText(TEXT_MINE);
            setForeground(COLOR_MINE);
            setBackground(COLOR_MINE_BG);
            //
            pnlMine.fail();
        } else {
            //如果雷数为空则自动翻开周围的方块
            if (value == 0) {
                //处理周围的按钮,只要不为雷的且未click的都click
                clickAround(true);
            } else {//显示雷数
                setText(String.valueOf(value));
                setForeground(this.getColorByValue(value));
            }
            //
            pnlMine.addRigthCount();
        }
    }
    //按值获得对应的颜色
    private Color getColorByValue(int value) {
        value --;
        return new Color(value*35, 0, 255-value*35);
    }
    //统计周围已标记的雷数
    private int countAroundWarns()
    {
        int count = 0;
        MineLabel lblMine;
        for (int r = rowIndex - 1; r <= rowIndex + 1; r++) {
            //如果行超出范围则开始下一行
            if (r < 0 || r >= pnlMine.getRowCount()) {
                continue;
            }
            for (int c = columnIndex - 1; c <= columnIndex + 1; c++) {
                //如果列超出范围则跳过
                if (c < 0 || c >= pnlMine.getColumnCount() ) {
                    continue;
                }
                lblMine = pnlMine.getMineArray()[r][c];
                //如果已经标记为雷则计数
                if(lblMine.isWarn())
                {
                    count++;
                }
            }
        }
        return count;
    }
    //处理周围,参数skipMine表示对雷不做处理
    private void clickAround(boolean skipMine)
    {
        MineLabel lblMine;
        for (int r = rowIndex - 1; r <= rowIndex + 1; r++) {
            //如果行超出范围则开始下一行
            if (r < 0 || r >= pnlMine.getRowCount()) {
                continue;
            }
            for (int c = columnIndex - 1; c <= columnIndex + 1; c++) {
                //如果列超出范围则跳过
                if (c < 0 || c >= pnlMine.getColumnCount() ) {
                    continue;
                }
                lblMine = pnlMine.getMineArray()[r][c];
                //已经点击过则跳过
                if(lblMine.isClicked())
                {
                    continue;
                }
                //如果指明跳过雷则跳过雷
                if ( skipMine && lblMine.isMine() ) {
                    continue;
                }
                //点击
                lblMine.click();
            }
        }
    }
    //
    private void onMousePressed(MouseEvent e) {

        //若在游戏中,且该方块被点击过则处理双击的情况
        if ( pnlMine.isPlaying() && clicked ) {
            //判断是否同时按下左右键
            boolean both = MouseEvent.BUTTON1_DOWN_MASK + MouseEvent.BUTTON3_DOWN_MASK == e.getModifiersEx();
            //处理双击或同时按下左右键的情况
            if(e.getClickCount()>1 || both)
            {
                //如果周围被标记的雷数与value相同则自动翻开周围的方块
                if(value==this.countAroundWarns())
                {
                    clickAround(false);
                }
            }
            return;
        }
        //
        if (e.getButton() == MouseEvent.BUTTON1) {  //左键
            //如果是第一个方块则正式开始游戏
            if( pnlMine.isReady() )
            {
                pnlMine.start(rowIndex, columnIndex);
                click();
            }
            else if( pnlMine.isPlaying() )//如果是在游戏中则翻开方块
            {
                click();
            }
        } else if(e.getButton() == MouseEvent.BUTTON3) { //右键
            // 非运行状态不做处理
            if( !pnlMine.isPlaying() )
                return;
            //
            if (getText().equals(TEXT_BLANK)) {//如果原为空白则变成警告状态
                if (pnlMine.getWarnCount() < pnlMine.getMineCount()) {
                    setForeground(COLOR_WARN);
                    setText(TEXT_WARN);
                    pnlMine.setWarnCount(pnlMine.getWarnCount() + 1);
                }
            } else if (getText().equals(TEXT_WARN)) {//如果原为警告状态则变成疑问状态
                setForeground(COLOR_UNKNOWN);
                setText(TEXT_UNKNOWN);
                pnlMine.setWarnCount(pnlMine.getWarnCount() - 1);
            } else if (getText().equals(TEXT_UNKNOWN)) {//如果原为疑问状态则变成空白状态
                setForeground(COLOR_BLANK);
                setText(TEXT_BLANK);
            }
        }
    }
}

⌨️ 快捷键说明

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