For Programmers: Free Programming Magazines  


Home > Archive > Java Help > June 2007 > printing in bold and colors









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printing in bold and colors
Roy Gourgi

2007-06-21, 10:08 pm

Hi,

How would I print out the following in bold and in red.
System.out.printf("Sum of Parts " + "\n");

How would I do it.

TIA

Roy


Joshua Cranmer

2007-06-21, 10:08 pm

On Thu, 21 Jun 2007 11:44:52 -0400, Roy Gourgi wrote:

> Hi,
>
> How would I print out the following in bold and in red.
> System.out.printf("Sum of Parts " + "\n");
>
> How would I do it.
>
> TIA
>
> Roy


Assuming you are outputting to an ANSI-compliant terminal (e.g., xterm,
etc.):

System.out.println("\x1B[1mBOLD\x1B[0m\t\x1B[31mRED\x1B[0m");

will print BOLD in bold and RED in red. For more information about
controlling terminal output, see http://en.wikipedia.org/wiki/
ANSI_escape_code.
Roy Gourgi

2007-06-21, 10:08 pm

Thanks Joshua,

There is no easier way than that?
It looks pretty cluttered. :)

I am using JBuilder

Roy

"Roy Gourgi" <royng@videotron.ca> wrote in message
news:02xei.88$we4.8410@wagner.videotron.net...
> Hi,
>
> How would I print out the following in bold and in red.
> System.out.printf("Sum of Parts " + "\n");
>
> How would I do it.
>
> TIA
>
> Roy
>
>



Ian Wilson

2007-06-21, 10:08 pm

Roy Gourgi wrote:[color=darkred]

GIYF
http://www.javaworld.com/javaworld/...20-console.html

[color=darkred]

GIYF
http://www.javaworld.com/javaworld/...20-console.html
Lew

2007-06-21, 10:08 pm

Roy Gourgi wrote:
> There is no easier way than that?
> It looks pretty cluttered.


Such is the nature of ANSI escape sequences. You think that's cluttered?
Take a look at printer control streams sometime.

Bear in mind that by hard-coding ANSI escape sequences you are limiting your
program to run on consoles that support those escape sewquences, which cuts
you down to what I would guess to be 90% or more of the consoles you'd
encounter. Regardless of the actual proportion it's less than 100%, so you'll
be sacrificing the "Write Once, Run Anywhere" philosophy of Java. That might
be acceptable if you know you can assure ANSI compatibility on all targeted
consoles.

If you can think up an easier way let us know. Just remember that these
escape sequences are defined externally to Java; they're data, not code.

--
Lew
Roy Gourgi

2007-06-21, 10:08 pm

Hi Joshua,

I get the error message that x1B is an illegal escape character?

How can I fix that?

Roy

"Joshua Cranmer" <Pidgeot18@verizon.net> wrote in message
news:ccxei.2959$AR5.330@trnddc06...
> On Thu, 21 Jun 2007 11:44:52 -0400, Roy Gourgi wrote:
>
>
> Assuming you are outputting to an ANSI-compliant terminal (e.g., xterm,
> etc.):
>
> System.out.println("\x1B[1mBOLD\x1B[0m\t\x1B[31mRED\x1B[0m");
>
> will print BOLD in bold and RED in red. For more information about
> controlling terminal output, see http://en.wikipedia.org/wiki/
> ANSI_escape_code.



Thomas Fritsch

2007-06-21, 10:08 pm

Roy Gourgi wrote:
[reordered top-down]
> "Joshua Cranmer" <Pidgeot18@verizon.net> wrote in message
> news:ccxei.2959$AR5.330@trnddc06...
> Hi Joshua,
>
> I get the error message that x1B is an illegal escape character?
>
> How can I fix that?
>
> Roy

Write \u001B instead \x1B

--
Thomas
Roy Gourgi

2007-06-21, 10:08 pm

Hi Thomas,

That did not work either.

Here is what I entered:

