• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Can toString print in color?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic