public class TextCanvas extends TexturedRectangle
A text rendering surface in 3D space that displays a grid of characters.

TextCanvas extends TexturedRectangle and renders a 2D grid of characters (rows and columns) onto a texture-mapped rectangle. Each character cell supports independent foreground and background colors through CanvasCharacter.

Characters are rendered using a monospace font at a fixed size (16 x 32 texture pixels per character). The canvas automatically switches between two render modes based on the viewer's distance and viewing angle:

  • RenderMode.TEXTURE -- renders all characters to a shared texture bitmap. This is efficient for distant or obliquely viewed text.
  • RenderMode.CHARACTERS -- renders each character as an individual textured polygon with higher quality anti-aliased tiles. Used when the viewer is close and looking at the text nearly head-on.

Usage example


 Transform location = new Transform(new Point3D(0, 0, 500));
 TextCanvas canvas = new TextCanvas(location, "Hello, World!",
         Color.WHITE, Color.BLACK);
 shapeCollection.addShape(canvas);

 // Or create a blank canvas and write to it
 TextCanvas blank = new TextCanvas(location, new TextPointer(10, 40),
         Color.GREEN, Color.BLACK);
 blank.locate(0, 0);
 blank.print("Line 1");
 blank.locate(1, 0);
 blank.print("Line 2");
 
See Also:
  • Field Details

    • FONT_CHAR_WIDTH

      public static final int FONT_CHAR_WIDTH
      Font character width in world coordinates.
      See Also:
    • FONT_CHAR_HEIGHT

      public static final int FONT_CHAR_HEIGHT
      Font character height in world coordinates.
      See Also:
    • FONT_CHAR_WIDTH_TEXTURE_PIXELS

      public static final int FONT_CHAR_WIDTH_TEXTURE_PIXELS
      Font character width in texture pixels.
      See Also:
    • FONT_CHAR_HEIGHT_TEXTURE_PIXELS

      public static final int FONT_CHAR_HEIGHT_TEXTURE_PIXELS
      Font character height in texture pixels.
      See Also:
    • FONT

      public static final Font FONT
  • Constructor Details

    • TextCanvas

      public TextCanvas(Transform location, String text, Color foregroundColor, Color backgroundColor)
      Creates a text canvas initialized with the given text string.

      The canvas dimensions are automatically computed from the text content (number of lines determines rows, longest line determines columns).

      Parameters:
      location - the 3D transform positioning this canvas in the scene
      text - the initial text content (may contain newlines for multiple rows)
      foregroundColor - the default text color
      backgroundColor - the default background color
    • TextCanvas

      public TextCanvas(Transform location, TextPointer dimensions, Color foregroundColor, Color backgroundColor)
      Creates a blank text canvas with the specified dimensions.

      The canvas is initialized with spaces in every cell, filled with the specified background color. Characters can be written using putChar(char), print(String), or setText(String).

      Parameters:
      location - the 3D transform positioning this canvas in the scene
      dimensions - the grid size as a TextPointer where row is the number of rows and column is the number of columns
      foregroundColor - the default text color
      backgroundColor - the default background color
  • Method Details

    • getTextDimensions

      public static TextPointer getTextDimensions(String text)
      Computes the row and column dimensions needed to fit the given text.
      Parameters:
      text - the text content (may contain newlines)
      Returns:
      a TextPointer where row is the number of lines and column is the length of the longest line
    • beforeTransformHook

      public void beforeTransformHook(TransformStack transformPipe, RenderingContext context)
      Description copied from class: AbstractCompositeShape
      This method should be overridden by anyone wanting to customize shape before it is rendered.
      Overrides:
      beforeTransformHook in class AbstractCompositeShape
    • clear

      public void clear()
      Clears the entire canvas, resetting all characters to spaces with the default colors.

      Both the character grid and the backing texture bitmap are reset.

    • getCharLocation

      public Point3D getCharLocation(int row, int column)
      Computes the 3D world coordinate for the center of the character cell at the given row and column.
      Parameters:
      row - the row index (0-based, from the top)
      column - the column index (0-based, from the left)
      Returns:
      the 3D coordinate of the character cell center, relative to the canvas origin
    • getSize

      public TextPointer getSize()
      Returns the dimensions of this text canvas.
      Returns:
      a TextPointer where row is the number of rows and column is the number of columns
    • locate

      public void locate(int row, int column)
      Moves the internal cursor to the specified row and column.

      Subsequent calls to putChar(char) and print(String) will begin writing at this position.

      Parameters:
      row - the target row (0-based)
      column - the target column (0-based)
    • print

      public void print(String text)
      Prints a string starting at the current cursor location, advancing the cursor after each character.

      When the cursor reaches the end of a row, it wraps to the beginning of the next row.

      Parameters:
      text - the text to print
      See Also:
    • putChar

      public void putChar(char character)
      Writes a character at the current cursor location and advances the cursor.

      The cursor moves one column to the right. If it exceeds the row width, it wraps to column 0 of the next row.

      Parameters:
      character - the character to write
    • putChar

      public void putChar(int row, int column, char character)
      Writes a character at the specified row and column using the current foreground and background colors.

      If the row or column is out of bounds, the call is silently ignored.

      Parameters:
      row - the row index (0-based)
      column - the column index (0-based)
      character - the character to write
    • putChar

      public void putChar(TextPointer location, char character)
      Writes a character at the position specified by a TextPointer.
      Parameters:
      location - the row and column position
      character - the character to write
    • setBackgroundColor

      public void setBackgroundColor(Color backgroundColor)
      Sets the default background color for subsequent character writes.
      Parameters:
      backgroundColor - the new background color
    • setForegroundColor

      public void setForegroundColor(Color foregroundColor)
      Sets the default foreground (text) color for subsequent character writes.
      Parameters:
      foregroundColor - the new foreground color
    • setText

      public void setText(String text)
      Replaces the entire canvas content with the given multi-line text string.

      Each line of text (separated by newlines) is written to consecutive rows, starting from row 0. Characters beyond the canvas width are ignored.

      Parameters:
      text - the text to display (may contain newline characters)
    • setTextColor

      public void setTextColor(Color color)
      Sets the foreground color of all existing characters on the canvas.

      This updates the color of every CanvasCharacter in the grid, but does not affect the backing texture. It is primarily useful in RenderMode.CHARACTERS mode.

      Parameters:
      color - the new foreground color for all characters