- All Superinterfaces:
AutoCloseable
,Closeable
,InputProvider
,Scrollable
- All Known Implementing Classes:
AbstractScreen
,TerminalScreen
,VirtualScreen
Screen is a fundamental layer in Lanterna, presenting the terminal as a bitmap-like surface where you can perform
smaller in-memory operations to a back-buffer, effectively painting out the terminal as you'd like it, and then call
refresh
to have the screen automatically apply the changes in the back-buffer to the real terminal. The
screen tracks what's visible through a front-buffer, but this is completely managed internally and cannot be expected
to know what the terminal looks like if it's being modified externally.
If you want to do more complicated drawing operations, please see the class DefaultScreenWriter
which has many
utility methods that works on Screens.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enum
This enum represents the different ways a Screen can refresh the screen, moving the back-buffer data into the front-buffer that is being displayed. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TextCharacter
This is the character Screen implementations should use as a filler is there are areas not set to any particular character. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Erases all the characters on the screen, effectively giving you a blank area.void
close()
Same as callingstopScreen()
One problem working with Screens is that whenever the terminal is resized, the front and back buffers needs to be adjusted accordingly and the program should have a chance to figure out what to do with this extra space (or less space).getBackCharacter
(int column, int row) Reads a character and its associated meta-data from the back-buffer and returns it encapsulated as a ScreenCharacter.getBackCharacter
(TerminalPosition position) Reads a character and its associated meta-data from the back-buffer and returns it encapsulated as a ScreenCharacter.A screen implementation typically keeps a location on the screen where the cursor will be placed after drawing and refreshing the buffers, this method returns that location.getFrontCharacter
(int column, int row) Reads a character and its associated meta-data from the front-buffer and returns it encapsulated as a ScreenCharacter.getFrontCharacter
(TerminalPosition position) Reads a character and its associated meta-data from the front-buffer and returns it encapsulated as a ScreenCharacter.Gets the behaviour for what to do about tab characters.Returns the size of the screen.Creates a new TextGraphics objects that is targeting this Screen for writing to.void
refresh()
This method will take the content from the back-buffer and move it into the front-buffer, making the changes visible to the terminal in the process.void
refresh
(Screen.RefreshType refreshType) This method will take the content from the back-buffer and move it into the front-buffer, making the changes visible to the terminal in the process.void
scrollLines
(int firstLine, int lastLine, int distance) Scroll a range of lines of this Screen according to given distance.void
setCharacter
(int column, int row, TextCharacter screenCharacter) Sets a character in the back-buffer to a specified value with specified colors and modifiers.void
setCharacter
(TerminalPosition position, TextCharacter screenCharacter) Sets a character in the back-buffer to a specified value with specified colors and modifiers.void
setCursorPosition
(TerminalPosition position) A screen implementation typically keeps a location on the screen where the cursor will be placed after drawing and refreshing the buffers, this method controls that location.void
setTabBehaviour
(TabBehaviour tabBehaviour) Sets the behaviour for what to do about tab characters.void
Before you can use a Screen, you need to start it.void
Calling this method will make the underlying terminal leave private mode, effectively going back to whatever state the terminal was in before callingstartScreen()
.Methods inherited from interface com.googlecode.lanterna.input.InputProvider
pollInput, readInput
-
Field Details
-
DEFAULT_CHARACTER
This is the character Screen implementations should use as a filler is there are areas not set to any particular character.
-
-
Method Details
-
startScreen
Before you can use a Screen, you need to start it. By starting the screen, Lanterna will make sure the terminal is in private mode (Screen only supports private mode), clears it (so that is can set the front and back buffers to a known value) and places the cursor in the top left corner. After calling startScreen(), you can begin using the other methods on this interface. When you want to exit from the screen and return to what you had before, you can callstopScreen()
.- Throws:
IOException
- if there was an underlying IO error when exiting from private mode
-
close
Same as callingstopScreen()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if there was an underlying IO error when exiting from private mode
-
stopScreen
Calling this method will make the underlying terminal leave private mode, effectively going back to whatever state the terminal was in before callingstartScreen()
. Once a screen has been stopped, you can start it again withstartScreen()
which will restore the screens content to the terminal.- Throws:
IOException
- if there was an underlying IO error when exiting from private mode
-
clear
void clear()Erases all the characters on the screen, effectively giving you a blank area. The default background color will be used. This is effectively the same as callingfill(TerminalPosition.TOP_LEFT_CORNER, getSize(), TextColor.ANSI.Default)
.Please note that calling this method will only affect the back buffer, you need to call refresh to make the change visible.
-
getCursorPosition
TerminalPosition getCursorPosition()A screen implementation typically keeps a location on the screen where the cursor will be placed after drawing and refreshing the buffers, this method returns that location. If it returns null, it means that the terminal will attempt to hide the cursor (if supported by the terminal).- Returns:
- Position where the cursor will be located after the screen has been refreshed or
null
if the cursor is not visible
-
setCursorPosition
A screen implementation typically keeps a location on the screen where the cursor will be placed after drawing and refreshing the buffers, this method controls that location. If you pass null, it means that the terminal will attempt to hide the cursor (if supported by the terminal).- Parameters:
position
- TerminalPosition of the new position where the cursor should be placed after refresh(), or ifnull
, hides the cursor
-
getTabBehaviour
TabBehaviour getTabBehaviour()Gets the behaviour for what to do about tab characters. If a tab character is written to the Screen, it would cause issues because we don't know how the terminal emulator would render it and we wouldn't know what state the front-buffer is in. Because of this, we convert tabs to a determined number of spaces depending on different rules that are available.- Returns:
- Tab behaviour that is used currently
- See Also:
-
setTabBehaviour
Sets the behaviour for what to do about tab characters. If a tab character is written to the Screen, it would cause issues because we don't know how the terminal emulator would render it and we wouldn't know what state the front-buffer is in. Because of this, we convert tabs to a determined number of spaces depending on different rules that are available.- Parameters:
tabBehaviour
- Tab behaviour to use when converting a \t character to a spaces- See Also:
-
getTerminalSize
TerminalSize getTerminalSize()Returns the size of the screen. This call is not blocking but should return the size of the screen as it is represented by the buffer at the time this method is called.- Returns:
- Size of the screen, in columns and rows
-
setCharacter
Sets a character in the back-buffer to a specified value with specified colors and modifiers.- Parameters:
column
- Column of the character to modify (x coordinate)row
- Row of the character to modify (y coordinate)screenCharacter
- New data to put at the specified position
-
setCharacter
Sets a character in the back-buffer to a specified value with specified colors and modifiers.- Parameters:
position
- Which position in the terminal to modifyscreenCharacter
- New data to put at the specified position
-
newTextGraphics
TextGraphics newTextGraphics()Creates a new TextGraphics objects that is targeting this Screen for writing to. Any operations done on this TextGraphics will be affecting this screen. Remember to callrefresh()
on the screen to see your changes.- Returns:
- New TextGraphic object targeting this Screen
-
getFrontCharacter
Reads a character and its associated meta-data from the front-buffer and returns it encapsulated as a ScreenCharacter.- Parameters:
column
- Which column to get the character fromrow
- Which row to get the character from- Returns:
- A
ScreenCharacter
representation of the character in the front-buffer at the specified location
-
getFrontCharacter
Reads a character and its associated meta-data from the front-buffer and returns it encapsulated as a ScreenCharacter.- Parameters:
position
- What position to read the character from- Returns:
- A
ScreenCharacter
representation of the character in the front-buffer at the specified location
-
getBackCharacter
Reads a character and its associated meta-data from the back-buffer and returns it encapsulated as a ScreenCharacter.- Parameters:
column
- Which column to get the character fromrow
- Which row to get the character from- Returns:
- A
ScreenCharacter
representation of the character in the back-buffer at the specified location
-
getBackCharacter
Reads a character and its associated meta-data from the back-buffer and returns it encapsulated as a ScreenCharacter.- Parameters:
position
- What position to read the character from- Returns:
- A
ScreenCharacter
representation of the character in the back-buffer at the specified location
-
refresh
This method will take the content from the back-buffer and move it into the front-buffer, making the changes visible to the terminal in the process. The graphics workflow with Screen would involve drawing text and text-like graphics on the back buffer and then finally calling refresh(..) to make it visible to the user.- Throws:
IOException
- If there was an underlying I/O error- See Also:
-
refresh
This method will take the content from the back-buffer and move it into the front-buffer, making the changes visible to the terminal in the process. The graphics workflow with Screen would involve drawing text and text-like graphics on the back buffer and then finally calling refresh(..) to make it visible to the user.Using this method call instead of
refresh()
gives you a little bit more control over how the screen will be refreshed.- Parameters:
refreshType
- What type of refresh to do- Throws:
IOException
- If there was an underlying I/O error- See Also:
-
doResizeIfNecessary
TerminalSize doResizeIfNecessary()One problem working with Screens is that whenever the terminal is resized, the front and back buffers needs to be adjusted accordingly and the program should have a chance to figure out what to do with this extra space (or less space). The solution is to call, at the start of your rendering code, this method, which will check if the terminal has been resized and in that case update the internals of the Screen. After this call finishes, the screen's internal buffers will match the most recent size report from the underlying terminal.- Returns:
- If the terminal has been resized since this method was last called, it will return the new size of the terminal. If not, it will return null.
-
scrollLines
void scrollLines(int firstLine, int lastLine, int distance) Scroll a range of lines of this Screen according to given distance. Screen implementations of this method do not throw IOException.- Specified by:
scrollLines
in interfaceScrollable
- Parameters:
firstLine
- first line of the range to be scrolled (top line is 0)lastLine
- last (inclusive) line of the range to be scrolleddistance
- if > 0: move lines up, else if < 0: move lines down.
-