Skip to content

#08.3 Layer

Valkryst edited this page May 13, 2018 · 10 revisions
import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.component.Layer;

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

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

        // Construct Layer
        final Dimension dimensions = new Dimension(5, 5);
        final Point position = new Point(10, 10);
        final Layer layer = new Layer(dimensions, position);

        // Iterate over layer tiles and set them to be black
        for (int y = 0 ; y < layer.getTiles().getHeight() ; y++) {
            for (int x = 0; x < layer.getTiles().getWidth(); x++) {
                layer.getTileAt(x, y).setBackgroundColor(Color.BLACK);
            }
        }

        screen.addComponent(layer);
        screen.draw();
    }
}

Code Explanation

final Dimension dimensions = new Dimension(5, 5);
final Point position = new Point(10, 10);
final Layer layer = new Layer(dimensions, position);

Constructs a new Layer at the position (5x, 5y) with a width of 10 and a height of 10.


for (int y = 0 ; y < layer.getTiles().getHeight() ; y++) {
    for (int x = 0; x < layer.getTiles().getWidth(); x++) {
        layer.getTileAt(x, y).setBackgroundColor(Color.BLACK);
    }
}

This sets the background color of every tile on the layer to black.


screen.addComponents(layer);

This adds the layer to the panel.


screen.draw();

This redraws the screen, to show the newly added layer.

Result