Class AnimatedTextRenderer
java.lang.Object
ca.corbett.extras.image.animation.AnimatedTextRenderer
Animates a text display, such that a given text string will be slowly "printed" out character
by character with configurable styling and with a configurable speed.
- Since:
- swing-extras 2.3
- Author:
- scorbo2 (with help from Claude!)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classHelper class to store cursor position -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Colorprotected final BufferedImageprotected final Graphics2Dprotected doubleprotected doubleprotected intprotected doubleprotected doubleprotected Colorstatic final Colorstatic final Fontstatic final Colorprotected Fontprotected longprotected booleanprotected intprotected booleanprotected Colorprotected Stringprotected int -
Constructor Summary
ConstructorsConstructorDescriptionAnimatedTextRenderer(int width, int height, String text) Creates a new AnimatedTextRenderer with the given dimensions and the given text string.AnimatedTextRenderer(int width, int height, String text, double charsPerSecond) Creates a new AnimatedTextRenderer with the given dimensions, text string, and typing speed.AnimatedTextRenderer(int width, int height, String text, double charsPerSecond, Font font, Color textColor, Color backgroundColor) Creates a new AnimatedTextRenderer with the given dimensions, text string, typing speed, and styling options. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidvoiddispose()Cleans up resourcesprotected voidDraws the block cursor at the specified positionReturns the current buffer imagedoubleGets current progress as a percentage (0.0 to 1.0)booleanChecks if animation is completeprotected voidReflows the text to fit within the buffer boundsprotected voidrenderUpToCharacter(int charIndex) Renders text up to the specified character indexvoidResets animation to beginningvoidsetBackgroundColor(Color backgroundColor) Changes background colorvoidsetCharsPerSecond(double charsPerSecond) Changes the animation speedvoidsetCursorBlinkRate(double blinksPerSecond) Sets cursor blink rate (blinks per second)voidsetCursorColor(Color cursorColor) Sets cursor color independently of text colorvoidChanges the font (triggers reflow)voidsetPadding(int padding) Sets padding around textvoidChanges the text to animate (restarts animation)voidsetTextColor(Color textColor) Changes text colorprotected voidvoidImmediately shows all text (skips animation)voidUpdates the animation state and renders new characters if needed.wrapText(String text, FontRenderContext frc, Font font, int maxWidth) Wraps text to fit within the specified width
-
Field Details
-
DEFAULT_FONT
-
DEFAULT_TEXT_COLOR
-
DEFAULT_BACKGROUND_COLOR
-
buffer
-
bufferGraphics
-
textToRender
-
wrappedLines
-
totalChars
protected int totalChars -
currentCharIndex
protected int currentCharIndex -
charsPerSecond
protected double charsPerSecond -
lastUpdateTime
protected long lastUpdateTime -
charAccumulator
protected double charAccumulator -
showCursor
protected boolean showCursor -
cursorBlinkRate
protected double cursorBlinkRate -
cursorAccumulator
protected double cursorAccumulator -
font
-
textColor
-
backgroundColor
-
cursorColor
-
padding
protected int padding -
needsReflow
protected boolean needsReflow
-
-
Constructor Details
-
AnimatedTextRenderer
Creates a new AnimatedTextRenderer with the given dimensions and the given text string. All styling options and speed will use built-in default values. -
AnimatedTextRenderer
Creates a new AnimatedTextRenderer with the given dimensions, text string, and typing speed. All styling options will use built-in default values. -
AnimatedTextRenderer
public AnimatedTextRenderer(int width, int height, String text, double charsPerSecond, Font font, Color textColor, Color backgroundColor) Creates a new AnimatedTextRenderer with the given dimensions, text string, typing speed, and styling options.
-
-
Method Details
-
setupGraphics
protected void setupGraphics() -
clearBuffer
protected void clearBuffer() -
updateTextAnimation
public void updateTextAnimation()Updates the animation state and renders new characters if needed. Call this once per frame from your animation loop. This method checks how long it's been since the last time it was invoked, and will output the appropriate number of characters using the currently configured charsPerSecond. -
reflowText
protected void reflowText()Reflows the text to fit within the buffer bounds -
renderUpToCharacter
protected void renderUpToCharacter(int charIndex) Renders text up to the specified character index -
drawCursor
Draws the block cursor at the specified position -
wrapText
Wraps text to fit within the specified width -
getBuffer
Returns the current buffer image -
setText
Changes the text to animate (restarts animation) -
setCharsPerSecond
public void setCharsPerSecond(double charsPerSecond) Changes the animation speed -
setFont
Changes the font (triggers reflow) -
setTextColor
Changes text color -
setCursorColor
Sets cursor color independently of text color -
setCursorBlinkRate
public void setCursorBlinkRate(double blinksPerSecond) Sets cursor blink rate (blinks per second) -
setBackgroundColor
Changes background color -
setPadding
public void setPadding(int padding) Sets padding around text -
isAnimationComplete
public boolean isAnimationComplete()Checks if animation is complete -
showAllText
public void showAllText()Immediately shows all text (skips animation) -
resetAnimation
public void resetAnimation()Resets animation to beginning -
getProgress
public double getProgress()Gets current progress as a percentage (0.0 to 1.0) -
dispose
public void dispose()Cleans up resources
-