Detailed explanation of two ways for Java to output colored characters on the console

Thinking of a question, can you change the font color of the console, so that some desired data can be highlighted, and other data can still be displayed as they are?

For example: highlight non-zero data in the array, and keep the others unchanged.

img

In this way, can the data be highlighted and displayed well? (It's just a personal hobby and has no practical effect.)

        //1:创建一个11行11列的二位数组
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        System.err.println("原始的二维数组为:");
        for (int[] row: chessArr1) {
    
    
            for (int data: row) {
    
    
                if(data == 0){
    
    
                    System.out.printf("\033[30m" + "%d\t",data);
                }else{
    
    
                    System.out.printf("\033[31m" + "%d\t",data);
                }
            }
            System.out.println();
        }

Some control methods are used here, and then through judgment to control whether to highlight.

Below are some query methods.

        System.out.println("------ 字体颜色30~37 ------");
        System.out.println("\033[30m" + "就是酱紫的");
        System.out.println("\033[31m" + "就是酱紫的");
        System.out.println("\033[32m" + "就是酱紫的");
        System.out.println("\033[33m" + "就是酱紫的");
        System.out.println("\033[34m" + "就是酱紫的");
        System.out.println("\033[35m" + "就是酱紫的");
        System.out.println("\033[36m" + "就是酱紫的");
        System.out.println("\033[37m" + "就是酱紫的");
        System.out.println("------ 背景颜色40~47 -------");
        System.out.println("\033[40m" + "就是酱紫的");
        System.out.println("\033[41m" + "就是酱紫的");
        System.out.println("\033[42m" + "就是酱紫的");
        System.out.println("\033[43m" + "就是酱紫的");
        System.out.println("\033[44m" + "就是酱紫的"+"\033[m");
        System.out.println("\033[45m" + "就是酱紫的");
        System.out.println("\033[46m" + "就是酱紫的"+"\033[m");
        System.out.println("--- 1:加粗,;:隔开,90~97字体颜色变亮 ---");
        System.out.println("\033[1;90m" + "就是酱紫的");
        System.out.println("\033[1;94m" + "就是酱紫的");
        System.out.println("\033[1;95m" + "就是酱紫的");
        System.out.println("\033[1;97m" + "就是酱紫的");
        System.out.println("\033[1;93;45m" + "就是酱紫的"+"\033[m");

img

Detailed explanation of two ways for Java to output colored characters on the console

Realized by \033 special escape character

This method is mainly applicable to the console of the Linux system, and can also be implemented in IDEA in the Windows system, but it is garbled in the console of the Windows system.

public class ColourTest {
    
    
    /**
     * @param colour  颜色代号:背景颜色代号(41-46);前景色代号(31-36)
     * @param type    样式代号:0无;1加粗;3斜体;4下划线
     * @param content 要打印的内容
     */
    private static String getFormatLogString(String content, int colour, int type) {
    
    
        boolean hasType = type != 1 && type != 3 && type != 4;
        if (hasType) {
    
    
            return String.format("\033[%dm%s\033[0m", colour, content);
        } else {
    
    
            return String.format("\033[%d;%dm%s\033[0m", colour, type, content);
        }
    }

    public static void main(String[] args) {
    
    
        System.out.println("控制台颜色测试:");
        System.out.println(getFormatLogString("[ 红色 ]", 31, 0));
        System.out.println(getFormatLogString("[ 黄色 ]", 32, 0));
        System.out.println(getFormatLogString("[ 橙色 ]", 33, 0));
        System.out.println(getFormatLogString("[ 蓝色 ]", 34, 0));
        System.out.println(getFormatLogString("[ 紫色 ]", 35, 0));
        System.out.println(getFormatLogString("[ 绿色 ]", 36, 0));
    }
}

Test results in Linux

After compiling, it runs perfectly on the Linux system.

img

Test results in IDEA

As shown in the figure below, the color effect can also be achieved in IDEA on the Windows system, but please note that the digital parameters for generating colors are different from those in Linux. The actual color of the same code in Linux after running in IDEA is partly different from that in Linux, such as the green in the figure below.

img

Windows console test results

As shown in the figure below, the output is garbled and there is no color effect.

img

Implemented by org.fusesource.jansi

This method works perfectly in Windows console and Linux with consistent results, but there is no color effect in IDEA.

    <dependency>
        <groupId>org.fusesource.jansi</groupId>
        <artifactId>jansi</artifactId>
        <version>2.1.1</version>
    </dependency>

Windows console test results

img

Test results in Linux

img

Test results in IDEA

img

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/130072053