Screen Output - Sam36502/SimpleTUI GitHub Wiki

Creating A New Screen

To create a new Screen object, call the Screen constructor with your desired height and width. Screens may not be larger than 255x255. If anything less than 1x1 or greater than 255x255 is passed, the constructor will throw a BufferSizeException.

Screen scr = new Screen(10, 10);

The Buffer

The screen object contains a buffer which is a char matrix of a fixed size. When the screen is drawn with scr.printScreen() the contents of the buffer are drawn to the terminal.

Clearing The Buffer

To clear the buffer you can simple use scr.clearBuffer();

Filling The Buffer From A Matrix

To fill some predefined content from the top left corner of the screen use scr.setBuffer(matrix) This method takes a char matrix and copies it onto the buffer from the top left corner.

Example:

Buffer:
+----+
|XXXX|
|XXXX|
+----+

char[][] mat = {{'O', 'O'}, {'O', 'O'}};
scr.setBuffer(mat);

Buffer:
+----+
|OOXX|
|OOXX|
+----+

Setting A Specific Pixel

If you want to set a character at a position in the buffer, you can use the scr.setPixel(x, y, character) method. This will set the pixel at the given position to the char you pass as a parameter. It's also important to note that the coordinates are 0-indexed. This means that the top-left pixel of the screen is (0/0).

Writing Strings to The Screen

The scr.writeString(x, y, str) method lets you write a string of text starting from any position on the screen. It will continue writing vertically, until it hits the edge of the buffer. Once it has run out of room to display the text, it will cut it off.

Example:

Buffer:
+------+
|      |
|      |
+------+

scr.writeString(3, 1, "Hello!");

Buffer:
+------+
|      |
|   Hel|
+------+

The same works for vertical text, if you use scr.writeVerticalString(x, y, str);:

Buffer:
+------+
|      |
|      |
|      |
+------+

scr.writeString(3, 0, "Hello!");

Buffer:
+------+
|   H  |
|   e  |
|   l  |
+------+

Displaying The Screen

In order to output the buffer to the screen, you must use the scr.printScreen(); method. By default this will display the contents in an ASCII-art border, but this can be disabled by passing whether you want a border as a parameter.

Example:

Buffer Contents:
+---+
|X X|
| X |
|X X|
+---+

scr.printScreen();
or
scr.printScreen(true);

Output:
+---+
|X X|
| X |
|X X|
+---+

scr.printScreen(false);

Output:
X X
 X 
X X