Class TerminalTextGraphics

java.lang.Object
com.googlecode.lanterna.graphics.AbstractTextGraphics
com.googlecode.lanterna.terminal.TerminalTextGraphics
All Implemented Interfaces:
StyleSet<TextGraphics>, TextGraphics

class TerminalTextGraphics extends AbstractTextGraphics
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

  • Field Details

  • Constructor Details

  • Method Details

    • setCharacter

      public TextGraphics setCharacter(int columnIndex, int rowIndex, TextCharacter textCharacter)
      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 character
      rowIndex - row of the location to set the character
      textCharacter - Character data to set at the current position
      Returns:
      Itself
    • setCharacter

      public TextGraphics setCharacter(TerminalPosition position, TextCharacter textCharacter)
      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 interface TextGraphics
      Overrides:
      setCharacter in class AbstractTextGraphics
      Parameters:
      position - position of the location to set the character
      textCharacter - Character data to set at the current position
      Returns:
      Itself
    • getCharacter

      public TextCharacter getCharacter(int column, int row)
      Description copied from interface: TextGraphics
      Returns the character at the specific position in the terminal. May return null if the TextGraphics implementation doesn't support it or doesn't know what the character is.
      Parameters:
      column - Column to return the character for
      row - Row to return the character for
      Returns:
      The text character at the specified position or null if not available
    • getCharacter

      public TextCharacter getCharacter(TerminalPosition position)
      Description copied from interface: TextGraphics
      Returns the character at the specific position in the terminal. May return null if the TextGraphics implementation doesn't support it or doesn't know what the character is.
      Specified by:
      getCharacter in interface TextGraphics
      Overrides:
      getCharacter in class AbstractTextGraphics
      Parameters:
      position - Position to return the character for
      Returns:
      The text character at the specified position or null if not available
    • applyGraphicState

      private void applyGraphicState(TextCharacter textCharacter) throws IOException
      Throws:
      IOException
    • getSize

      public TerminalSize 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

      public TextGraphics drawLine(TerminalPosition fromPoint, TerminalPosition toPoint, char character)
      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 interface TextGraphics
      Overrides:
      drawLine in class AbstractTextGraphics
      Parameters:
      fromPoint - From where to draw the line
      toPoint - Where to draw the line
      character - 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 interface TextGraphics
      Overrides:
      drawTriangle in class AbstractTextGraphics
      Parameters:
      p1 - First point on the screen of the triangle
      p2 - Second point on the screen of the triangle
      p3 - Third point on the screen of the triangle
      character - 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 interface TextGraphics
      Overrides:
      fillTriangle in class AbstractTextGraphics
      Parameters:
      p1 - First point on the screen of the triangle
      p2 - Second point on the screen of the triangle
      p3 - Third point on the screen of the triangle
      character - What character to use when drawing the triangle
      Returns:
      Itself
    • fillRectangle

      public TextGraphics fillRectangle(TerminalPosition topLeft, TerminalSize size, char character)
      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 interface TextGraphics
      Overrides:
      fillRectangle in class AbstractTextGraphics
      Parameters:
      topLeft - Coordinates of the top-left position of the rectangle
      size - Size (in columns and rows) of the area to draw
      character - What character to use when filling the rectangle
      Returns:
      Itself
    • drawRectangle

      public TextGraphics drawRectangle(TerminalPosition topLeft, TerminalSize size, char character)
      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 interface TextGraphics
      Overrides:
      drawRectangle in class AbstractTextGraphics
      Parameters:
      topLeft - Coordinates of the top-left position of the rectangle
      size - Size (in columns and rows) of the area to draw
      character - What character to use when drawing the outline of the rectangle
      Returns:
      Itself
    • putString

      public TextGraphics putString(int column, int row, String string)
      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 interface TextGraphics
      Overrides:
      putString in class AbstractTextGraphics
      Parameters:
      column - What column to put the string at
      row - What row to put the string at
      string - 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()