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
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 } } |
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.
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.
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 $ # $ # $ # $ # $ # $ # $ # $ # $ # $ # |
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.
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(); } } } |