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