Skip to content

#002 TileGrids & Tiles

Valkryst edited this page Jul 20, 2018 · 6 revisions

Before moving onto the more advanced concepts, it helps to know the building blocks of VTerminal. These building blocks are the TileGrid and Tile classes.

TileGrid

The TileGrid is just that, a fixed-size 2D grid of Tile or GraphicTile objects. For these Tile objects, the TileGrid class offers a number of useful functions that allow the user to retrieve single tiles as well as rows, columns, portions of rows, portions of columns, or rectangular regions of tiles.

Another extremely important feature of the TileGrid is that is can contain TileGrids within it. Think of your computer's desktop screen which has programs in movable windows within it, then within those movable windows there are buttons, text fields, and other components. This is how VTerminal handles its Component classes. You can learn more about how this hierarchy is rendered on the VTerminal wiki.

As you have seen in the previous tutorial, you may use the Tile-related functions to retrieve Tiles and display "Hello World!". We can now modify the previous example to change the background and foreground colors of the tiles that say "Hello World!".

package com.valkryst.VTerminal_Test;

import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.Tile;

import java.awt.*;
import java.io.IOException;

public class Driver {
    public static void main(String[] args) throws IOException {
        final Screen screen = new Screen();
        screen.addCanvasToFrame();

        screen.getTileAt(0, 0).setCharacter('H');
        screen.getTileAt(1, 0).setCharacter('e');
        screen.getTileAt(2, 0).setCharacter('l');
        screen.getTileAt(3, 0).setCharacter('l');
        screen.getTileAt(4, 0).setCharacter('o');

        screen.getTileAt(6, 0).setCharacter('W');
        screen.getTileAt(7, 0).setCharacter('o');
        screen.getTileAt(8, 0).setCharacter('r');
        screen.getTileAt(9, 0).setCharacter('l');
        screen.getTileAt(10, 0).setCharacter('d');
        screen.getTileAt(11, 0).setCharacter('!');

        final Tile[] helloTiles = screen.getTiles().getRowSubset(0, 0, 12);

        for (final Tile tile : helloTiles) {
            tile.setBackgroundColor(Color.BLACK);
            tile.setForegroundColor(Color.WHITE);
        }

        screen.draw();
    }
}

Code Explanation

Retrieving the Tiles

final Tile[] helloTiles = screen.getTiles().getRowSubset(0, 0, 12);

This retrieves the TileGrid of the Screen, using the getTiles() function, then retrieves the first 12 Tiles of the row beginning at position (0x, 0y).

Altering the Tiles

for (final Tile tile : helloTiles) {
    tile.setBackgroundColor(Color.BLACK);
    tile.setForegroundColor(Color.WHITE);
}

After retrieving the tiles, we iterate over them using a for-each loop. In this loop, we use the setBackgroundColor() and setForegroundColor() functions of the Tile class to change the background color of every tile to black and the foreground color to white.

Tile

Each Tile can display a single character with a unique background and foreground color, underlined state, underline thickness, visible/hidden state, horizontal/vertical flip state, and shaders.

Unlike the TileGrid class, the Tile class is fairly straightforward, so I direct you to look at the JavaDoc documentation for the Tile class in order to learn the functions that allow you to manually edit each Tile to your liking.

Useful Resources

Clone this wiki locally