Skip to content

Instantly share code, notes, and snippets.

@mkdika
Created November 25, 2016 08:24
Show Gist options
  • Save mkdika/15f6bad7c5884ffc84ff4f4b2cd393cb to your computer and use it in GitHub Desktop.
Save mkdika/15f6bad7c5884ffc84ff4f4b2cd393cb to your computer and use it in GitHub Desktop.
import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.util.Date;
public class RpgUtil {
// update this variable to TRUE if you in development mode
public static boolean NETBEANS_MODE = true;
// constanta variable untuk FOREGROUND COLOR CLI
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = " \u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
// constanta variable untuk BACKGROUND COLOR CLI
public static final String ANSI_BG_RED = "\u001B[41m";
public static final String ANSI_BG_GREEN = "\u001B[42m";
public static final String ANSI_BG_YELLOW = "\u001B[43m";
public static final String ANSI_BG_BLUE = "\u001B[44m";
public static final String ANSI_BG_PURPLE = "\u001B[45m";
public static final String ANSI_BG_CYAN = "\u001B[46m";
public static final String ANSI_BG_WHITE = "\u001B[47m";
// untuk mengembalikan nilai random dari rentang int min - max
public static int randInt(int min, int max) {
return ((int) (Math.random() * ((max - min) + 1)) + min);
}
// untuk mengembalikan nilai random dari rentang double min - max
public static double randDouble(double min, double max) {
return (Math.random() * ((max - min) + 1) + min);
}
// untuk membesihkan layar CLI
public static void clearScreen() {
try {
if (NETBEANS_MODE) {
// clear screen di NetBeans Output Window
Robot pressbot = new Robot();
pressbot.keyPress(17); // Holds CTRL key.
pressbot.keyPress(76); // Holds L key.
pressbot.keyRelease(17); // Releases CTRL key.
pressbot.keyRelease(76); // Releases L key.
} else {
// clear screen di Windows CMD & Linux Terminal
final String os = System.getProperty("os.name");
if (os.contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
System.out.print("\033[H\033[2J");
}
}
} catch (AWTException | IOException | InterruptedException e) {
System.out.println("Error: " + e);
}
}
// untuk delay/pause proses selama mili-detik
public static void delay(long milisec) {
try {
Thread.sleep(milisec);
} catch (InterruptedException ex) {
}
}
// untuk mengambil tanggal dan waktu saat ini
public static Date getSysDate() {
return new Date();
}
// untuk konversi inputan Integer tangga, bulan, tahun, jam, menit, detik
// kedalam format Date
public static Date toDate(int date, int month, int year, int hour, int minute, int sec) {
Date d = new Date();
d.setDate(date);
d.setMonth(month);
d.setYear(year);
d.setHours(hour);
d.setMinutes(minute);
d.setSeconds(sec);
return d;
}
}
@mkdika
Copy link
Author

mkdika commented Nov 25, 2016

  • Fixing pada method clearScreen(), telah berkerja dengan baik di Netbeans Output Window, Linux Terminal, Windows Terminal, belum dicoba di Mac Terminal.
  • Update instance variable NETBEANS_MODE menjadi true apabila Anda di development mode dengan NetBeans, update menjadi false apabila hendak di jalankan di CMD Windows atau Terminal Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment