December 20, 2002
I want to use colors and fonts in Java console. Is that possible?
Manipulating console is not something pure Java can do very well. The language is obviously biased towards platform-independent
graphical user interfaces (GUIs) based on technologies like Abstract Windowing Toolkit (AWT) and Swing. That is a good thing,
but occasionally there is a need for plain, old-style text GUIs. Let's examine the options in Java.
As the simplest option, if your target platform and shell support ANSI escape sequences, you might try sending them to System.out. For instance, ESC [2J is an escape sequence for VT100-compatible terminals and Windows ANSI.SYS drivers that means "erase the entire display":
System.out.println ((char)27 + "[2J");
However, escape sequences are not very portable. For example, Windows NT provides an ANSI.SYS driver, but only for legacy DOS applications (which a JVM is not). I do not recommend this option except for the most basic functionality for a chosen OS/shell combination.
A typical Unix approach for building text GUIs is to use a higher level library such as curses. There are various Java curses ports available on the Web. They must employ native code via Java Native Interface (JNI), which means your application will only be as portable as your willingness to maintain and deploy these platform-specific libraries.
For a concrete example, Alexei Chmelev's Java Curses Library (JCurses) offers a JNI-based implementation that uses curses on Unix and Win32 to provide a platform-independent text windowing toolkit. The following JCurses program creates a 40x20 window in the center of your console and prints "Hello World!" in it:
import jcurses.system.CharColor;
import jcurses.widgets.*;
public class Main
{
public static void main(String[] args) throws Exception
{
Window w = new Window(40, 20, true, "Hello World Window");
DefaultLayoutManager mgr = new DefaultLayoutManager();
mgr.bindToContainer(w.getRootPanel());
mgr.addWidget(
new Label("Hello World!", new CharColor(CharColor.WHITE, CharColor.GREEN)),
0, 0, 40, 20,
WidgetsConstants.ALIGNMENT_CENTER,
WidgetsConstants.ALIGNMENT_CENTER);
w.show();
Thread.currentThread().sleep(5000);
w.close(); // reset the native console
}
}
A completely different approach is to create a console look-alike in AWT or Swing, add an API for managing fonts and colors,
and couple it with a PrintStream override that can be plugged in as System.out. This is precisely what Ethan Nicholas's Enigma Console does. Here is the Enigma Console version of "Hello World":
import java.awt.Color;
import enigma.console.*;
import enigma.core.Enigma;
public class Main
{
public static void main(String[] args)
{
TextAttributes attrs = new TextAttributes(Color.BLUE, Color.WHITE);
s_console.setTextAttributes(attrs);
System.out.println("Hello World!");
}
private static final Console s_console;
static
{
s_console = Enigma.getConsole("Hellow World!");
}
}
Using Enigma.getConsole() above has the side effect of installing Enigma Console as a System.out replacement. This makes it fairly easy to retrofit Enigma Console into an existing Java application.
ColorBy Anonymous on March 16, 2009, 11:00 pmis there anyway to do that in smaller amounts of text?
Reply | Read entire comment
View all comments