This document will help you understand how to compose Tiles into in-memory graphics objects called TileGraphics
A TileGraphics is a two dimensional in-memory image composed of Tiles. When you create one it is
held in memory but you can
draw a TileGraphics onto any DrawSurface (like a TileGrid, a Screen or another TileGraphics).
Let’s see an example:
AppConfig config = AppConfig.newBuilder() .withDefaultTileset(CP437TilesetResources.rexPaint16x16()) .build(); TileGrid tileGrid = SwingApplications.startTileGrid(config); final TileGraphics background = DrawSurfaces.tileGraphicsBuilder() .withSize(tileGrid.getSize()) // you can fetch the size of a TileGrid like this .withFiller(Tile.newBuilder() .withCharacter(Symbols.BULLET) .withBackgroundColor(ANSITileColor.BLUE) .withForegroundColor(ANSITileColor.CYAN) .build()) .build(); final TileGraphics rectangle = Shapes.buildRectangle( Position.zero(), tileGrid.getSize()) .toTileGraphics(Tile.newBuilder() .withCharacter(Symbols.BLOCK_DENSE) .withBackgroundColor(TileColor.transparent()) .withForegroundColor(ANSITileColor.RED) .build(), config.getDefaultTileset()); background.draw(rectangle, Position.zero()); // the default position is (0x0) which is the top left corner tileGrid.draw(background, Position.zero());
The result of running this code snippet should look like this:
How TileGraphics works
draw a TileGraphics on a DrawSurface the original image will stay the same and only the DrawSurface
will change which means that
draw is a copy operation. After
drawing a TileGraphics you won’t be able to tell
about a Tile that it belongs to your image. If you need this kind of information look at how
how layers work work.
You can draw TileGraphics objects onto each other (as you can see in the example above). Keep in mind that there is a special
empty Tile which is accessible from the Tile class like this:
draw a TileGraphics onto a DrawSurface you also need to supply a Position. This will be the
Position on the DrawSurface from which the copying will start. If a TileGraphics is bigger than the target
DrawSurface then the characters which are out of bounds will be ignored.