System.out.println(" \u001B[1mBOLD\u001B[0m\t\u001B[31mRED\u0
01B[0m");

And here is the output that I got
BOLD RED

Roy

"Thomas Fritsch" <i.dont.like.spam@invalid.com> wrote in message
news:newscache$vnwzjj$8w6$1@news.ops.de...
> Roy Gourgi wrote:
> [reordered top-down]
> Write \u001B instead \x1B
>
> --
> Thomas



Lew

2007-06-21, 10:08 pm

Roy Gourgi wrote:
> That did not work either.
>
> Here is what I entered:
>
> System.out.println(" \u001B[1mBOLD\u001B[0m\t\u001B[31mRED\u0
01B[0m");
>
> And here is the output that I got
> BOLD RED


Please do not top post.

Does your console use ANSI escape sequences?

--
Lew
Roy Gourgi

2007-06-21, 10:08 pm

I do not know, I am using JBuilder.

How can I find out?

Roy

"Roy Gourgi" <royng@videotron.ca> wrote in message
news:02xei.88$we4.8410@wagner.videotron.net...
> Hi,
>
> How would I print out the following in bold and in red.
> System.out.printf("Sum of Parts " + "\n");
>
> How would I do it.
>
> TIA
>
> Roy
>
>



Real Gagnon

2007-06-21, 10:08 pm

> I do not know, I am using JBuilder.

Ansi control code works only on Win9x.

http://www.rgagnon.com/javadetails/java-0047.html

On more recent Windows relaease (NT, 2000, XP), you need to use a JNI
routine.

http://www.rgagnon.com/javadetails/java-0469.html

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html
Lew

2007-06-23, 4:19 am

Real Gagnon wrote:
>
> Ansi control code works only on Win9x.


Huh. Works fine on my Linux console. Works fine in my Cygwin console in Win
XP also.

> http://www.rgagnon.com/javadetails/java-0047.html
>
> On more recent Windows relaease (NT, 2000, XP), you need to use a JNI
> routine.
>
> http://www.rgagnon.com/javadetails/java-0469.html


Or run the program in an ANSI console. Works fine for me on Win XP.

--
Lew
Real Gagnon

2007-06-23, 4:19 am

Lew <lew@lewscanon.nospam> wrote in
news:__GdnaMWN9QvoubbnZ2dnUVZ_oLinZ2d@co
mcast.com:

> Real Gagnon wrote:
>
> Huh. Works fine on my Linux console. Works fine in my Cygwin console
> in Win XP also.


My comments were for regular CMD.EXE or COMMAND.COM DOS shell.

Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html
Roedy Green

2007-06-23, 4:19 am

On Thu, 21 Jun 2007 11:44:52 -0400, "Roy Gourgi" <royng@videotron.ca>
wrote, quoted or indirectly quoted someone who said :

>How would I print out the following in bold and in red.
>System.out.printf("Sum of Parts " + "\n");


You can't do it with System.out. You write the code that would
display it on screen, then feed it to a printer rendering engine. See
http://mindprod.com/jgloss/printing.html

Another way is to generate HTML, then feed it too a browser to print.

another way is to generate some sort of metafile then feed it to a C++
utility to print.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew

2007-06-23, 4:19 am

Real Gagnon wrote:
> Lew <lew@lewscanon.nospam> wrote in
> news:__GdnaMWN9QvoubbnZ2dnUVZ_oLinZ2d@co
mcast.com:
>
>
> My comments were for regular CMD.EXE or COMMAND.COM DOS shell.


The person who suggested ANSI control codes in the first place was careful to
exclude those.

Joshua Cranmer said:
> Assuming you are outputting to an ANSI-compliant terminal (e.g., xterm,
> etc.):


So that point was already covered. Plus, I asked,

> Does your console use ANSI escape sequences?


Interestingly there was no answer to that question. I also pointed out

> Bear in mind that by hard-coding ANSI escape sequences you are limiting your program to run on consoles that support those escape sewquences,


It is helpful to the OP to know that the command shells you mentioned do not
fit into the target space for the advice. The issue that the suggestion is
not portable was well covered, however.

--
Lew
Roedy Green

2007-06-23, 7:10 pm

On Fri, 22 Jun 2007 06:05:04 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>
>You can't do it with System.out. You write the code that would
>display it on screen, then feed it to a printer rendering engine. See
>http://mindprod.com/jgloss/printing.html
>
>Another way is to generate HTML, then feed it too a browser to print.
>
>another way is to generate some sort of metafile then feed it to a C++
>utility to print.


Another way is to generate PostScript and then feed that to a PS
printer or software PS rendering engine.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com