Can I write colored letters with System.out.println?

G

gajo

I was wondering if there is a way to write colored characters to the DOS
console using System.out.println?
 
C

Chris Smith

gajo said:
I was wondering if there is a way to write colored characters to the DOS
console using System.out.println?

System.out is a character stream, and has no concept of font, style, or
color. You should fully realize that Java treats it that way, and goes
no further. Hence, it's difficult to obtain a decent-looking modern
user interface using the standard output stream in Java. If you intend
to try this, expect much frustration, and consider a native library such
as jcurses that may make this much easier.

That said, some terminals will interpret certain character sequences as
control commands to change some of these characteristics. Generally,
these sequences begin with an ESC character, and are often called
"escape sequences". They are sometimes also called ANSI sequences, in
reference to the DOS driver ANSI.SYS, which causes them to be
interpreted and used in a MS-DOS command prompt. You can send those
sequences, and the terminal will do whatever it will with them.

However, you have no way of determining what the actual capabilities of
the terminal are. There are platform-specific means of doing so (e.g.,
the TERM environment variable and termcap/terminfo in UNIX), but nothing
cross-platform for all of Java.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

I was wondering if there is a way to write colored characters to the DOS
console using System.out.println?

I don't think the console you see it actually a DOS console. It is
simulated one on a Canvas. I wrote a logging class that gives you
some colour ability that is based on JTable.

Here is the code. Add your own package, and Config class and work from
there. You could use HTML inside the lines.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
* Logs both to a Swing console and the DOS console.
* It does not log to a file, unless the console is configured to log
to a file.
*/
public class Log extends JFrame
{

/**
* severity of message
*/
public final static int INFO = 0;
public final static int WARNING = 1;
public final static int ERROR = 2;
public final static int FATAL = 3;
public final static int BUG = 4;

/**
* JFrame, but do not allow close
*/
private static Log console;

/**
* data for the log. Drops off data after a while.
*/
private static LogTableModel tableModel;
/**
* GUI visible console log
*/
private static JTable jtable;

/**
* Open the log.
*/
public static void open()
{
if ( console != null )
{
// already open
return;
}
console = new Log();
LightsOut.on();
console.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
console.setVisible( true );
}

/**
* private constructor. only used by open
*/
private Log()
{
super( "Console Log" );
setLocation( 300, 300 );
tableModel = new LogTableModel();
}

public void addNotify()
{
super.addNotify();
jtable = new JTable( tableModel );
jtable.setBackground( Config.LOG_BACKGROUND );
jtable.setForeground( Config.LOG_FOREGROUND );
TimestampRenderer.install( jtable, 0 );
SeverityRenderer.install( jtable, 1 );
// pad the message column out a bit
TableColumnModel tcm = jtable.getColumnModel();
TableColumn tc = tcm.getColumn( 2 );
tc.setPreferredWidth( 300 );

jtable.setPreferredScrollableViewportSize( new
Dimension(Config.LOG_WIDTH, Config.LOG_HEIGHT) );

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(jtable);

//Add the scroll pane to this window.
getContentPane().add( scrollPane, BorderLayout.CENTER );
pack();
hookWindowClosing();
setIconImage( ResourceGetter.createImage( "images/log32x32.gif",
console ));

}

/**
* create and attach anonymous inner class event handler for Console
window closing.
*/
private static void hookWindowClosing()
{
console.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
// what happens when user closes the Console
WindowListener windowListener = new WindowAdapter()
{
// anonymous WindowAdapter class
public void windowClosing ( WindowEvent w )
{
int answer = JOptionPane.showConfirmDialog ( console, "Do
you really want to permanently close the log?" );
if ( answer == JOptionPane.YES_OPTION )
{
close();
}

} // end windowClosing
}; // end anonymous class

console.addWindowListener( windowListener );
} // end hookWindowClosing

/**
* close the log.
*/
public static void close ()
{
if ( console != null )
{
console.setVisible( false );
console.dispose();
console = null;
tableModel = null;
jtable = null;
LightsOut.off();
}
}

/**
* log the string
*
* @param severity Severity of error: INFORMATIONAL, WARNING,
* ERROR, FATAL, BUG
*
* @param s string to log
*
* @exception IOException
*/
public static void println( int severity, String s )
{
if ( tableModel == null )
{
open();
}
tableModel.addRow ( severity, s );
String level;
switch ( severity )
{

case INFO:
level = "info: ";
break;

case WARNING:
level = "warning: ";
break;

case ERROR:
level = "error: ";
break;

case FATAL:
level = "FATAL: ";
break;

default:
case BUG:
level = "BUG: ";
break;
}
System.out.println ( level + s );

}

/**
* log the INFO string both to the Swing and DOS console.
*
* @param s string to log.
*
* @exception IOException
*/
public static void println( String s )
{
println ( INFO, s );
}

} // end class Log
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top