Java實(shí)現(xiàn)空指針后的猜拳游戲
前言
“java.lang.NullPointerException” 空指針異??梢哉f是Java程序最容易出現(xiàn)的異常了,我寫了一個 IDEA 插件,每當(dāng)程序出現(xiàn)空指針異常時就會彈出一個“猜拳游戲”窗口,該窗口不能直接關(guān)閉,只有當(dāng)你游戲獲勝時,窗口才會自動關(guān)閉。
作用是啥?
嘲諷罷了。
插件實(shí)現(xiàn)
創(chuàng)建項(xiàng)目
IDEA 創(chuàng)建一個插件開發(fā)項(xiàng)目非常方便,已經(jīng)內(nèi)置了。

猜拳游戲?qū)崿F(xiàn)
很簡單。
實(shí)現(xiàn)原理:提供3個按鈕,分別為“石頭、剪刀、布”,對應(yīng)值“1、2、3”,再為按鈕綁定點(diǎn)擊事件,按鍵點(diǎn)擊之后調(diào)用處理函數(shù)傳入對應(yīng)的值即可。
處理函數(shù) handle(int selectedValue) 的實(shí)現(xiàn):利用隨機(jī)數(shù)隨機(jī)為電腦生成一個值與用戶選擇的值做比較,“石頭贏剪刀、剪刀贏布、布贏石頭”,然后顯示游戲結(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;
/**
* 提示標(biāo)簽
*/
private JLabel tipsLabel;
/**
* 結(jié)束標(biāo)記
*/
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);
}
}游戲彈窗實(shí)現(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)聽空指針異常
實(shí)現(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 添加我們自定義的控制臺過濾器實(shí)現(xiàn)。
<extensions defaultExtensionNs="com.intellij">
<consoleFilterProvider implementation="cn.xeblog.mora.filter.ConsoleFilter"/>
</extensions>
安裝插件
插件打包
Gradle -> Tasks -> build -> assemble

打包之后的文件位于項(xiàng)目的 build 目錄下:build/distributions/xxx.zip
插件安裝
進(jìn)入插件中心,選擇本地文件安裝即可。


演示
會出現(xiàn)空指針的代碼
public static void main(String[] args) {
Object obj = null;
System.out.println(obj.toString());
}運(yùn)行之后


當(dāng)我猜拳贏了之后,窗口就自動關(guān)閉了。
最后
完整代碼:https://github.com/anlingyi/Mora
到此這篇關(guān)于Java實(shí)現(xiàn)空指針后的猜拳游戲的文章就介紹到這了,更多相關(guān)Java猜拳游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)WebSocket四個步驟
這篇文章主要為大家介紹了Java實(shí)現(xiàn)WebSocket的方法實(shí)例,只需要簡單四個步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Java Kafka實(shí)現(xiàn)延遲隊(duì)列的示例代碼
kafka作為一個使用廣泛的消息隊(duì)列,很多人都不會陌生。本文將利用Kafka實(shí)現(xiàn)延遲隊(duì)列,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-08-08
ibatis學(xué)習(xí)之搭建Java項(xiàng)目
本文的主要內(nèi)容是簡單介紹了ibatis和如何通過iBatis搭建JAVA項(xiàng)目,包含了一個相關(guān)實(shí)例,需要的朋友可以參考下。2017-09-09
maven-surefire-plugin總結(jié)示例詳解
這篇文章主要介紹了maven-surefire-plugin總結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Spring Boot定制type Formatters實(shí)例詳解
在本篇文章里小編給大家整理的是關(guān)于Spring Boot定制type Formatters實(shí)例知識點(diǎn),需要的朋友們學(xué)習(xí)下。2019-11-11
Java處理InterruptedException異常的理論與實(shí)踐
在使用Java的過程中,有個情景或許很多人見過,您在編寫一個測試程序,程序需要暫停一段時間,于是調(diào)用 Thread.sleep()。但是編譯器或 IDE 報(bào)錯說沒有處理檢查到的 InterruptedException。InterruptedException 是什么呢,為什么必須處理它?下面跟著小編一起來看看。2016-08-08
java?-jar啟動參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問題
這篇文章主要介紹了java?-jar啟動參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

