Archived

This topic is now archived and is closed to further replies.

WiseMan

[java] Is there a way to output a char in a specific color ? (console mode)

Recommended Posts

Hi, Is it possible to output a character (or string..) in a color that we want ? (in console mode) Then another char in another color, etc. Thanks in advance

Share this post


Link to post
Share on other sites
Via standard, portable Java? No.

Via kinda, sorta portable ANSI codes? Probably.

Although I''ve never tried this in Java (done it plenty in Perl), an ANSI aware terminal should accept ANSI color codes coming from a Java program. If you''re in Linux or any modern Unix your shell should handle them fine, for Windows I have no idea what cmd/command do.

The disadvantage is they are really tedious, maybe someone out there whipped up a package to make it easier.

Just try this, see what happens

System.out.println("\c[32mThis should be green\c[0m, and this should be normal");

Share this post


Link to post
Share on other sites
Because it is for a school work (i am new to Java), so i have some constraints to follow, like not using GUIs, etc.

[edited by - WiseMan on October 9, 2003 1:31:14 PM]

Share this post


Link to post
Share on other sites
Depends...since java is meant to be cross-platform compatable it does not inherently contain machine specific code for setting the color of text in console mode. If you know that you are developing a java applet or application specifically for a single platform then you can use its method of console color asignment. for example, say that you are only gonna make a console application that runs in a dos environment. you would have to embed ansi escape codes to set the color in the print line.

for a list of ANSI escape seq check out this site http://www.bluesock.org/~willg/dev/ansi.html

hope this helps



r3alityc0d3r
icq: 252463839
r3alityc0d3r@hotmail.com
"I am the Architect. I created the Matrix...Your life is the sum of a remainder of an unbalanced equation inherent to the programming of the Matrix. Which has led you, inexorably... here." Great Architect - Matrix Reloaded

Share this post


Link to post
Share on other sites
quote:
Original post by WiseMan
Unfortunnaly i am on Windows so the command doesn''t work


Yeah, apparently you can get cmd to recognize ANSI sequences, but it doesn''t work for me. I can confirm this method works in Unix environments. My bad for thinking MS might actually adopt a standard or two.

Ah well, they''re a complete pain in the butt anyway.

Share this post


Link to post
Share on other sites
You can use ANSI colour in Windows. You have to set the font that the DOS window uses. Unfortunately, it has been so long since I used it, I can''t remember it.

Do you need to use colour, or are you just doing by choice?

If it is required, it is not a very fair assigment.



First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
quote:
Original post by CaptainJester
You can use ANSI colour in Windows. You have to set the font that the DOS window uses. Unfortunately, it has been so long since I used it, I can''t remember it.



Rather than speculation, solid advice would be nice. Many websites give the steps to add ansi support to command.com (cmd does not suport ansi escape sequences at all, this is well documented).

edit c:\windows\system32\CONFIG.NT, add the line
device=c:\windows\system32\ansi.sys to it. Reboot the computer. Bring up command.com, give it an ansi escape sequence as input (which I did with both a test Java program and a test Perl script). Nope, doesn''t work. The same test programs work just fine in Linux. So if it does work, I''d like to hear how.

Share this post


Link to post
Share on other sites
quote:
Original post by tortoise
Rather than speculation, solid advice would be nice. Many websites give the steps to add ansi support to command.com (cmd does not suport ansi escape sequences at all, this is well documented).



Pardon me for trying to help. In case you didn;t notice, it was not speculation. It is fact that ANSI does work with Windows 98. I was not aware that it does not work under NT/2000. So instead of being an arse, just answer the question. Some people were telling him that it was not possible and I know it is under 95/98.



First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites