December 20, 2002
Q: I want to use colors and fonts in Java console. Is that possible?
A: 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.
Learn more about this topic
- Documentation on terminal control sequences
http://vt100.net/ - Alexei Chmelev's Java Curses Library
http://sourceforge.net/projects/javacurses/ - Ethan Nicholas's Enigma Console
http://www.ethannicholas.com/enigma/console/ - Another interesting option in Java 1.x (it no longer works in Java 2) was to provide text implementations of AWT peers
http://www.bmsi.com/tuipeer/ - Want more? See the Java Q&A index page for the full Q&A catalog
http://www.javaworld.com/columns/jw-qna-index.shtml - For more than 100 insightful Java tips, visit JavaWorld's Java Tips index page
http://www.javaworld.com/columns/jw-tips-index.shtml - Browse the AWT/Swing section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-awt-index.shtml - Browse the User Interface Design section of JavaWorld's Topical Index
http://www.javaworld.com/channel_content/jw-ui-index.shtml - Get more of your questions answered in our Java Beginner discussion
http://forums.devworld.com/webx?50@@.ee6b804 - Sign up for JavaWorld's free weekly email newsletters
http://www.javaworld.com/subscribe - You'll find a wealth of IT-related articles from our sister publications at IDG.net