Class Text
java.lang.Object
org.openpatch.scratch.extensions.text.Text
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
ConstructorDescriptionText()
Constructs a new Text object with default values.Constructs a new Text object with the specified text, position, and width.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 TypeMethodDescriptionvoid
addedToStage
(Stage stage) void
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.getFont()
Retrieves the name of the font.Retrieves the current position as a Vector2 object.getStage()
Retrieves the current stage associated with this object.int
Returns the size of the text.double
getWidth()
Returns the width of the text.double
getX()
Returns the x-coordinate of this object.double
getY()
Returns the y-coordinate of this object.boolean
isUI()
Checks if the current instance is a UI element.void
nextFont()
Advances to the next font in the list of available fonts.void
remove()
Removes this object from its current stage if it is associated with one.void
removedFromStage
(Stage stage) void
setAlign
(int align) Sets the alignment of the text.void
setBackgroundColor
(double h) 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 textvoid
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
Displays the specified text for a given duration.void
switchFont
(String name) 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
whenAddedToStage
(Stage stage) 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
whenRemovedFromStage
(Stage stage) This method is called when the text is removed from the stage.
-
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
Constructs a new Text object associated with a given Sprite.- Parameters:
s
- the Sprite to associate with this Text object
-
Text
Constructs a new Text object with the specified text, position, and width.- Parameters:
text
- the text to be displayedx
- the x-coordinate of the text's positiony
- the y-coordinate of the text's positionwidth
- the width of the text
-
Text
Constructs a new Text object with the specified text, position, width, and style.- Parameters:
text
- the text content to be displayedx
- the x-coordinate of the text's positiony
- the y-coordinate of the text's positionwidth
- the width of the text areastyle
- the style to be applied to the text
-
Text
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
-
removedFromStage
-
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
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
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
Retrieves the current stage associated with this object.- Returns:
- the current stage
-
addFont
Adds a new font to the list of fonts if it does not already exist.- Parameters:
name
- the name of the font to be addedpath
- the path to the font file
-
switchFont
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
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 coordinatey
- a y coordinate
-
setPosition
Sets the position of the text object using a Vector2 object.- Parameters:
v
- the Vector2 object containing the x and y coordinates
-
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
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
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
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
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
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
Sets the stroke color for the text.- Parameters:
c
- the color to set as the stroke color
-
setFont
Sets the font name for the text.- Parameters:
name
- the name of the font to be set
-
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)
-