How to print color in console using System.out.println?
How can I print color in console? I want to show data in colors when the processor sends data and in different colors when it receives data.
If your terminal supports it, you can use ANSI escape codes to use color in your output. It generally works for Unix shell prompts; however, it doesn't work for Windows Command Prompt (Although, it does work for Cygwin). For example, you could define constants like these for the colors:
public static final String ANSI_RESET = "u001B[0m";
public static final String ANSI_BLACK = "u001B[30m";
public static final String ANSI_RED = "u001B[31m";
public static final String ANSI_GREEN = "u001B[32m";
public static final String ANSI_YELLOW = "u001B[33m";
public static final String ANSI_BLUE = "u001B[34m";
public static final String ANSI_PURPLE = "u001B[35m";
public static final String ANSI_CYAN = "u001B[36m";
public static final String ANSI_WHITE = "u001B[37m";
Then, you could reference those as necessary.
For example, using the above constants, you could make the following red text output on supported terminals:
System.out.println(ANSI_RED + "This text is red!" + ANSI_RESET);
Update: You might want to check out the Jansi library. It provides an API and has support for Windows using JNI. I haven't tried it yet; however, it looks promising.
Update 2: Also, if you wish to change the background color of the text to a different color, you could try the following as well:
public static final String ANSI_BLACK_BACKGROUND = "u001B[40m";
public static final String ANSI_RED_BACKGROUND = "u001B[41m";
public static final String ANSI_GREEN_BACKGROUND = "u001B[42m";
public static final String ANSI_YELLOW_BACKGROUND = "u001B[43m";
public static final String ANSI_BLUE_BACKGROUND = "u001B[44m";
public static final String ANSI_PURPLE_BACKGROUND = "u001B[45m";
public static final String ANSI_CYAN_BACKGROUND = "u001B[46m";
public static final String ANSI_WHITE_BACKGROUND = "u001B[47m";
For instance:
System.out.println(ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET);
System.out.println(ANSI_RED + "This text has red text but a default background!" + ANSI_RESET);
System.out.println(ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET);
I created a library called JCDP (Java Colored Debug Printer).
For Linux it uses the ANSI escape codes that WhiteFang mentioned, but abstracts them using words instead of codes which is much more intuitive. It becomes as easy as:
print("Hello World!", Attribute.BOLD, FColor.YELLOW, BColor.GREEN);
For Windows it actually includes the JAnsi library but creates an abstraction layer over it, maintaining the intuitive and simple interface created for Linux.
This library is licensed under the MIT License so feel free to use it.
Have a look at JCDP's github repository.
Here are a list of colors in a Java class with public static
fields
Usage
System.out.println(ConsoleColors.RED + "RED COLORED" +
ConsoleColors.RESET + " NORMAL");
Note Don't forget to use the RESET
after printing as the effect will remain if it's not cleared
public class ConsoleColors {
// Reset
public static final String RESET = " 33[0m"; // Text Reset
// Regular Colors
public static final String BLACK = " 33[0;30m"; // BLACK
public static final String RED = " 33[0;31m"; // RED
public static final String GREEN = " 33[0;32m"; // GREEN
public static final String YELLOW = " 33[0;33m"; // YELLOW
public static final String BLUE = " 33[0;34m"; // BLUE
public static final String PURPLE = " 33[0;35m"; // PURPLE
public static final String CYAN = " 33[0;36m"; // CYAN
public static final String WHITE = " 33[0;37m"; // WHITE
// Bold
public static final String BLACK_BOLD = " 33[1;30m"; // BLACK
public static final String RED_BOLD = " 33[1;31m"; // RED
public static final String GREEN_BOLD = " 33[1;32m"; // GREEN
public static final String YELLOW_BOLD = " 33[1;33m"; // YELLOW
public static final String BLUE_BOLD = " 33[1;34m"; // BLUE
public static final String PURPLE_BOLD = " 33[1;35m"; // PURPLE
public static final String CYAN_BOLD = " 33[1;36m"; // CYAN
public static final String WHITE_BOLD = " 33[1;37m"; // WHITE
// Underline
public static final String BLACK_UNDERLINED = " 33[4;30m"; // BLACK
public static final String RED_UNDERLINED = " 33[4;31m"; // RED
public static final String GREEN_UNDERLINED = " 33[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = " 33[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = " 33[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = " 33[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = " 33[4;36m"; // CYAN
public static final String WHITE_UNDERLINED = " 33[4;37m"; // WHITE
// Background
public static final String BLACK_BACKGROUND = " 33[40m"; // BLACK
public static final String RED_BACKGROUND = " 33[41m"; // RED
public static final String GREEN_BACKGROUND = " 33[42m"; // GREEN
public static final String YELLOW_BACKGROUND = " 33[43m"; // YELLOW
public static final String BLUE_BACKGROUND = " 33[44m"; // BLUE
public static final String PURPLE_BACKGROUND = " 33[45m"; // PURPLE
public static final String CYAN_BACKGROUND = " 33[46m"; // CYAN
public static final String WHITE_BACKGROUND = " 33[47m"; // WHITE
// High Intensity
public static final String BLACK_BRIGHT = " 33[0;90m"; // BLACK
public static final String RED_BRIGHT = " 33[0;91m"; // RED
public static final String GREEN_BRIGHT = " 33[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = " 33[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = " 33[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = " 33[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = " 33[0;96m"; // CYAN
public static final String WHITE_BRIGHT = " 33[0;97m"; // WHITE
// Bold High Intensity
public static final String BLACK_BOLD_BRIGHT = " 33[1;90m"; // BLACK
public static final String RED_BOLD_BRIGHT = " 33[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = " 33[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = " 33[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = " 33[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = " 33[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = " 33[1;96m"; // CYAN
public static final String WHITE_BOLD_BRIGHT = " 33[1;97m"; // WHITE
// High Intensity backgrounds
public static final String BLACK_BACKGROUND_BRIGHT = " 33[0;100m";// BLACK
public static final String RED_BACKGROUND_BRIGHT = " 33[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = " 33[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = " 33[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = " 33[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = " 33[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = " 33[0;106m"; // CYAN
public static final String WHITE_BACKGROUND_BRIGHT = " 33[0;107m"; // WHITE
}
链接地址: http://www.djcxy.com/p/15316.html
上一篇: JavaScript控制台中的颜色