1

I'm doing a word search, and the words that the user has found I wanted to mark in color, but I only get the printed string code and not the color. I have created a separate class to test if the colors work, but there is no way. How can I print colors?

public class color {
    
    public static void main(String[] args) {
        String black="\033[30m"; 
        String red="\033[31m"; 
        String green="\033[32m"; 
        String yellow="\033[33m"; 
        String blue="\033[34m"; 
        String purple="\033[35m"; 
        String cyan="\033[36m"; 
        String white="\033[37m"; 
        String reset="\u001B[0m";

        System.out.println ();
        System.out.println (red + "Text string in red" + reset);
        System.out.println (green + "Text string in green" + reset);
        System.out.println (yellow + "Text string in yellow" + reset);
        System.out.println (white + "Text string in white" + reset);
        System.out.println (black + "Black text string" + reset + "(<- black text string that cannot be seen because my background is black)" + reset);
        System.out.println (blue + "Text string in blue" + reset);
        System.out.println (purple + "Magenta string" + reset);
        System.out.println (cyan + "Text string in cyan" + reset);
        System.out.println (reset + "Default color string" + reset);
        System.out.println ();
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
YoutuberMX
  • 13
  • 1
  • 3

1 Answers1

3

The ANSI color escape codes work on the assumption that there is some instance (let's call it "console") that receives characters (including the escape sequences), interprets them and displays glyphs with colors based on the interpretation.

If you send your output to such an ANSI-compatible console, then you'll get colored text, but the console you are using (the "Console" view in Eclipse?) apparently isn't ANSI-compatible.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7