Class AnimatedTextRenderer

java.lang.Object
ca.corbett.extras.image.animation.AnimatedTextRenderer

public class AnimatedTextRenderer extends Object
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!)
  • Field Details

    • DEFAULT_FONT

      public static final Font DEFAULT_FONT
    • DEFAULT_TEXT_COLOR

      public static final Color DEFAULT_TEXT_COLOR
    • DEFAULT_BACKGROUND_COLOR

      public static final Color DEFAULT_BACKGROUND_COLOR
    • buffer

      protected final BufferedImage buffer
    • bufferGraphics

      protected final Graphics2D bufferGraphics
    • textToRender

      protected String textToRender
    • wrappedLines

      protected List<String> 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

      protected Font font
    • textColor

      protected Color textColor
    • backgroundColor

      protected Color backgroundColor
    • cursorColor

      protected Color cursorColor
    • padding

      protected int padding
    • needsReflow

      protected boolean needsReflow
  • Constructor Details

    • AnimatedTextRenderer

      public AnimatedTextRenderer(int width, int height, String text)
      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

      public AnimatedTextRenderer(int width, int height, String text, double charsPerSecond)
      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

      protected void drawCursor(AnimatedTextRenderer.CursorPosition pos)
      Draws the block cursor at the specified position
    • wrapText

      protected List<String> wrapText(String text, FontRenderContext frc, Font font, int maxWidth)
      Wraps text to fit within the specified width
    • getBuffer

      public BufferedImage getBuffer()
      Returns the current buffer image
    • setText

      public void setText(String text)
      Changes the text to animate (restarts animation)
    • setCharsPerSecond

      public void setCharsPerSecond(double charsPerSecond)
      Changes the animation speed
    • setFont

      public void setFont(Font font)
      Changes the font (triggers reflow)
    • setTextColor

      public void setTextColor(Color textColor)
      Changes text color
    • setCursorColor

      public void setCursorColor(Color cursorColor)
      Sets cursor color independently of text color
    • setCursorBlinkRate

      public void setCursorBlinkRate(double blinksPerSecond)
      Sets cursor blink rate (blinks per second)
    • setBackgroundColor

      public void setBackgroundColor(Color backgroundColor)
      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