Cool Colorful Java Println()’s

The classic System.out.println(“Some String …”) is not going away. All the cool kids use it. But what if you could be cooler than all the cool kids?

If you print to a terminal window, now you too can be cooler than all the cool kids by mixing Java and ANSI escape sequences. Even diehard Java programmers often have never used ANSI color codes directly with their println’s

What if you could print from your Java program to a terminal window in multiple colors?

Such a thing can’t be done! You shout.

In fact is can.

Prepare to be amazed!

Embedding ANSI codes in your print’s and println’s allow for colored backgrounds, colored text, and in the hands of the rare genius, multi-colored ASCII art.

Here’s your first example. If you run it inside Eclipse, you might get a result that looks broken. For example, “Hi[31m there[0m!” However, when run from the command line of a terminal that supports ANSI escape codes, magic happens!

package com.genedavis;

public class AnsiCodeExample {
  
  public static final String ANSI_RESET = "\u001B[0m";
  public static final String ANSI_RED = "\u001B[31m";

  public static void main(String[] args) {

    System.out.println(
            "Hi" +
            ANSI_RED +
            " there" + 
            ANSI_RESET + 
            "!");
  }
}

This displays, “Hi there!”

Why, yes! You have entered the Twilight Zone!

If you haven’t read my article introducing ANSI codes and ANSI escape sequences, then this would be a good time to review it.

The short version is that the ANSI escape sequences are formatted as follows

\u001B[<code>m

The code can be one ANSI code or multiple codes separated by semicolons. More advanced codes have a letter different than ‘m’ ending them, but the color codes all end in ‘m’.

This is the full list of colors and other graphics codes.

Text attributes
0 All attributes off
1 Bold on
4 Underscore (on monochrome display adapter only)
5 Blink on
Foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

I want to call your attention to ‘0’. Zero resets the color attributes. It is customary to issue a ‘0’, before doing any other color changes to your text. This ensure that you get the style you want.

The ASCII code for ‘1’ makes colors brighter. Some people consider this to be bold, and some just consider it to be annoying.

Anyway, … Let’s look at another example to show you what these codes are capable of.

package com.genedavis;

public class AnsiCodeExample {
  
  public static final String ANSI_RESET = "\u001B[0m";
  
  //Okay, ... I cheated and used normal black for brown
  //and bright yellow for tan. Don't tell!
  public static final String RED_FILL = "\u001B[0;41m ";
  public static final String BLUE_FILL = "\u001B[0;44m ";
  public static final String CYAN_FILL = "\u001B[0.46m ";
  public static final String YELLOW_FILL = "\u001B[1;43m ";
  public static final String WHITE_FILL = "\u001B[0;47m ";
  public static final String BLACK_FILL = "\u001B[0;40m ";
  public static final String BROWN_FILL = "\u001B[0;40m ";
  public static final String GREEN_FILL = "\u001B[0;42m ";

  
  public static void main(String[] args) {
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );

    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + RED_FILL + RED_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        WHITE_FILL + WHITE_FILL + YELLOW_FILL + 
        YELLOW_FILL + YELLOW_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        YELLOW_FILL + YELLOW_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        BROWN_FILL + BROWN_FILL + BROWN_FILL + 
        YELLOW_FILL + YELLOW_FILL + BLACK_FILL + 
        YELLOW_FILL + WHITE_FILL + RED_FILL + 
        RED_FILL + RED_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + BROWN_FILL + 
        YELLOW_FILL + BROWN_FILL + YELLOW_FILL + 
        YELLOW_FILL + YELLOW_FILL + BLACK_FILL + 
        YELLOW_FILL + YELLOW_FILL + YELLOW_FILL + 
        RED_FILL + RED_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + BROWN_FILL + 
        YELLOW_FILL + BROWN_FILL + BROWN_FILL + 
        YELLOW_FILL + YELLOW_FILL + YELLOW_FILL + 
        BLACK_FILL + YELLOW_FILL + YELLOW_FILL + 
        YELLOW_FILL + RED_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + BROWN_FILL + 
        BROWN_FILL + YELLOW_FILL + YELLOW_FILL + 
        YELLOW_FILL + YELLOW_FILL + BLACK_FILL + 
        BLACK_FILL + BLACK_FILL + BLACK_FILL + 
        RED_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + YELLOW_FILL + YELLOW_FILL + 
        YELLOW_FILL + YELLOW_FILL + YELLOW_FILL + 
        YELLOW_FILL + YELLOW_FILL + RED_FILL + 
        RED_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + YELLOW_FILL + YELLOW_FILL + 
        YELLOW_FILL + RED_FILL + RED_FILL + 
        RED_FILL + RED_FILL + BLUE_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        BLUE_FILL + RED_FILL + RED_FILL + 
        WHITE_FILL + WHITE_FILL + BROWN_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + YELLOW_FILL + YELLOW_FILL + 
        YELLOW_FILL + RED_FILL + RED_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        BLUE_FILL + RED_FILL + RED_FILL + 
        RED_FILL + BLUE_FILL + RED_FILL + 
        WHITE_FILL + BROWN_FILL + BROWN_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + YELLOW_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        RED_FILL + RED_FILL + RED_FILL + 
        BLUE_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + CYAN_FILL + BLUE_FILL + 
        BLUE_FILL + BROWN_FILL + BROWN_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + CYAN_FILL + BLUE_FILL + 
        BLUE_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + BROWN_FILL + BROWN_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + BROWN_FILL + 
        BROWN_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + BROWN_FILL + BROWN_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + BROWN_FILL + BROWN_FILL + 
        BROWN_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + BLUE_FILL + BLUE_FILL + 
        BLUE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
    
    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + BROWN_FILL + BROWN_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );

    
    System.out.println(
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + WHITE_FILL + WHITE_FILL + 
        WHITE_FILL + ANSI_RESET
        );
  }
}

You may notice a striking resemblance to everyone’s favorite Mario brother!

Now you have the power to geek out your System.out.println()’s with color. Use your new found power to freak out your fellow coders. Enjoy!