Colour specific words in JEditorPane depending on string tags

35 Views Asked by At

I am working on a GUI for my game, which previously purely ran as text in the terminal. The GUI interfaces with the engine by simply taking the text (which would otherwise just be printed to standard output) and giving it to fields across the window.

The way colours are handled in the terminal interface is by using ANSI escape sequences. I have a method, CFG.colour(), which takes in the string to be coloured and colours it. It does this by appending the string to the ANSI sequence for the colour, and then appending the reset sequence to the end of that.

I would like to keep this backwards compatible, so there is still an option to run in the terminal if desired. I thought I could do this by using HTML, as I know that can be displayed with a JEditorPane. My thinking was, when the program is started in the terminal, the strings which store the colour tags would be set to their ANSI versions, and if the program is started in graphical mode, the colour tags would be set to HTML tags. I couldn't get this to work, but I am not very experienced with HTML.

More concisely, what I want are some Strings 'colourStart' and 'end', such that when I display

colourStart + "Hello" + end

in the JEditorPane, it will show "Hello", coloured with whatever is colour is specified by colourStart.

I know exactly what I'm saying is not possible, but that is the ideal scenario, so I figured by explaining that it would make it easier to find a solution that works for me.

0

There are 0 best solutions below