Text color in Windows console

Discussion in 'Java' started by Xavier, Jun 3, 2005.

  1. Xavier

    Xavier Guest

    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, Jun 3, 2005
    #1
    1. Advertising

  2. Xavier wrote:
    >
    > 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" <>
    Subject: Re: California Rolls out $54M Hydrogen Highway Plan - Boondoggle
    X-Nntp-Posting-Host: e056750.nw.nos.boeing.com
    Message-ID: <>
    X-Mimeole: Produced By Microsoft MimeOLE V6.00.2800.1441
    X-Priority: 3
    X-Msmail-Priority: Normal
    Lines: 14
    Sender: (Boeing NNTP News Access)
    Organization: The Boeing Company
    X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    References: <W%mme.1516$> <> <EWtme.6265$> <> <GZ2ne.2602$> <> <> <> <dTcne.2648$> <>
    Date: Fri, 3 Jun 2005 14:29:51 GMT


    "Tim Campbell" <> wrote in message
    news:...
    ....
    > 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.
     
    Fred L. Kleinschmidt, Jun 3, 2005
    #2
    1. Advertising

  3. Xavier wrote:

    > 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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jun 3, 2005
    #3
  4. Xavier

    Real Gagnon Guest

    > 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.


    You can use a JNI routine for that.

    See http://www.rgagnon.com/javadetails/java-0469.html
    for an example.

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB code examples ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
     
    Real Gagnon, Jun 4, 2005
    #4
  5. Xavier

    Lucy Guest

    "Knute Johnson" <> wrote in message
    news:1117810422.67a27adf455c87e7d36db3e548ff38cd@1usenet...
    > Xavier wrote:
    >
    > > 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

    >
    > If you need more functionality than that, try AWT or Swing.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
     
    Lucy, Jun 4, 2005
    #5
  6. Xavier wrote:
    > 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
     
    Steve Horsley, Jun 4, 2005
    #6
  7. Xavier

    Xavier Guest

    > 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
     
    Xavier, Jun 6, 2005
    #7
  8. Xavier

    Xavier Guest

    > 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
     
    Xavier, Jun 6, 2005
    #8
  9. Xavier

    Xavier Guest

    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
     
    Xavier, Jun 6, 2005
    #9
  10. On Mon, 6 Jun 2005 09:14:24 +0200, Xavier wrote:

    (Knute)
    >> 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, ...


    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.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jun 6, 2005
    #10
  11. "Xavier" <> writes:

    >> 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 6, 2005
    #11
  12. Xavier

    Lucy Guest

    "Andrew Thompson" <> wrote in message
    news:1vo40nef14th6$.779nslaqplb0$...
    > On Mon, 6 Jun 2005 09:14:24 +0200, Xavier wrote:
    >
    > (Knute)
    > >> 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, ...

    >
    > 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.

    >
    > If the user is that interested, they can store the output and do a find
    > on '**** Error - '.
    >
    > Just a thought.
    >
    > --
    > Andrew Thompson
    > http://www.PhySci.org/codes/ Web & IT Help
    > http://www.PhySci.org/ Open-source software suite
    > http://www.1point1C.org/ Science & Technology
    > http://www.LensEscapes.com/ Images that escape the mundane
     
    Lucy, Jun 6, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Adams
    Replies:
    0
    Views:
    930
    Matt Adams
    Aug 26, 2003
  2. Ben Finney
    Replies:
    2
    Views:
    1,027
    Egor Bolonev
    Jun 30, 2003
  3. Steve M

    Changing console text color

    Steve M, Oct 10, 2005, in forum: Python
    Replies:
    2
    Views:
    522
    Steve M
    Oct 11, 2005
  4. Kamaljeet Saini
    Replies:
    0
    Views:
    499
    Kamaljeet Saini
    Feb 13, 2009
  5. Replies:
    7
    Views:
    410
    Mick White
    Feb 2, 2005
Loading...

Share This Page