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

📄 minebutton.java

📁 用java实现的全真模拟windows下的扫雷程序。不过目前实现以button方式实现。改进可以考虑以位图形式实现
💻 JAVA
字号:
/*
 * MineButton.java 1.0 2003-6-15
 *
 * Copyleft (c) 2003 RatKing.
 */

package jmine;

import java.awt.Dimension;
import javax.swing.*;
import javax.swing.plaf.ButtonUI;
import javax.swing.border.Border;

/**
 * 地雷按钮
 *
 * @author <a href="ratking@ynet.com">RatKing</a>
 * @version 1.0
 */
public class MineButton extends AbstractButton {
    private JMine jmine;

    public static final int ICON_M0  = 0;
    public static final int ICON_M1  = 1;
    public static final int ICON_M2  = 2;
    public static final int ICON_M3  = 3;
    public static final int ICON_M4  = 4;
    public static final int ICON_M5  = 5;
    public static final int ICON_M6  = 6;
    public static final int ICON_M7  = 7;
    public static final int ICON_M8  = 8;
    public static final int ICON_NULL   = 9;
    public static final int ICON_FLAG   = 10;
    public static final int ICON_MARK   = 11;
    public static final int ICON_MINE_BLACK     = 12;
    public static final int ICON_MINE_ERROR     = 13;
    public static final int ICON_MINE_EXPLODED  = 14;

    private ImageIcon[] icon = new ImageIcon[15];

    private int row;     // 所在行
    private int column;  // 所在列
    private boolean isMine;
    private int iconStyle;

    public MineButton(JMine jmine, int row, int column) {
        this(jmine, row, column, false, ICON_NULL);
    }

    /**
     * @param row       横向坐标
     * @param column    纵向坐标
     * @param isMine    是否是地雷
     * @param iconStyle 按钮上的图标
     */
    public MineButton(JMine jmine, int row, int column, boolean isMine, int iconStyle) {
        this.jmine = jmine;

        // Create the model
        setModel(new DefaultButtonModel());
        // initialize
        init(null, null);

        icon[ICON_M0]   = null;
        icon[ICON_M1]   = jmine.createImageIcon("m1.png", "1");
        icon[ICON_M2]   = jmine.createImageIcon("m2.png", "2");
        icon[ICON_M3]   = jmine.createImageIcon("m3.png", "3");
        icon[ICON_M4]   = jmine.createImageIcon("m4.png", "4");
        icon[ICON_M5]   = jmine.createImageIcon("m5.png", "5");
        icon[ICON_M6]   = jmine.createImageIcon("m6.png", "6");
        icon[ICON_M7]   = jmine.createImageIcon("m7.png", "7");
        icon[ICON_M8]   = jmine.createImageIcon("m8.png", "8");
        icon[ICON_NULL] = null;
        icon[ICON_FLAG] = jmine.createImageIcon("flag.png", "小红旗");
        icon[ICON_MARK] = jmine.createImageIcon("mark.png", "?标记");
        icon[ICON_MINE_BLACK]   = jmine.createImageIcon("mine_black.png", "黑色地雷");
        icon[ICON_MINE_EXPLODED]= jmine.createImageIcon("mine_exploded.png", "爆炸的地雷");
        icon[ICON_MINE_ERROR]   = jmine.createImageIcon("mine_error.png", "错误标记的地雷");

        this.row = row;
        this.column = column;
        this.isMine = isMine;
        setIconStyle(iconStyle);

        setPreferredSize(new Dimension(16, 16));
        setFocusPainted(false);
        setContentAreaFilled(false);
    }

    public int getRow() {
        return row;
    }

    public int getColumn() {
        return column;
    }

    public boolean isMine() {
        return isMine;
    }

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

    /**
     * 设置边框为凸出样式
     */
    public void setBorderUp() {
        setBorder(BorderFactory.createRaisedBevelBorder());
    }


    /**
     * 设置边框为凹下样式
     */
    public void setBorderDown() {
        setBorder(MineLineBorder.createBorder());
    }

    /**
     * @return 返回图标类型
     */
    public int getIconStyle() {
        return iconStyle;
    }

    /**
     * 设置图像类型
     */
    public void setIconStyle(int iconStyle) {
        this.iconStyle = iconStyle;
        Border border = (iconStyle == ICON_NULL || iconStyle == ICON_FLAG
                ||iconStyle == ICON_MARK)
                ? BorderFactory.createRaisedBevelBorder()
                : MineLineBorder.createBorder();

        setIcon(icon[iconStyle]);
        if (iconStyle == ICON_NULL || iconStyle == ICON_FLAG
                ||iconStyle == ICON_MARK) {
            setBorder(BorderFactory.createRaisedBevelBorder());
        }
        else {
            setBorder(MineLineBorder.createBorder());
        }
        //repaint();
    }

    /**
     * Resets the UI property to a value from the current look and
     * feel.
     *
     * @see JComponent#updateUI
     */
    public void updateUI() {
        setUI((ButtonUI)UIManager.getUI(this));
    }

    private static final String uiClassID = "ButtonUI";

    /**
     * Returns a string that specifies the name of the L&F class
     * that renders this component.
     *
     * @return the string "ButtonUI"
     * @see JComponent#getUIClassID
     * @see UIDefaults#getUI
     * @beaninfo
     *        expert: true
     *   description: A string that specifies the name of the L&F class.
     */
    public String getUIClassID() {
        return uiClassID;
    }
}

⌨️ 快捷键说明

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