Package org.openpatch.scratch
Class Text
java.lang.Object
org.openpatch.scratch.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);
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intSPEAK_BUBBLE_MAX_LIMIT is the maximum width of the speech bubble.static intSPEAK_BUBBLE_MIN_LIMIT is the minimum width of the speech bubble. -
Constructor Summary
ConstructorsConstructorDescriptionText()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.Constructs a new Text object associated with a given Sprite.Copy constructor for the Text class. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddedToStage(Stage stage) voidAdds a new font to the list of fonts if it does not already exist.voiddraw(processing.core.PGraphics buffer) getAlign()Returns where the text sits relative to its position.intReturns the index of the current font.Retrieves the name of the current font.static StringReturns the font that text is written in by default.static intReturns the size text is written in by default.static int[]Returns the sizes prepared in advance.getFont()Retrieves the name of the font.Retrieves the current position as a Vector2 object.getStage()Retrieves the current stage associated with this object.getStyle()Returns the style the text is drawn in.intReturns the size of the text.doublegetWidth()Returns the width of the text.doublegetX()Returns the x-coordinate of this object.doublegetY()Returns the y-coordinate of this object.voidgoLayersBackwards(int number) Moves the text backwards by a specified number of layers in the stage.voidgoLayersForwards(int number) Moves the text forward by a specified number of layers within its stage.voidMoves this text to the back layer of the stage, behind every other text on it.voidMoves this text to the front layer of the stage, in front of every other text on it.static booleanReturns whether letters are smoothed.booleanisUI()Checks if the current instance is a UI element.voidnextFont()Advances to the next font in the list of available fonts.voidremove()Removes this object from its current stage if it is associated with one.voidremovedFromStage(Stage stage) voidSets the alignment of the text.voidsetBackgroundColor(double h) Sets the background color using the specified hue value.voidsetBackgroundColor(int r, int g, int b) Sets the background color using the specified RGB values.voidSets the background color of the text.voidSets the font name for the text.voidsetIsUI(boolean isUI) Sets the UI status of this object.voidsetPosition(double x, double y) Sets the position of the textvoidSets the position of the text object using a Vector2 object.voidsetStrokeColor(double h) Sets the stroke color using the specified hue value.voidsetStrokeColor(int r, int g, int b) Sets the stroke color using the specified RGB values.voidSets the stroke color for the text.voidSets the style of the text.voidsetTextColor(double h) Sets the text color using the specified hue value.voidsetTextColor(int r, int g, int b) Sets the text color using RGB values.voidSets the color of the text.voidsetTextSize(int size) Sets the size of the text.voidsetWidth(double width) Sets the width of the text.voidsetX(double x) Sets the x-coordinate for this object.voidsetY(double y) Sets the Y coordinate of the text.voidDisplays the specified text.voidDisplays the specified text for a given duration.voidswitchFont(String name) Switches the current font to the font with the specified name.static voidChooses the font all text is written in, keeping the current size.static voidChooses the font all text is written in, and how big it is.static voiduseFontSizes(int... sizes) Prepares several sizes of the font in advance.static voiduseSmoothing(boolean smooth) Chooses whether letters are smoothed.voidThis method is called when the object is added to the stage.voidwhenAddedToStage(Stage stage) This method is called when the text is added to the stage.voidThis method is called when the object is removed from the stage.voidwhenRemovedFromStage(Stage stage) This method is called when the text is removed from the stage.
-
Field Details
-
SPEAK_BUBBLE_MAX_LIMIT
public static int SPEAK_BUBBLE_MAX_LIMITSPEAK_BUBBLE_MAX_LIMIT is the maximum width of the speech bubble. -
SPEAK_BUBBLE_MIN_LIMIT
public static int SPEAK_BUBBLE_MIN_LIMITSPEAK_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. CallsetAlign(TextAlign)for anything else. -
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
-
useFont
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 filesize- the size to write in
-
useFont
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
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
-
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
-
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
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
nullif 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
-
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
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
Sets the alignment of the text.- Parameters:
align- where the text should sit relative to its position
-
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:
trueif this instance is a UI element,falseotherwise.
-
draw
public void draw(processing.core.PGraphics buffer)
-