Skip to content
Related Articles

Related Articles

Improve Article

How to Print Colored Text in Java Console?

  • Last Updated : 12 Aug, 2021
Geek Week

Remember that in Java Programming the background color and text color of the output screen is black or white by default. If we want to Highlight some text on the output screen then we can use the ANSI color codes and highlight the particular text. One can refer to the ANSI escape code in order to explore more.

Syntax:

System.out.println(ANSI_COLORNAME + "This text is colored" + ANSI_RESET);

As perceived from the above syntax contains This Syntax contains 3 parts:

  • In ANSI_COLORNAME we have to write the name in which we have given the particular ANSI code. for e.g  public static final String ANSI_BLACK = “\u001B[30m”;

The above is pseudo-code is to print text in black color. So here we can use ANSI_BLACK in place of ANSI_COLORNAME to print the text in Black color.

  • The second part is to write the text which we want to print in that color.
  • The ANSI_RESET code turns off all ANSI attributes set so far, which should return the console to its defaults.

Below is the ANSI color code table :



Color NameColor codeBackground ColorBackground Color code
BLACK\u001B[30mBLACK_BACKGROUND\u001B[40m
RED\u001B[31mRED_BACKGROUND\u001B[41m
GREEN\u001B[32mGREEN_BACKGROUND\u001B[42m
YELLOW\u001B[33mYELLOW_BACKGROUND\u001B[43m
BLUE\u001B[34mBLUE_BACKGROUND\u001B[44m
PURPLE\u001B[35mPURPLE_BACKGROUND\u001B[45m
CYAN\u001B[36mCYAN_BACKGROUND\u001B[46m
WHITE\u001B[37mWHITE_BACKGROUND\u001B[47m

Illustration: Text coloring in Java:

Example 1:

Java




// Java Program to Print Colored Text in Console
  
// Importing input output classes
import java.io.*;
  
// Main class
public class GFG {
  
    // Declaring ANSI_RESET so that we can reset the color
    public static final String ANSI_RESET = "\u001B[0m";
  
    // Declaring the color
    // Custom declaration
    public static final String ANSI_YELLOW = "\u001B[33m";
  
    // Main driver method
    public static void main(String[] args)
    {
        // Printing the text on console prior adding
        // the desired color
        System.out.println(ANSI_YELLOW
                           + "This text is yellow"
                           + ANSI_RESET);
    }
}

Output:

Example 2:

Java




// Java Program to Print Colored Text in Console
  
// Importing required classes
import java.io.*;
  
// Main class
class GFG {
  
    // Declaring ANSI_RESET so that we can reset the color
    public static final String ANSI_RESET = "\u001B[0m";
    // Declaring the background color
    public static final String ANSI_RED_BACKGROUND
        = "\u001B[41m";
  
    // Main driver method
    public static void main(String[] args)
    {
        // Now add the particular background color
        System.out.println(ANSI_RED_BACKGROUND
                           + "The background color is red"
                           + ANSI_RESET);
    }
}

Output:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :