Java Chapter 19 - Bells & Whistles

19.1 Java Colors on Windows 10 and macOS.

You may use ANSI escape sequences to add colored text on Windows 10 Command Prompt and macOS Terminal.

For newer versions of macOS, you shouldn't have to configure anything for them to work.

For Windows 10, you may need to enable the ANSI escape sequences since they are disabled by default.  It can be enabled with the following registry modification (run from the command prompt as administrator):
reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f


The following article also covers Windows escape sequences in more detail:  How to Put Color on Windows Console
 

Java Console Colors on Windows & Mac
import java.util.*;
public class colors
{
	static String CLS = "\033[2J\033[1;1H";
	static String Red = "\033[31;1m";
	static String Green = "\033[32;1m";
	static String Yellow = "\033[33;1m";
	static String Blue = "\033[34;1m";
	static String Purple = "\033[35;1m";
	static String Cyan = "\033[36;1m";
	static String White = "\033[37;1m";
	static String Normal = "\033[0m"; // default gray color & reset background to black

	static String WhiteOnRed = "\033[41;1m";
	static String WhiteOnGreen = "\033[42;1m";
	static String WhiteOnYellow = "\033[43;1m";
	static String WhiteOnBlue = "\033[44;1m";
	static String WhiteOnPurple = "\033[45;1m";
	static String WhiteOnCyan = "\033[46;1m";

	public static void main (String[] args)
	{
		System.out.println(Red + " Red " + Green + " Green " + Yellow + " Yellow " + Blue + " Blue ");
		System.out.println(Purple+" Purple " + Cyan + " Cyan " + White + " White " + Normal + " Normal ");

		System.out.println(WhiteOnRed+" White on Red "+WhiteOnGreen+" White on Green "+WhiteOnYellow+" White on Yellow ");
		System.out.println(WhiteOnBlue+" White on Blue "+WhiteOnPurple+" White on Purple "+WhiteOnCyan+" White on Cyan ");
		System.out.print(Normal);  // reset background to black after printing inverse colors
	}
}

 

19.2 Jansi:  Another method for Java Colors on Windows

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.3 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.4 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.5 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();
    		}
	}
}