Class Text

java.lang.Object
org.openpatch.scratch.Text

public class Text extends Object
The Text class represents a text object that can be displayed on the stage. It provides methods to set the text content, position, size, style, and alignment. The text can be displayed in various styles such as plain, speech bubble, or box. The text can be associated with a sprite and displayed relative to the sprite's position. Example usage:

 Text text = new Text("Hello, World!", 100, 100, 200);
 text.setStyle(TextStyle.SPEAK);
 text.setBackgroundColor(255, 255, 255);
 text.setTextColor(0, 0, 0);
 text.setStrokeColor(0, 0, 0);
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
    SPEAK_BUBBLE_MAX_LIMIT is the maximum width of the speech bubble.
    static int
    SPEAK_BUBBLE_MIN_LIMIT is the minimum width of the speech bubble.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Text object with default values.
    Text(String text, double x, double y, double width)
    Constructs a new Text object with the specified text, position, and width.
    Text(String text, double x, double y, double width, TextStyle style)
    Constructs a new Text object with the specified text, position, width, and style.
    Constructs a new Text object associated with a given Sprite.
    Copy constructor for the Text class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    addFont(String name, String path)
    Adds a new font to the list of fonts if it does not already exist.
    void
    draw(processing.core.PGraphics buffer)
     
    Returns where the text sits relative to its position.
    int
    Returns the index of the current font.
    Retrieves the name of the current font.
    static String
    Returns the font that text is written in by default.
    static int
    Returns the size text is written in by default.
    static int[]
    Returns the sizes prepared in advance.
    Retrieves the name of the font.
    Retrieves the current position as a Vector2 object.
    Retrieves the current stage associated with this object.
    Returns the style the text is drawn in.
    int
    Returns the size of the text.
    double
    Returns the width of the text.
    double
    Returns the x-coordinate of this object.
    double
    Returns the y-coordinate of this object.
    void
    goLayersBackwards(int number)
    Moves the text backwards by a specified number of layers in the stage.
    void
    goLayersForwards(int number)
    Moves the text forward by a specified number of layers within its stage.
    void
    Moves this text to the back layer of the stage, behind every other text on it.
    void
    Moves this text to the front layer of the stage, in front of every other text on it.
    static boolean
    Returns whether letters are smoothed.
    boolean
    Checks if the current instance is a UI element.
    void
    Advances to the next font in the list of available fonts.
    void
    Removes this object from its current stage if it is associated with one.
    void
     
    void
    Sets the alignment of the text.
    void
    Sets the background color using the specified hue value.
    void
    setBackgroundColor(int r, int g, int b)
    Sets the background color using the specified RGB values.
    void
    Sets the background color of the text.
    void
    Sets the font name for the text.
    void
    setIsUI(boolean isUI)
    Sets the UI status of this object.
    void
    setPosition(double x, double y)
    Sets the position of the text
    void
    Sets the position of the text object using a Vector2 object.
    void
    setStrokeColor(double h)
    Sets the stroke color using the specified hue value.
    void
    setStrokeColor(int r, int g, int b)
    Sets the stroke color using the specified RGB values.
    void
    Sets the stroke color for the text.
    void
    Sets the style of the text.
    void
    setTextColor(double h)
    Sets the text color using the specified hue value.
    void
    setTextColor(int r, int g, int b)
    Sets the text color using RGB values.
    void
    Sets the color of the text.
    void
    setTextSize(int size)
    Sets the size of the text.
    void
    setWidth(double width)
    Sets the width of the text.
    void
    setX(double x)
    Sets the x-coordinate for this object.
    void
    setY(double y)
    Sets the Y coordinate of the text.
    void
    Displays the specified text.
    void
    showText(String text, int millis)
    Displays the specified text for a given duration.
    void
    Switches the current font to the font with the specified name.
    static void
    Chooses the font all text is written in, keeping the current size.
    static void
    useFont(String path, int size)
    Chooses the font all text is written in, and how big it is.
    static void
    useFontSizes(int... sizes)
    Prepares several sizes of the font in advance.
    static void
    useSmoothing(boolean smooth)
    Chooses whether letters are smoothed.
    void
    This method is called when the object is added to the stage.
    void
    This method is called when the text is added to the stage.
    void
    This method is called when the object is removed from the stage.
    void
    This method is called when the text is removed from the stage.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SPEAK_BUBBLE_MAX_LIMIT

      public static int SPEAK_BUBBLE_MAX_LIMIT
      SPEAK_BUBBLE_MAX_LIMIT is the maximum width of the speech bubble.
    • SPEAK_BUBBLE_MIN_LIMIT

      public static int SPEAK_BUBBLE_MIN_LIMIT
      SPEAK_BUBBLE_MIN_LIMIT is the minimum width of the speech bubble.
  • Constructor Details

    • Text

      public Text()
      Constructs a new Text object with default values. The text is initialized to an empty string, positioned at (0, 0), with a default size of 0. The text style is set to plain and the text is centred on its position, as every text is. Call setAlign(TextAlign) for anything else.
    • Text

      public Text(Sprite s)
      Constructs a new Text object associated with a given Sprite.
      Parameters:
      s - the Sprite to associate with this Text object
    • Text

      public Text(String text, double x, double y, double width)
      Constructs a new Text object with the specified text, position, and width.
      Parameters:
      text - the text to be displayed
      x - the x-coordinate of the text's position
      y - the y-coordinate of the text's position
      width - the width of the text
    • Text

      public Text(String text, double x, double y, double width, TextStyle style)
      Constructs a new Text object with the specified text, position, width, and style.
      Parameters:
      text - the text content to be displayed
      x - the x-coordinate of the text's position
      y - the y-coordinate of the text's position
      width - the width of the text area
      style - the style to be applied to the text
    • Text

      public Text(Text t)
      Copy constructor for the Text class. Creates a new Text object by copying the properties of the given Text object.
      Parameters:
      t - the Text object to copy
  • Method Details

    • useFont

      public static void useFont(String path, int size)
      Chooses the font all text is written in, and how big it is.

      Call it before the first stage is created. The font is loaded while the window starts up, so choosing one later is not reliable.

      
       public static void main(String[] args) {
         Text.useFont("assets/Retro Gaming.ttf", 11);
         new MyStage();
       }
       
      Parameters:
      path - the path to a .ttf or .otf file
      size - the size to write in
    • useFont

      public static void useFont(String path)
      Chooses the font all text is written in, keeping the current size.
      Parameters:
      path - the path to a .ttf or .otf file
    • useFontSizes

      public static void useFontSizes(int... sizes)
      Prepares several sizes of the font in advance. Only needed when text is shown at more than one size, so that every size looks sharp.
      Parameters:
      sizes - the sizes to prepare
    • useSmoothing

      public static void useSmoothing(boolean smooth)
      Chooses whether letters are smoothed. Turn it off for pixel fonts.
      Parameters:
      smooth - true to smooth the letters
    • getDefaultFont

      public static String getDefaultFont()
      Returns the font that text is written in by default.
      Returns:
      the path to the font file
    • getDefaultFontSize

      public static int getDefaultFontSize()
      Returns the size text is written in by default.
      Returns:
      the font size
    • getDefaultFontSizes

      public static int[] getDefaultFontSizes()
      Returns the sizes prepared in advance.
      Returns:
      the font sizes
    • isSmoothing

      public static boolean isSmoothing()
      Returns whether letters are smoothed.
      Returns:
      true if smoothing is on
    • addedToStage

      public void addedToStage(Stage stage)
    • removedFromStage

      public void removedFromStage(Stage stage)
    • whenAddedToStage

      public void whenAddedToStage()
      This method is called when the object is added to the stage. Override this method to define custom behavior when the object is added to the stage.
    • whenAddedToStage

      public void whenAddedToStage(Stage stage)
      This method is called when the text is added to the stage. Override this method to define custom behavior when the text.
      Parameters:
      stage - The stage to which the text is added.
    • whenRemovedFromStage

      public void whenRemovedFromStage()
      This method is called when the object is removed from the stage. Override this method to define custom behavior that should occur when the object is no longer part of the stage.
    • whenRemovedFromStage

      public void whenRemovedFromStage(Stage stage)
      This method is called when the text is removed from the stage. Override this method to define custom behavior that should occur.
      Parameters:
      stage - The stage from which the text is removed.
    • remove

      public void remove()
      Removes this object from its current stage if it is associated with one. If the object is not associated with any stage, this method does nothing.
    • getStage

      public Stage getStage()
      Retrieves the current stage associated with this object.
      Returns:
      the current stage
    • goToFrontLayer

      public void goToFrontLayer()
      Moves this text to the front layer of the stage, in front of every other text on it. If the text is not on a stage, the method does nothing.

      Texts are drawn on top of the sprites, so this orders a text against the other texts rather than against the sprites.

      Scratch Block:
      go to [front v] layer
    • goToBackLayer

      public void goToBackLayer()
      Moves this text to the back layer of the stage, behind every other text on it. If the text is not on a stage, the method does nothing.

      Texts are drawn on top of the sprites, so this orders a text against the other texts rather than against the sprites.

      Scratch Block:
      go to [back v] layer
    • goLayersForwards

      public void goLayersForwards(int number)
      Moves the text forward by a specified number of layers within its stage. If the text is not on a stage, the method does nothing.
      Parameters:
      number - the number of layers to move the text forward
      Scratch Block:
      go [forward v] (number) layers
    • goLayersBackwards

      public void goLayersBackwards(int number)
      Moves the text backwards by a specified number of layers in the stage. If the text is not on a stage, the method does nothing.
      Parameters:
      number - the number of layers to move the text backwards
      Scratch Block:
      go [backward v] (number) layers
    • addFont

      public void addFont(String name, String path)
      Adds a new font to the list of fonts if it does not already exist.
      Parameters:
      name - the name of the font to be added
      path - the path to the font file
    • switchFont

      public void switchFont(String name)
      Switches the current font to the font with the specified name.
      Parameters:
      name - the name of the font to switch to
    • nextFont

      public void nextFont()
      Advances to the next font in the list of available fonts. The current font index is incremented by one and wraps around to the beginning of the list if it exceeds the number of available fonts.
    • getCurrentFontName

      public String getCurrentFontName()
      Retrieves the name of the current font.
      Returns:
      the name of the current font, or null if no fonts are available.
    • getCurrentFontIndex

      public int getCurrentFontIndex()
      Returns the index of the current font.
      Returns:
      the index of the current font
    • setPosition

      public void setPosition(double x, double y)
      Sets the position of the text
      Parameters:
      x - a x coordinate
      y - a y coordinate
    • setPosition

      public void setPosition(Vector2 v)
      Sets the position of the text object using a Vector2 object.
      Parameters:
      v - the Vector2 object containing the x and y coordinates
    • getPosition

      public Vector2 getPosition()
      Retrieves the current position as a Vector2 object.
      Returns:
      a Vector2 object representing the current x and y coordinates.
    • setX

      public void setX(double x)
      Sets the x-coordinate for this object.
      Parameters:
      x - the new x-coordinate value
    • getX

      public double getX()
      Returns the x-coordinate of this object.
      Returns:
      the x-coordinate as a double
    • setY

      public void setY(double y)
      Sets the Y coordinate of the text.
      Parameters:
      y - the new Y coordinate value
    • getY

      public double getY()
      Returns the y-coordinate of this object.
      Returns:
      the y-coordinate as a double
    • showText

      public void showText(String text)
      Displays the specified text.
      Parameters:
      text - The text to be displayed. If the text is null or empty, the text will be set to null.
    • showText

      public void showText(String text, int millis)
      Displays the specified text for a given duration.
      Parameters:
      text - The text to be displayed.
      millis - The duration in milliseconds for which the text should be displayed.
    • setStyle

      public void setStyle(TextStyle style)
      Sets the style of the text.
      Parameters:
      style - the TextStyle to be applied to the text
    • getStyle

      public TextStyle getStyle()
      Returns the style the text is drawn in.
      Returns:
      the style, never null
    • setBackgroundColor

      public void setBackgroundColor(int r, int g, int b)
      Sets the background color using the specified RGB values.
      Parameters:
      r - the red component of the color (0-255)
      g - the green component of the color (0-255)
      b - the blue component of the color (0-255)
    • setBackgroundColor

      public void setBackgroundColor(double h)
      Sets the background color using the specified hue value.
      Parameters:
      h - the hue value for the background color
    • setBackgroundColor

      public void setBackgroundColor(Color c)
      Sets the background color of the text.
      Parameters:
      c - the new background color to be set
    • setTextColor

      public void setTextColor(int r, int g, int b)
      Sets the text color using RGB values.
      Parameters:
      r - the red component of the color (0-255)
      g - the green component of the color (0-255)
      b - the blue component of the color (0-255)
    • setTextColor

      public void setTextColor(double h)
      Sets the text color using the specified hue value.
      Parameters:
      h - the hue value for the text color
    • setTextColor

      public void setTextColor(Color c)
      Sets the color of the text.
      Parameters:
      c - the new color to be set for the text
    • setStrokeColor

      public void setStrokeColor(int r, int g, int b)
      Sets the stroke color using the specified RGB values.
      Parameters:
      r - the red component of the color (0-255)
      g - the green component of the color (0-255)
      b - the blue component of the color (0-255)
    • setStrokeColor

      public void setStrokeColor(double h)
      Sets the stroke color using the specified hue value.
      Parameters:
      h - the hue value for the stroke color
    • setStrokeColor

      public void setStrokeColor(Color c)
      Sets the stroke color for the text.
      Parameters:
      c - the color to set as the stroke color
    • setFont

      public void setFont(String name)
      Sets the font name for the text.
      Parameters:
      name - the name of the font to be set
    • getFont

      public String getFont()
      Retrieves the name of the font.
      Returns:
      the name of the font as a String.
    • setTextSize

      public void setTextSize(int size)
      Sets the size of the text.
      Parameters:
      size - the new size of the text
    • getTextSize

      public int getTextSize()
      Returns the size of the text.
      Returns:
      the size of the text as an integer
    • getWidth

      public double getWidth()
      Returns the width of the text.
      Returns:
      the width of the text as a double
    • setWidth

      public void setWidth(double width)
      Sets the width of the text.
      Parameters:
      width - the new width to set
    • setAlign

      public void setAlign(TextAlign align)
      Sets the alignment of the text.
      Parameters:
      align - where the text should sit relative to its position
    • getAlign

      public TextAlign getAlign()
      Returns where the text sits relative to its position.
      Returns:
      the alignment
    • setIsUI

      public void setIsUI(boolean isUI)
      Sets the UI status of this object.
      Parameters:
      isUI - a boolean indicating whether this object is part of the UI
    • isUI

      public boolean isUI()
      Checks if the current instance is a UI element.
      Returns:
      true if this instance is a UI element, false otherwise.
    • draw

      public void draw(processing.core.PGraphics buffer)