Class TextObject

java.lang.Object
com.iragui.objects.GUIObject
com.iragui.objects.TextObject

public class TextObject extends GUIObject
A GUI object that renders text as a texture and displays it in OpenGL.

This class creates a BufferedImage, rasterizes the text with a given Font, Color, and background, and uploads it as an OpenGL texture. The text can be updated dynamically, and it will automatically regenerate the texture when changed.

Features include:

  • Support for anti-aliased text rendering
  • Support for both RGB and RGBA textures
  • Automatic recalculation of text width when the string changes
  • Ability to revert to the original text
  • Constructor Details

    • TextObject

      public TextObject(String name, int layer, GUI gui, int x, int y, boolean nearestFilter, boolean rgba, String text, Font font, Color color, Color bkgColor, boolean antiAliasing)
      Constructs a new TextObject.
      Parameters:
      name - the name of this object
      layer - the rendering layer
      gui - the parent GUI
      x - the X position
      y - the Y position
      nearestFilter - whether to use nearest-neighbor filtering
      rgba - whether to render as RGBA (true) or RGB (false)
      text - the text string to display
      font - the font used for rendering
      color - the foreground color of the text
      bkgColor - the background color
      antiAliasing - whether to enable text anti-aliasing
  • Method Details

    • setText

      public void setText(String text)
      Updates the text content and regenerates the texture if changed.
      Parameters:
      text - the new text string
    • getTextWidth

      public int getTextWidth()
      Returns:
      the width of the current text in pixels
    • getTextWidth

      public int getTextWidth(String text)
      Returns the width of a specific string in pixels.
      Parameters:
      text - the string to measure
      Returns:
      the pixel width of the given string
    • setFont

      public void setFont(Font font)
      Sets the font used for rendering text and regenerates the texture.
      Parameters:
      font - the new font
    • createTexture

      public void createTexture()
      Creates or updates the texture with the current text, font, and colors. Handles both RGB and RGBA modes.
    • update

      public void update(boolean showFrame)
      Description copied from class: GUIObject
      Update logic called each frame.
      Specified by:
      update in class GUIObject
    • sendKey

      public void sendKey(int key, int action)
      Description copied from class: GUIObject
      Handles key input events.
      Specified by:
      sendKey in class GUIObject
    • sendMousePos

      public void sendMousePos(long window, double xPos, double yPos)
      Description copied from class: GUIObject
      Handles mouse position updates.
      Specified by:
      sendMousePos in class GUIObject
    • sendMouseButton

      public void sendMouseButton(long window, int button, int action, int mods)
      Description copied from class: GUIObject
      Handles mouse button input.
      Specified by:
      sendMouseButton in class GUIObject
    • sendMouseScroll

      public void sendMouseScroll(long window, double xOffset, double yOffset)
      Description copied from class: GUIObject
      Handles mouse scroll input.
      Specified by:
      sendMouseScroll in class GUIObject
    • getText

      public String getText()
      Returns:
      the current text string
    • isOriginalText

      public boolean isOriginalText()
      Returns:
      true if the text matches the original text
    • getOriginalTextWidth

      public int getOriginalTextWidth()
      Returns:
      the pixel width of the original text
    • revertText

      public void revertText()
      Reverts the text back to its original value.
    • setTextOverride

      public void setTextOverride(String string)
      Sets the text and overrides the original text value as well.
      Parameters:
      string - the new text string
    • setColor

      public void setColor(Color color)
      Sets the text color and regenerates the texture.
      Parameters:
      color - the new color
    • getFont

      public Font getFont()
      Returns:
      the current font used for rendering