java.lang.Object
com.googlecode.lanterna.graphics.AbstractTextGraphics
com.googlecode.lanterna.terminal.TerminalTextGraphics
- All Implemented Interfaces:
StyleSet<TextGraphics>
,TextGraphics
This is the terminal's implementation of TextGraphics. Upon creation it takes a snapshot for the terminal's size, so
that it won't require to do an expensive lookup on every call to
getSize()
, but this also means that it can
go stale quickly if the terminal is resized. You should try to use the object quickly and then let it be GC:ed. It
will not pick up on terminal resize! Also, the state of the Terminal after an operation performed by this
TextGraphics implementation is undefined and you should probably re-initialize colors and modifiers.
Any write operation that results in an IOException will be wrapped by a RuntimeException since the TextGraphics
interface doesn't allow throwing IOException-
Nested Class Summary
Nested classes/interfaces inherited from interface com.googlecode.lanterna.graphics.StyleSet
StyleSet.Set
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate TextCharacter
private TerminalPosition
private AtomicInteger
private final Terminal
private final TerminalSize
private final Map
<TerminalPosition, TextCharacter> Fields inherited from class com.googlecode.lanterna.graphics.AbstractTextGraphics
activeModifiers, backgroundColor, foregroundColor, tabBehaviour
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
applyGraphicState
(TextCharacter textCharacter) drawLine
(TerminalPosition fromPoint, TerminalPosition toPoint, char character) Draws a line from a specified position to a specified position, using a supplied character.drawRectangle
(TerminalPosition topLeft, TerminalSize size, char character) Draws the outline of a rectangle with a particular character (and the currently active colors and modifiers).drawTriangle
(TerminalPosition p1, TerminalPosition p2, TerminalPosition p3, char character) Draws the outline of a triangle on the screen, using a supplied character.private void
It's tricky with this implementation because we can't rely on any state in between two calls to setCharacter since the caller might modify the terminal's state outside of this writer.fillRectangle
(TerminalPosition topLeft, TerminalSize size, char character) Takes a rectangle and fills it with a particular character (and the currently active colors and modifiers).fillTriangle
(TerminalPosition p1, TerminalPosition p2, TerminalPosition p3, char character) Draws a filled triangle, using a supplied character.getCharacter
(int column, int row) Returns the character at the specific position in the terminal.getCharacter
(TerminalPosition position) Returns the character at the specific position in the terminal.getSize()
Returns the size of the area that this text graphic can write to.private void
Puts a string on the screen at the specified position with the current colors and modifiers.setCharacter
(int columnIndex, int rowIndex, TextCharacter textCharacter) Sets the character at the current position to the specified value, without using the current colors and modifiers of this TextGraphics.setCharacter
(TerminalPosition position, TextCharacter textCharacter) Sets the character at the current position to the specified value, without using the current colors and modifiers of this TextGraphics.Methods inherited from class com.googlecode.lanterna.graphics.AbstractTextGraphics
clearModifiers, disableModifiers, drawImage, drawImage, drawLine, drawLine, drawLine, drawRectangle, drawTriangle, enableModifiers, fill, fillRectangle, fillTriangle, getActiveModifiers, getBackgroundColor, getForegroundColor, getTabBehaviour, newTextGraphics, putCSIStyledString, putCSIStyledString, putString, putString, putString, putString, setBackgroundColor, setCharacter, setCharacter, setForegroundColor, setModifiers, setStyleFrom, setTabBehaviour
-
Field Details
-
terminal
-
terminalSize
-
writeHistory
-
manageCallStackSize
-
lastCharacter
-
lastPosition
-
-
Constructor Details
-
TerminalTextGraphics
TerminalTextGraphics(Terminal terminal) throws IOException - Throws:
IOException
-
-
Method Details
-
setCharacter
Description copied from interface:TextGraphics
Sets the character at the current position to the specified value, without using the current colors and modifiers of this TextGraphics.- Parameters:
columnIndex
- column of the location to set the characterrowIndex
- row of the location to set the charactertextCharacter
- Character data to set at the current position- Returns:
- Itself
-
setCharacter
Description copied from interface:TextGraphics
Sets the character at the current position to the specified value, without using the current colors and modifiers of this TextGraphics.- Specified by:
setCharacter
in interfaceTextGraphics
- Overrides:
setCharacter
in classAbstractTextGraphics
- Parameters:
position
- position of the location to set the charactertextCharacter
- Character data to set at the current position- Returns:
- Itself
-
getCharacter
Description copied from interface:TextGraphics
Returns the character at the specific position in the terminal. May returnnull
if the TextGraphics implementation doesn't support it or doesn't know what the character is.- Parameters:
column
- Column to return the character forrow
- Row to return the character for- Returns:
- The text character at the specified position or
null
if not available
-
getCharacter
Description copied from interface:TextGraphics
Returns the character at the specific position in the terminal. May returnnull
if the TextGraphics implementation doesn't support it or doesn't know what the character is.- Specified by:
getCharacter
in interfaceTextGraphics
- Overrides:
getCharacter
in classAbstractTextGraphics
- Parameters:
position
- Position to return the character for- Returns:
- The text character at the specified position or
null
if not available
-
applyGraphicState
- Throws:
IOException
-
getSize
Description copied from interface:TextGraphics
Returns the size of the area that this text graphic can write to. Any attempts of placing characters outside of this area will be silently ignored.- Returns:
- Size of the writable area that this TextGraphics can write too
-
drawLine
Description copied from interface:TextGraphics
Draws a line from a specified position to a specified position, using a supplied character. The current foreground color, background color and modifiers will be applied.- Specified by:
drawLine
in interfaceTextGraphics
- Overrides:
drawLine
in classAbstractTextGraphics
- Parameters:
fromPoint
- From where to draw the linetoPoint
- Where to draw the linecharacter
- Character to use for the line- Returns:
- Itself
-
drawTriangle
public TextGraphics drawTriangle(TerminalPosition p1, TerminalPosition p2, TerminalPosition p3, char character) Description copied from interface:TextGraphics
Draws the outline of a triangle on the screen, using a supplied character. The triangle will begin at p1, go through p2 and then p3 and then back to p1. The current foreground color, background color and modifiers will be applied.- Specified by:
drawTriangle
in interfaceTextGraphics
- Overrides:
drawTriangle
in classAbstractTextGraphics
- Parameters:
p1
- First point on the screen of the trianglep2
- Second point on the screen of the trianglep3
- Third point on the screen of the trianglecharacter
- What character to use when drawing the lines of the triangle- Returns:
- Itself
-
fillTriangle
public TextGraphics fillTriangle(TerminalPosition p1, TerminalPosition p2, TerminalPosition p3, char character) Description copied from interface:TextGraphics
Draws a filled triangle, using a supplied character. The triangle will begin at p1, go through p2 and then p3 and then back to p1. The current foreground color, background color and modifiers will be applied.- Specified by:
fillTriangle
in interfaceTextGraphics
- Overrides:
fillTriangle
in classAbstractTextGraphics
- Parameters:
p1
- First point on the screen of the trianglep2
- Second point on the screen of the trianglep3
- Third point on the screen of the trianglecharacter
- What character to use when drawing the triangle- Returns:
- Itself
-
fillRectangle
Description copied from interface:TextGraphics
Takes a rectangle and fills it with a particular character (and the currently active colors and modifiers). The topLeft coordinate is inclusive.For example, calling fillRectangle with size being the size of the terminal and top-left value being the terminal's top-left (0x0) corner will fill the entire terminal with this character.
The current foreground color, background color and modifiers will be applied.
- Specified by:
fillRectangle
in interfaceTextGraphics
- Overrides:
fillRectangle
in classAbstractTextGraphics
- Parameters:
topLeft
- Coordinates of the top-left position of the rectanglesize
- Size (in columns and rows) of the area to drawcharacter
- What character to use when filling the rectangle- Returns:
- Itself
-
drawRectangle
Description copied from interface:TextGraphics
Draws the outline of a rectangle with a particular character (and the currently active colors and modifiers). The topLeft coordinate is inclusive.For example, calling drawRectangle with size being the size of the terminal and top-left value being the terminal's top-left (0x0) corner will draw a border around the terminal.
The current foreground color, background color and modifiers will be applied.
- Specified by:
drawRectangle
in interfaceTextGraphics
- Overrides:
drawRectangle
in classAbstractTextGraphics
- Parameters:
topLeft
- Coordinates of the top-left position of the rectanglesize
- Size (in columns and rows) of the area to drawcharacter
- What character to use when drawing the outline of the rectangle- Returns:
- Itself
-
putString
Description copied from interface:TextGraphics
Puts a string on the screen at the specified position with the current colors and modifiers. If the string contains newlines (\r and/or \n), the method will stop at the character before that; you have to manage multi-line strings yourself! The current foreground color, background color and modifiers will be applied.- Specified by:
putString
in interfaceTextGraphics
- Overrides:
putString
in classAbstractTextGraphics
- Parameters:
column
- What column to put the string atrow
- What row to put the string atstring
- String to put on the screen- Returns:
- Itself
-
enterAtomic
private void enterAtomic()It's tricky with this implementation because we can't rely on any state in between two calls to setCharacter since the caller might modify the terminal's state outside of this writer. However, many calls inside TextGraphics will indeed make multiple calls in setCharacter where we know that the state won't change (actually, we can't be 100% sure since the caller might create a separate thread and maliciously write directly to the terminal while call one of the draw/fill/put methods in here). We could just set the state before writing every single character but that would be inefficient. Rather, we keep a counter of if we are inside an 'atomic' (meaning we know multiple calls to setCharacter will have the same state). Some drawing methods call other drawing methods internally for their implementation so that's why this is implemented with an integer value instead of a boolean; when the counter reaches zero we remove the memory of what state the terminal is in. -
leaveAtomic
private void leaveAtomic()
-