I am working on a small text-based game in Java. I am trying to understand the concept of how to color the output without needing to use JNI. C++ has the ability to modify the console directly and also make system calls but as far as I can tell JAVA doesn't because of its ability to be system independent.

Is this possible or should I look at using a JFrame and the paint object?

  • \$\begingroup\$ If you rely on a JFrame, then that will provide you with more consistency, but this will likely be a little bit more work. I've been working on a JComponent class which I call JScreen, but I haven't got ANSI support in there yet. Unfortunately it's going to be quite some time before I release this for download, but you could start out with a JTable (one column, multiple rows) and write your own internal println() method that handles scrolling and colours (you may even be able to use HTML "font" tags to make the colours appear). \$\endgroup\$ Jun 22 '11 at 3:37

https://stackoverflow.com/questions/1448858/how-to-color-system-out-println-output is a answer to your question.

  • 2
    \$\begingroup\$ The question is wrong. ANSI escape codes work fine in Java. It may be that their terminal wasn't set up right? \$\endgroup\$
    – munificent
    Jun 22 '11 at 2:22
  • \$\begingroup\$ @munificent: There were some issues with ANSI output from Java in the very early days, but as I recall this got resolved long before Swing/JFC was introduced. You're right that current versions support it just fine (+1) as I've experimented with this once and can confirm that it does indeed work. The only challenge might be MS-Windows, for which XP at least (and possibly Vista and Windows 7 also) don't have ANSI support at the DOS prompt by default. Most of the other systems should be okay though. \$\endgroup\$ Jun 22 '11 at 3:35
  • 3
    \$\begingroup\$ @munificent Your post pointed me to this jansi.fusesource.org which is a small library to include to get the windows CMD window to handle ascii escape commands. \$\endgroup\$ Jun 22 '11 at 19:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .