亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現(xiàn)空指針后的猜拳游戲

 更新時間:2022年09月15日 16:28:04   作者:AnLingYi  
“java.lang.NullPointerException”?空指針異??梢哉f是Java程序最容易出現(xiàn)的異常了,小編寫了一個?IDEA?插件,每當程序出現(xiàn)空指針異常時就會彈出一個“猜拳游戲”窗口,該窗口不能直接關(guān)閉,只有當你游戲獲勝時,窗口才會自動關(guān)閉

前言

“java.lang.NullPointerException” 空指針異??梢哉f是Java程序最容易出現(xiàn)的異常了,我寫了一個 IDEA 插件,每當程序出現(xiàn)空指針異常時就會彈出一個“猜拳游戲”窗口,該窗口不能直接關(guān)閉,只有當你游戲獲勝時,窗口才會自動關(guān)閉。

作用是啥?

嘲諷罷了。

插件實現(xiàn)

創(chuàng)建項目

IDEA 創(chuàng)建一個插件開發(fā)項目非常方便,已經(jīng)內(nèi)置了。

猜拳游戲?qū)崿F(xiàn)

很簡單。

實現(xiàn)原理:提供3個按鈕,分別為“石頭、剪刀、布”,對應(yīng)值“1、2、3”,再為按鈕綁定點擊事件,按鍵點擊之后調(diào)用處理函數(shù)傳入對應(yīng)的值即可。

處理函數(shù) handle(int selectedValue) 的實現(xiàn):利用隨機數(shù)隨機為電腦生成一個值與用戶選擇的值做比較,“石頭贏剪刀、剪刀贏布、布贏石頭”,然后顯示游戲結(jié)果,用戶獲勝時會觸發(fā)回調(diào)函數(shù)(用于關(guān)閉彈窗)。

package cn.xeblog.mora.ui;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

/**
 * @author anlingyi
 * @date 2022/8/11 8:02 PM
 */
public class MoraGame extends JPanel {

    /**
     * 猜拳獲勝調(diào)用
     */
    private Runnable runnable;

    /**
     * 提示標簽
     */
    private JLabel tipsLabel;

    /**
     * 結(jié)束標記
     */
    private boolean isOver;

    public MoraGame(Runnable runnable) {
        setMinimumSize(new Dimension(250, 100));
        setLayout(new BorderLayout());
        this.runnable = runnable;
        init();
    }

    private void init() {
        this.tipsLabel = new JLabel("請出拳!", JLabel.CENTER);
        this.tipsLabel.setPreferredSize(new Dimension(250, 50));
        this.tipsLabel.setFont(new Font("", 0, 15));
        this.tipsLabel.setForeground(new Color(255, 128, 128));

        JButton stoneButton = new JButton("石頭");
        JButton shearsButton = new JButton("剪刀");
        JButton clothButton = new JButton("布");

        stoneButton.setFocusPainted(false);
        stoneButton.setBorderPainted(false);
        stoneButton.addActionListener(l -> handle(1));

        shearsButton.setFocusPainted(false);
        shearsButton.setBorderPainted(false);
        shearsButton.addActionListener(l -> handle(2));

        clothButton.setFocusPainted(false);
        clothButton.setBorderPainted(false);
        clothButton.addActionListener(l -> handle(3));

        JPanel centerPanel = new JPanel();
        centerPanel.setPreferredSize(new Dimension(250, 30));
        centerPanel.add(stoneButton);
        centerPanel.add(shearsButton);
        centerPanel.add(clothButton);

        add(tipsLabel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
    }

    private void handle(int selectedValue) {
        if (isOver) {
            return;
        }

        int value = new Random().nextInt(3) + 1;
        boolean isWin = selectedValue == (value - 1 == 0 ? 3 : value - 1);

        String result;
        if (isWin) {
            isOver = true;
            result = "你贏~";
        } else if (selectedValue == value) {
            result = "平局~";
        } else {
            result = "電腦贏~";
        }
        showTips("電腦 -> " + getText(value) + ",你 -> " + getText(selectedValue) + "," + result);

        if (isWin) {
            new Thread(() -> {
                try {
                    Thread.sleep(800);
                    this.runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
    }

    private String getText(int value) {
        switch (value) {
            case 1:
                return "石頭";
            case 2:
                return "剪刀";
            case 3:
                return "布";
        }

        return "";
    }

    private void showTips(String tips) {
        tipsLabel.setText(tips);
    }

}

游戲彈窗實現(xiàn)

將窗口設(shè)置為不可關(guān)閉,傳遞彈窗關(guān)閉回調(diào)函數(shù)到游戲處理對象。

package cn.xeblog.mora.ui;

import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author anlingyi
 * @date 2022/8/11 7:58 PM
 */
public class MoraDialog extends DialogWrapper {

    public MoraDialog() {
        super(true);
        setTitle("猜拳游戲?");
        setResizable(false);
        setCrossClosesWindow(false);
        init();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return new MoraGame(() -> SwingUtilities.invokeLater(() -> this.close(0)));
    }

    @Override
    protected @NotNull Action[] createActions() {
        return new Action[]{};
    }

}

監(jiān)聽空指針異常

實現(xiàn)控制臺過濾接口,判斷控制臺的輸出內(nèi)容是否包含 java.lang.NullPointerException ,如果包含則彈出游戲窗口。

package cn.xeblog.mora.filter;

import cn.xeblog.mora.ui.MoraDialog;
import com.intellij.execution.filters.ConsoleFilterProvider;
import com.intellij.execution.filters.Filter;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
 * @author anlingyi
 * @date 2022/8/11 11:46 PM
 */
public class ConsoleFilter implements ConsoleFilterProvider {

    @Override
    public Filter @NotNull [] getDefaultFilters(@NotNull Project project) {
        return new Filter[]{(line, entireLength) -> {
            if (line.contains("java.lang.NullPointerException")) {
                SwingUtilities.invokeLater(() -> new MoraDialog().show());
            }

            return null;
        }};
    }

}

注冊過濾器

plugin.xml 添加我們自定義的控制臺過濾器實現(xiàn)。

    <extensions defaultExtensionNs="com.intellij">
        <consoleFilterProvider implementation="cn.xeblog.mora.filter.ConsoleFilter"/>
    </extensions>

安裝插件

插件打包

Gradle -> Tasks -> build -> assemble

打包之后的文件位于項目的 build 目錄下:build/distributions/xxx.zip

插件安裝

進入插件中心,選擇本地文件安裝即可。

演示

會出現(xiàn)空指針的代碼

    public static void main(String[] args) {
        Object obj = null;
        System.out.println(obj.toString());
    }

運行之后

當我猜拳贏了之后,窗口就自動關(guān)閉了。

最后

完整代碼:https://github.com/anlingyi/Mora

到此這篇關(guān)于Java實現(xiàn)空指針后的猜拳游戲的文章就介紹到這了,更多相關(guān)Java猜拳游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論