Class ScreenBuffer

  • All Implemented Interfaces:
    Scrollable, TextImage

    public class ScreenBuffer
    extends java.lang.Object
    implements TextImage
    Defines a buffer used by AbstractScreen and its subclasses to keep its state of what's currently displayed and what the edit buffer looks like. A ScreenBuffer is essentially a two-dimensional array of TextCharacter with some utility methods to inspect and manipulate it in a safe way.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void copyFrom​(TextImage source, int startRowIndex, int rows, int startColumnIndex, int columns, int destinationRowOffset, int destinationColumnOffset)
      Copies the content from a TextImage into this buffer.
      void copyTo​(TextImage destination)
      Copies this TextImage's content to another TextImage.
      void copyTo​(TextImage destination, int startRowIndex, int rows, int startColumnIndex, int columns, int destinationRowOffset, int destinationColumnOffset)
      Copies this TextImage's content to another TextImage.
      TextCharacter getCharacterAt​(int column, int row)
      Returns the character stored at a particular position in this image
      TextCharacter getCharacterAt​(TerminalPosition position)
      Returns the character stored at a particular position in this image
      TerminalSize getSize()
      Returns the dimensions of this TextImage, in columns and rows
      (package private) boolean isVeryDifferent​(ScreenBuffer other, int threshold)  
      TextGraphics newTextGraphics()
      Creates a TextGraphics object that targets this TextImage for all its drawing operations.
      ScreenBuffer resize​(TerminalSize newSize, TextCharacter filler)
      Returns a copy of this image resized to a new size and using a specified filler character if the new size is larger than the old and we need to fill in empty areas.
      void scrollLines​(int firstLine, int lastLine, int distance)
      Scroll a range of lines of this TextImage according to given distance.
      void setAll​(TextCharacter character)
      Sets the text image content to one specified character (including color and style)
      void setCharacterAt​(int column, int row, TextCharacter character)
      Sets the character at a specific position in the image to a particular TextCharacter.
      void setCharacterAt​(TerminalPosition position, TextCharacter character)
      Sets the character at a specific position in the image to a particular TextCharacter.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ScreenBuffer

        public ScreenBuffer​(TerminalSize size,
                            TextCharacter filler)
        Creates a new ScreenBuffer with a given size and a TextCharacter to initially fill it with
        Parameters:
        size - Size of the buffer
        filler - What character to set as the initial content of the buffer
    • Method Detail

      • resize

        public ScreenBuffer resize​(TerminalSize newSize,
                                   TextCharacter filler)
        Description copied from interface: TextImage
        Returns a copy of this image resized to a new size and using a specified filler character if the new size is larger than the old and we need to fill in empty areas. The copy will be independent from the one this method is invoked on, so modifying one will not affect the other.
        Specified by:
        resize in interface TextImage
        Parameters:
        newSize - Size of the new image
        filler - Filler character to use on the new areas when enlarging the image (is not used when shrinking)
        Returns:
        Copy of this image, but resized
      • isVeryDifferent

        boolean isVeryDifferent​(ScreenBuffer other,
                                int threshold)
      • getSize

        public TerminalSize getSize()
        Description copied from interface: TextImage
        Returns the dimensions of this TextImage, in columns and rows
        Specified by:
        getSize in interface TextImage
        Returns:
        Size of this TextImage
      • getCharacterAt

        public TextCharacter getCharacterAt​(TerminalPosition position)
        Description copied from interface: TextImage
        Returns the character stored at a particular position in this image
        Specified by:
        getCharacterAt in interface TextImage
        Parameters:
        position - Coordinates of the character
        Returns:
        TextCharacter stored at the specified position
      • getCharacterAt

        public TextCharacter getCharacterAt​(int column,
                                            int row)
        Description copied from interface: TextImage
        Returns the character stored at a particular position in this image
        Specified by:
        getCharacterAt in interface TextImage
        Parameters:
        column - Column coordinate of the character
        row - Row coordinate of the character
        Returns:
        TextCharacter stored at the specified position
      • setCharacterAt

        public void setCharacterAt​(TerminalPosition position,
                                   TextCharacter character)
        Description copied from interface: TextImage
        Sets the character at a specific position in the image to a particular TextCharacter. If the position is outside of the image's size, this method does nothing.
        Specified by:
        setCharacterAt in interface TextImage
        Parameters:
        position - Coordinates of the character
        character - What TextCharacter to assign at the specified position
      • setCharacterAt

        public void setCharacterAt​(int column,
                                   int row,
                                   TextCharacter character)
        Description copied from interface: TextImage
        Sets the character at a specific position in the image to a particular TextCharacter. If the position is outside of the image's size, this method does nothing.
        Specified by:
        setCharacterAt in interface TextImage
        Parameters:
        column - Column coordinate of the character
        row - Row coordinate of the character
        character - What TextCharacter to assign at the specified position
      • setAll

        public void setAll​(TextCharacter character)
        Description copied from interface: TextImage
        Sets the text image content to one specified character (including color and style)
        Specified by:
        setAll in interface TextImage
        Parameters:
        character - The character to fill the image with
      • newTextGraphics

        public TextGraphics newTextGraphics()
        Description copied from interface: TextImage
        Creates a TextGraphics object that targets this TextImage for all its drawing operations.
        Specified by:
        newTextGraphics in interface TextImage
        Returns:
        TextGraphics object for this TextImage
      • copyTo

        public void copyTo​(TextImage destination)
        Description copied from interface: TextImage
        Copies this TextImage's content to another TextImage. If the destination TextImage is larger than this ScreenBuffer, the areas outside of the area that is written to will be untouched.
        Specified by:
        copyTo in interface TextImage
        Parameters:
        destination - TextImage to copy to
      • copyTo

        public void copyTo​(TextImage destination,
                           int startRowIndex,
                           int rows,
                           int startColumnIndex,
                           int columns,
                           int destinationRowOffset,
                           int destinationColumnOffset)
        Description copied from interface: TextImage
        Copies this TextImage's content to another TextImage. If the destination TextImage is larger than this TextImage, the areas outside of the area that is written to will be untouched.
        Specified by:
        copyTo in interface TextImage
        Parameters:
        destination - TextImage to copy to
        startRowIndex - Which row in this image to copy from
        rows - How many rows to copy
        startColumnIndex - Which column in this image to copy from
        columns - How many columns to copy
        destinationRowOffset - Offset (in number of rows) in the target image where we want to first copied row to be
        destinationColumnOffset - Offset (in number of columns) in the target image where we want to first copied column to be
      • copyFrom

        public void copyFrom​(TextImage source,
                             int startRowIndex,
                             int rows,
                             int startColumnIndex,
                             int columns,
                             int destinationRowOffset,
                             int destinationColumnOffset)
        Copies the content from a TextImage into this buffer.
        Parameters:
        source - Source to copy content from
        startRowIndex - Which row in the source image to start copying from
        rows - How many rows to copy
        startColumnIndex - Which column in the source image to start copying from
        columns - How many columns to copy
        destinationRowOffset - The row offset in this buffer of where to place the copied content
        destinationColumnOffset - The column offset in this buffer of where to place the copied content
      • scrollLines

        public void scrollLines​(int firstLine,
                                int lastLine,
                                int distance)
        Description copied from interface: TextImage
        Scroll a range of lines of this TextImage according to given distance. TextImage implementations of this method do not throw IOException.
        Specified by:
        scrollLines in interface Scrollable
        Specified by:
        scrollLines in interface TextImage
        Parameters:
        firstLine - first line of the range to be scrolled (top line is 0)
        lastLine - last (inclusive) line of the range to be scrolled
        distance - if > 0: move lines up, else if < 0: move lines down.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object