[Solved] Java graphic library for multicoloured text [closed]


I’m assuming you’re rendering text to an arbitrary component (via paintComponent()) rather than trying to modify the color of text in a JTextPane, JLabel, or other pre-existing widget.

If this is the case, you should look into using an AttributedString along with TextAttribute. These allow you to assign different styles (color, font, etc.) to various ranges of characters within a string and then render the whole string using Graphics.drawString(…). This way, the underlying graphics subsystem will handle any necessary changes to the graphics state during rendering making your code much more readable, and probably faster.

Here is an example usage.

Of course, as other have mentioned, you should also be caching your Color objects rather than recreating them over and over.

solved Java graphic library for multicoloured text [closed]