Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

A splash of text color with your Java

Get more control over Java console

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

December 20, 2002

Q I want to use colors and fonts in Java console. Is that possible?

A 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.

About the author

Vladimir Roubtsov has programmed in a variety of languages for more than 12 years, including Java since 1995. Currently, he develops enterprise software as a senior developer for Trilogy in Austin, Texas.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

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

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources