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 Default = "\033[0m"; // default gray color & reset background to black static String Red = "\033[31;1m"; static String Pink = "\033[38;5;213m"; static String Green = "\033[32;1m"; static String SeaGreen = "\033[38;5;158m"; static String Yellow = "\033[33;1m"; static String Navy = "\033[34;1m"; static String Blue = "\033[38;5;117m"; static String Cyan = "\033[36;1m"; static String Purple = "\033[35;1m"; static String Orange = "\033[38;5;208m"; static String Coral = "\033[38;5;204m"; static String White = "\033[37;1m"; 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(Default + " Default " + White + "White " + Red + "Red " + Pink + "Pink " + Green + "Green " + SeaGreen + "SeaGreen " + Yellow + "Yellow "); System.out.println(Navy + " Navy " + Blue + "Blue " + Cyan + "Cyan " + Purple + "Purple " + Orange + "Orange " + Coral + "Coral "); System.out.println(Default + 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(Default); // reset background to black after printing inverse colors } } |
Default
White Red
Pink
Green
SeaGreen Yellow Navy Blue Cyan Purple Orange Coral White on Red White on Green White on Yellow White on Blue White on Purple White on Cyan |
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. This will not work using an online compiler.
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(); } } } |