Text color in Windows console

X

Xavier

Hi,

Running java (1.4) under Windows (XP), how can I change the console text
color for a part of the output text, perhaps something like :

System.out.println("This is [?1] green [?2]");

Where [?1] and [?2] are some (ANSI ?) code to set and unset text color in
green.

Thanks

Xavier
 
F

Fred L. Kleinschmidt

Xavier said:
Hi,

Running java (1.4) under Windows (XP), how can I change the console text
color for a part of the output text, perhaps something like :

System.out.println("This is [?1] green [?2]");

Where [?1] and [?2] are some (ANSI ?) code to set and unset text color in
green.

Thanks

Xavier

There is no portable way to do that. How to do it will depend on the
type of screen being used, or the visual being used on that screen (it
might be running on a black-and-white screen). It will also depend on
what kind of 'terminal' is being used for stdout - Xterm, dtterm, qterm,
dos, etc.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
#! rnews 1721
Xref: xyzzy sci.energy:132022 sci.energy.hydrogen:80848 sci.environment:397270 sci.physics:890044
Newsgroups: sci.energy,sci.environment,sci.energy.hydrogen,sci.physics,alt.politics
Path: xyzzy!nntp
From: "Fred McGalliard" <[email protected]>
Subject: Re: California Rolls out $54M Hydrogen Highway Plan - Boondoggle
X-Nntp-Posting-Host: e056750.nw.nos.boeing.com
Message-ID: <[email protected]>
X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1441
X-Priority: 3
X-Msmail-Priority: Normal
Lines: 14
Sender: (e-mail address removed) (Boeing NNTP News Access)
Organization: The Boeing Company
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
References: <W%[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]> <[email protected]>
Date: Fri, 3 Jun 2005 14:29:51 GMT


....
Though superior in many ways, the "horseless carriage" and fuel was not
cheaper than horse, wagon and fuel. Economies of choice are not always
solely driven by price and access.

A major factor, as I recall reading (wasn't there myself), was pollution.
The thousands of folk employed cleaning up horse manure from every street
and alley, and the "smog" of dried horse dung, made the city both dirty and
unhealthy.
 
K

Knute Johnson

Xavier said:
Hi,

Running java (1.4) under Windows (XP), how can I change the console text
color for a part of the output text, perhaps something like :

System.out.println("This is [?1] green [?2]");

Where [?1] and [?2] are some (ANSI ?) code to set and unset text color in
green.

Thanks

Xavier

Xavier:

Unfortunately the console is stream i/o, black and white only.

If you need more functionality than that, try AWT or Swing.
 
L

Lucy

Knute Johnson said:
Xavier said:
Hi,

Running java (1.4) under Windows (XP), how can I change the console text
color for a part of the output text, perhaps something like :

System.out.println("This is [?1] green [?2]");

Where [?1] and [?2] are some (ANSI ?) code to set and unset text color in
green.

Thanks

Xavier

Xavier:

Unfortunately the console is stream i/o, black and white only.

Not true. I just tried running my program on win xt pro. Just prior to
the java command, I entered the following command

cmd /T:78

which set the dos window to gray on silver (or similar). Then the
java program output maintained this color scheme, it did not
switch to black and white.

look into "ansi.sys" it may have what you want
 
S

Steve Horsley

Xavier said:
Hi,

Running java (1.4) under Windows (XP), how can I change the console text
color for a part of the output text, perhaps something like :

System.out.println("This is [?1] green [?2]");

Where [?1] and [?2] are some (ANSI ?) code to set and unset text color in
green.

Thanks

Xavier

The ansi sequences for changing attributes (not all hosts support
these so you are being platform specific) are Escape followed by
'[' followed by a number followed by 'm'. Escape is the character
value 27. This makes the following text come out red for example:
String red = (char) 27 + "[31m";

Attributes can be combined by separating their values with a ';'.
For example, this makes the following text appear in blinking
underlined green:
String bug = "\u0027[5;4;32m";

Attributes I know of are:
0 Normal (clear all)
1 bold
2 dim
4 underline
5 blink
7 reverse
8 blank
9 overstrike
22 normal intensity (cancel bold and blank)
24 underline off
25 blink off
27 reverse off
28 blank off
29 overstrike off
30 black
31 red
32 green
33 yellow
34 blue
35 magenta
36 cyan
37 white
40 black background
41 red background
42 green background
43 yellow background
44 blue background
45 magenta background
46 cyan background
47 white background

Steve
 
X

Xavier

Unfortunately the console is stream i/o, black and white only.
If you need more functionality than that, try AWT or Swing.

I don't want GUI, I have a "server" program producing some informations on
the console, and for example, I just want to output in red color when it is
an error.

Xavier
 
X

Xavier

Not true. I just tried running my program on win xt pro. Just prior to
the java command, I entered the following command

cmd /T:78

which set the dos window to gray on silver (or similar). Then the
java program output maintained this color scheme, it did not
switch to black and white.

Thank you, but I know this command which change all the console. I need to
change only on line, for example for an error information.

Xavier
 
X

Xavier

Hi Steve,
The ansi sequences for changing attributes (not all hosts support
these so you are being platform specific) are Escape followed by
'[' followed by a number followed by 'm'. Escape is the character
value 27. This makes the following text come out red for example:
String red = (char) 27 + "[31m";

Attributes can be combined by separating their values with a ';'.
For example, this makes the following text appear in blinking
underlined green:
String bug = "\u0027[5;4;32m";

Thak you for your answer,

I need something like that, but when I try you example (Java 1.4, Windows
XP), the output is :

red : ?[31m
bug : '[5;4;32m

The colors are not changed.

Any idea ?

Xavier
 
A

Andrew Thompson

On Mon, 6 Jun 2005 09:14:24 +0200, Xavier wrote:

(Knute)
I don't want GUI, I have a "server" program producing some informations on
the console, ...

Something that has not been *specifically* mentioned yet(?) is that
Java is not a language well suited to interacting with the console.
I suggest you prefix the console errors with..

**** Error - The kablooi has ....

...and be done with it.

If the user is that interested, they can store the output and do a find
on '**** Error - '.

Just a thought.
 
L

Lasse Reichstein Nielsen

Xavier said:
String bug = "\u0027[5;4;32m";

I think the string should start with "\u001b" instead. Escape is
character point 27, but the unicode escape sequence is in hex,
so that's "1b".
The colors are not changed.

Make sure you have an ANSI console driver installed. I have no idea
how you do that in Windows, but I remember ANSI.SYS from DOS :)
.... ah, heck, let's google for it ...
Yep, you still need to install ansi.sys. This pages seems to say
how to do it on Windows XP:
<URL:http://www.evergreen.edu/biophysics/technotes/program/ansi_esc.htm>

You should be able to test it by doing
echo ^[[32m
where "^[" means CTRL+[.

Good luck.
/L
 
L

Lucy

Andrew Thompson said:
On Mon, 6 Jun 2005 09:14:24 +0200, Xavier wrote:

(Knute)

Something that has not been *specifically* mentioned yet(?) is that
Java is not a language well suited to interacting with the console.
I suggest you prefix the console errors with..

**** Error - The kablooi has ....

..and be done with it.

Besides, after the message scrolls off the screen the color is gone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top