• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can toString print in color?

 
David Tschoepe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to spruce up an assignment for class and was wondering if there was a way to make the output from toString to the console display in some color other than white?

Thanks,

David
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, toString() doesn't print anything. It simply returns a String representation of the object it is called on. However, OutputStreams and Writers from the java.io package DO print. There may be a way to print in color, if your terminal supports ANSI codes. However, this would take some sophisticated manipulation of the OutputStream. Even more importantly, it seems to me that any attempt to do this would be system dependent.

Anyway, that's my long-winded way of saying that I don't know how to do it. Perhaps I've given you some ideas to think on or keywords you can google for.

Layne
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
google for curses. it's a time-honored Unix library meant to alleviate some of the platform-dependency of this sort of thing; basically, it's an abstraction library meant to translate between platform-independent, higher-level notions (even some text-mode "GUI" widgets!) and whatever the machine-dependent lower layers need.

as far as i can tell (mostly from this JavaWorld article) you'll likely still need to use JNI to access a curses implementation. but at least you won't have to hardcode ANSI escape sequences in your code, and curses has been ported to Windows, so it's quite widely available.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
curses may have been ported to Windows but it's not generally available on Windows machines.
The results of relying on it are therefore unpredictable.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unless you choose to distribute it with your application. granted that this will bloat your jar files somewhat, but at least it will save you the bother and headache of hand-coding escape sequences.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic