I have sixteen character pairs (e.g. §c, each code starts with § and is followed with 0-9 or a-f) that represent a java.awt.Color. Here's two examples:
- §a is Color.getHSBColor(0.333f, 1.000f, 1.000f);
- §6 is Color.getHSBColor(0.167f, 1.000f, 0.502f);
I need a way to set the color of a String to any combination of these colors.
Example: §aThis text is green, but §6this text is gold.
Formatted: <green>This text is green, but <gold>this text is gold
Note that the <> tags were just examples, I need to use the following method to actually set the color in a JTextPane:
public void append(Color c, String s)
{
try
{
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, c);
this.getDocument().insertString(getDocument().getLength(), s, sas);
}
catch (BadLocationException ex) { ex.printStackTrace();}
}
So far no methods of mine have worked. I've seen several close questions, but none relating to changing text depending on 'color codes'.
Tl;dr - I need a method for programmatically inserting formatted text into a JTextPane using color codes.