Java Chapter 19 - Bells & Whistles

19.1 Installing Jansi for Colors and Cursor Movement

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:


 

19.2 Jansi Colors

Jansi Colors

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

public class JansiDemo
{
	public static String CLS = "\u001b[2J\u001b[1;1H";
	public static String RED = "\u001b[31;1m";
	public static String GREEN = "\u001b[32;1m";
	public static String YELLOW = "\u001b[33;1m";
	public static String BLUE = "\u001b[34;1m";
	public static String PURPLE = "\u001b[35;1m";
	public static String CYAN = "\u001b[36;1m";
	public static String WHITE = "\u001b[37;1m";
	public static String NORMAL = "\u001b[0m"; // default gray color
	public static String WHITEONBLUE = "\u001b[37;44m";
	public static 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);
	}
}

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

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


19
.4 Audio

The code below implements the playSound method to play an audio file.  It only works with .wav audio files.  The audio stops if your program ends.

Java Audio

import java.io.*;
import javax.sound.sampled.*;
public class audio
{
	public static void main(String[] args) throws IOException
	{
		playSound("starwars.wav");  // only works with .wav sound files
		while(true);    // added infinite loop because sound stops when program ends
	}

	public static void playSound(String songname)
	{
		try
		{
			AudioInputStream audioInputStream = 
				AudioSystem.getAudioInputStream(new File(songname).getAbsoluteFile());
			Clip clip = AudioSystem.getClip();
			clip.open(audioInputStream);
			clip.start();
		}
		catch(Exception ex)
		{
			System.out.println("Error with playing sound.");
			ex.printStackTrace();
    		}
	}
}