How to Print Colored Text in Java Console?


When dealing with Java terminal apps, individuals may frequently want to print colored text to improve the output's aesthetic appeal and readability. ANSI escape codes can be used to generate colored text instead of the monochrome output that Java's default terminal generally produces. When printed to the console, ANSI escape codes are unique sets of characters that alter the text's appearance by altering its color, style, or background. In this article, we'll look at how to print colored text in the Java console using ANSI escape codes. We'll go over two examples, one with colored text only and the other with colored text and a different background.

Utilizing the ANSI Escape Codes for Text Color.

Use the escape code sequence u001B[xxm, where xx stands for the ANSI color code, to change the text's color using ANSI escape codes. Here are some typical ANSI text color codes:

Blue: \u001B[34m

Cyan: \u001B[36m

Red: \u001B[31m

Green: \u001B[32m

White: \u001B[37m

Yellow: \u001B[33m

Black: \u001B[30m

Magenta: \u001B[35m

One must add the relevant ANSI color code before the text to apply a specific color, and one must append the reset code \u001B[0m after the text to complete the process. The reset code makes sure that the text displayed after that is displayed in the standard color.

Utilizing the ANSI Escape Codes for Background Color.

ANSI escape codes can change the text's backdrop color in addition to its text color. The escape code sequence u001B[xx;yym can be used to modify the background color, where xx stands for the ANSI color code for the background color. Here are some typical ANSI backdrop color codes:

Blue: \u001B[44m

Cyan: \u001B[46m

Black: \u001B[40m

Red: \u001B[41m

White: \u001B[47m

Green: \u001B[42m

Yellow: \u001B[43m

Magenta: \u001B[45m

To apply a specific background color to your text, you prepend the ANSI background color code before the text and append the reset code \u001B[0m after the text.

Let us look into two examples for the given query:

Example 1: Printing colored text in the Java console

Here, we'll concentrate on producing colored text with ANSI escape codes. Here is the algorithm and an example of code:

Algorithm

The steps to print colored text in the Java console are as follows:

Step-1: Create ANSI escape code constants for the text colors they want to use.

Step-2: To specify the desired color before the text you want to print, employ the ANSI escape code constants.

Step-3: Utilize the RESET code found after the colored text to return the formatting to its original condition.

Example

public class Main {
    public static void main(String[] args) {
        // ANSI escape code constants for text colors
        String RESET = "\u001B[0m";
        String RED = "\u001B[31m";
        String GREEN = "\u001B[32m";
        String YELLOW = "\u001B[33m";

        // Print colored text to the console
        System.out.println(GREEN + "This text is green." + RESET);
        System.out.println(YELLOW + "This text is yellow." + RESET);
       System.out.println(RED + "This text is red." + RESET);
    }
}

Output

Example 2: Printing colored text in java console with different background

Algorithm

The steps to print colored text with different backgrounds are as follows:

Step-1: Create ANSI escape code constants for the background and text colors you want to use.

Step-2: Before printing any text, employ the proper background and color codes.

Step-3: Utilize the RESET code found after the colored text to return the formatting to its original condition.

Example

public class Main {
    public static void main(String[] args) {
        // ANSI escape code constants for text colors and background colors
        String RESET = "\u001B[0m";
        String RED_TEXT = "\u001B[31m";
        String GREEN_TEXT = "\u001B[32m";
        String YELLOW_TEXT = "\u001B[33m";
        String BLACK_BG = "\u001B[40m";
        String WHITE_BG = "\u001B[47m";

        // Print colored text with the background to the console
        System.out.println(RED_TEXT + BLACK_BG + "This text is red with black background." + RESET);
        System.out.println(GREEN_TEXT + WHITE_BG + "This text is green with white background." + RESET);
        System.out.println(YELLOW_TEXT + BLACK_BG + "This text is yellow with black background." + RESET);
    }
}

Output

Conclusion

The Java terminal output can be formatted and colored utilizing ANSI escape codes, a potent tool. The user can alter the text's look to make it more aesthetically appealing and improve the readability of your terminal applications by using escape sequences and color codes. To produce excellent console output, play around with various color schemes and formatting choices. To ensure adequate support for ANSI escape codes, be certain to test your code in various console environments.

Updated on: 25-Aug-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements