| 1 |
// pjm2@ukc.ac.uk |
| 2 |
// Class for laying out text in a terminal screen in Java! |
| 3 |
// After hours of fiddling, it is possible ;-) |
| 4 |
|
| 5 |
public class TerminalScreen { |
| 6 |
|
| 7 |
private static final String ESCAPE_SEQUENCE = "["; |
| 8 |
|
| 9 |
private static final int BOLD = 1; |
| 10 |
private static final int BLACK_BG = 7; |
| 11 |
private static final int BLUE = 34; |
| 12 |
private static final int PINK = 35; |
| 13 |
private static final int RED = 31; |
| 14 |
|
| 15 |
// No-args constructor. |
| 16 |
public TerminalScreen() { |
| 17 |
// reserved. |
| 18 |
} |
| 19 |
|
| 20 |
// Set the cursor position. |
| 21 |
public void gotoxy(int x, int y) { |
| 22 |
System.out.print(ESCAPE_SEQUENCE+y+";"+x+"H"); |
| 23 |
} |
| 24 |
|
| 25 |
// Print a string at the current position. |
| 26 |
public void print(String s) { |
| 27 |
System.out.print(s); |
| 28 |
} |
| 29 |
|
| 30 |
// Print a string at the current position with the |
| 31 |
// specified colour. |
| 32 |
public void print(String s, int colour) { |
| 33 |
System.out.print(ESCAPE_SEQUENCE+colour+"m"); |
| 34 |
System.out.print(s); |
| 35 |
System.out.print(ESCAPE_SEQUENCE+"0m"); |
| 36 |
} |
| 37 |
|
| 38 |
// Print a string at the current position with a specified |
| 39 |
// foreground and background colour. |
| 40 |
public void print(String s, int colour, int other) { |
| 41 |
System.out.print(ESCAPE_SEQUENCE+other+"m"); |
| 42 |
this.print(s, colour); |
| 43 |
} |
| 44 |
|
| 45 |
// Clears the terminal screen and then resets the cursor position. |
| 46 |
public void clear() { |
| 47 |
System.out.print(ESCAPE_SEQUENCE+"2J"); |
| 48 |
System.out.print(ESCAPE_SEQUENCE+"1;24r"); |
| 49 |
} |
| 50 |
|
| 51 |
// Main method for testing! |
| 52 |
public static void main(String[] args) throws InterruptedException { |
| 53 |
|
| 54 |
TerminalScreen screen = new TerminalScreen(); |
| 55 |
|
| 56 |
screen.clear(); |
| 57 |
|
| 58 |
screen.gotoxy(34, 10); |
| 59 |
screen.print(" i - s c r e a m ", TerminalScreen.BOLD, TerminalScreen.BLACK_BG); |
| 60 |
screen.gotoxy(27, 12); |
| 61 |
screen.print("Central Monitoring System Client", TerminalScreen.BLUE); |
| 62 |
screen.gotoxy(32,13); |
| 63 |
screen.print("Unix/Linux version 1.0"); |
| 64 |
|
| 65 |
|
| 66 |
screen.gotoxy(1,1); |
| 67 |
screen.print("Top left"); |
| 68 |
screen.gotoxy(79,24); |
| 69 |
screen.print("E"); |
| 70 |
|
| 71 |
screen.gotoxy(1,24); |
| 72 |
|
| 73 |
Thread.sleep(3000); |
| 74 |
|
| 75 |
screen.clear(); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
} |