Java Chapter 19 - Jansi

19.1 Installing Package

Jansi is a Java package that enables ANSI escape sequences in your console application.  ANSI escape sequences allow you to clear the screen, change the color of outputted text, and move the cursor to an X,Y location.  Ansi escape sequences only work in a Windows Command Prompt so they do not work in Eclipse nor on a Mac.  You must first install Jansi on your computer and in your compiler.  In TextPad, to add the package you go to Configure, Preferences, then Environment Variables.  You create a new variable named CLASSPATH and set it's value to the path to the Jansi jar.  You should also append the existing CLASSPATH to it.  For example:

C:\Program Files\Java\jansi-1.11.jar;%CLASSPATH%;

 

19.2 Colors

Jansi Colors

import org.fusesource.jansi.AnsiConsole;  // import Jansi package

public class JansiDemo
{
    public static final String CLS = "\u001b[2J\u001b[1;1H";
    public static final String RED = "\u001b[31;1m";
    public static final String GREEN = "\u001b[32;1m";
    public static final String YELLOW = "\u001b[33;1m";
    public static final String BLUE = "\u001b[34;1m";
    public static final String PURPLE = "\u001b[35;1m";
    public static final String CYAN = "\u001b[36;1m";
    public static final String WHITE = "\u001b[37;1m";
    public static final String NORMAL = "\u001b[0m";
// default gray color
    public static final String WHITEONBLUE = "\u001b[37;44m";
    public static final String BLUEONWHITE = "\u001b[34;47m";

    public static void main(String[] args)
    {
        AnsiConsole.systemInstall();
//do this once at beginning of program

        System.out.println(RED + "apple " + YELLOW + "banana " + GREEN + "pepper");

        System.out.println(WHITEONBLUE + "this is white on blue" + NORMAL);
    }
}
Output

apple banana pepper           
this is white on blue         
Press any key to continue . . .

You can research ANSI Escape Sequences to find more color combinations.

19.3 Jansi Cursor Movement

Ansi escape sequences also allow you to move the cursor on the screen.  This program implements a Goto (x,y) method to move the cursor position.  The top left position is 1,1.  This can be used to do animations.

Jansi Cursor Movement

import java.util.*;
import org.fusesource.jansi.AnsiConsole;

public class movement
{
   public static void main (String[] args)
   {
      AnsiConsole.systemInstall();

      for (int y=1; y<=10; y++)
      {
         Goto(1,y);
         System.out.print("$");
         try { Thread.sleep(250); }
         catch (InterruptedException ie) { ie.printStackTrace(); }
      }
      for (int y=10; y>=1; y--)
      {
         Goto(10,y);
         System.out.print("#");
         try { Thread.sleep(250); }
         catch (InterruptedException ie) { ie.printStackTrace(); }
      }
      Goto(1,11);  
// move cursor to below
   }

   static void Goto(int y, int x) // top left corner is 1,1
   {
      System.out.print("\u001b["+x+";"+y+"H");
   }
}
Output

$          #
$          #
$          #
$          #
$          #
$          #
$          #
$          #
$          #
$          #