java.lang.Object
org.openpatch.scratch.extensions.text.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);
 
  • 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.
    Copy constructor for the Text class.
    Constructs a new Text object associated with a given Sprite.
  • 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)
     
    int
    Returns the index of the current font.
    Retrieves the name of the current font.
    Retrieves the name of the font.
    Retrieves the current position as a Vector2 object.
    Retrieves the current stage associated with this object.
    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.
    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
    setAlign(int align)
    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.
    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

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 alignment is set to center.
    • 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

    • 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
    • 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
    • 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(int align)
      Sets the alignment of the text.
      Parameters:
      align - the alignment value to set. This could be a constant representing left, center, or right 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)