-1

I've been looking for a while how can I change the color of the console at the time of send it.

I give you an example about what i want to do :

public String color_convert(String toConvert){

 toConvert = toConvert replace -> &0 with black text
 toConvert = toConvert replace -> &1 with dark blue text

 return toConvert;

}

So the string should look like, for example

String colorConverted = color_convert("&0This is black&1 and this is blue");

And it should be displayed more or less like this: Image

Mario
  • 19
  • 6
  • I believe the most common way of doing this is styling the text using HTML. But it all depends on how the console is set up to work – ControlAltDel Nov 16 '16 at 19:02
  • If the console supports ANSI escape codes you can embed them in your string. See https://en.wikipedia.org/wiki/ANSI_escape_code – dazedandconfused Nov 16 '16 at 19:03

1 Answers1

0

In a String that in general is not possible, one needs color/font attributes parallel to the text.

However java swing and also JavaFX can represent HTML text:

public String color_convert(String toConvert) {
    String html = "<html><span>"
        + toConvert
            .replace("&0", "</span><span style=\"color: black\">")
            .replace("&1", "</span><span style=\"color: blue\">")
            .replace("\n", "<br>") // line break
        + "</span>";
    return html;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138