Class ImageComponent

java.lang.Object
de.gurkenlabs.litiengine.gui.GuiComponent
de.gurkenlabs.litiengine.gui.ImageComponent
All Implemented Interfaces:
IRenderable, Tweenable, MouseListener, MouseMotionListener, MouseWheelListener, EventListener
Direct Known Subclasses:
CheckBox, MouseDrawComponent, TextFieldComponent

public class ImageComponent extends GuiComponent
The ImageComponent class extends the GuiComponent class to provide functionality for handling images and spritesheets within a GUI component. It supports various image scaling modes, alignment, and a caching mechanism to retain the scaled images in memory.
  • Field Details

  • Constructor Details

    • ImageComponent

      public ImageComponent(double x, double y, Image image)
      Constructs an ImageComponent with the specified position and image.
      Parameters:
      x - The x-coordinate of the component.
      y - The y-coordinate of the component.
      image - The image to be displayed by the component.
    • ImageComponent

      public ImageComponent(double x, double y, double width, double height)
      Constructs an ImageComponent with the specified position and dimensions.
      Parameters:
      x - The x-coordinate of the component.
      y - The y-coordinate of the component.
      width - The width of the component.
      height - The height of the component.
    • ImageComponent

      public ImageComponent(double x, double y, double width, double height, String text)
      Constructs an ImageComponent with the specified position, dimensions, and text.
      Parameters:
      x - The x-coordinate of the component.
      y - The y-coordinate of the component.
      width - The width of the component.
      height - The height of the component.
      text - The text to be displayed by the component.
    • ImageComponent

      public ImageComponent(double x, double y, double width, double height, Image image)
      Constructs an ImageComponent with the specified position, dimensions, and image.
      Parameters:
      x - The x-coordinate of the component.
      y - The y-coordinate of the component.
      width - The width of the component.
      height - The height of the component.
      image - The image to be displayed by the component.
    • ImageComponent

      public ImageComponent(double x, double y, double width, double height, Spritesheet spritesheet, String text, Image image)
      Constructs an ImageComponent with the specified position, dimensions, spritesheet, text, and image.
      Parameters:
      x - The x-coordinate of the component.
      y - The y-coordinate of the component.
      width - The width of the component.
      height - The height of the component.
      spritesheet - The spritesheet to be used by the component.
      text - The text to be displayed by the component.
      image - The image to be displayed by the component.
  • Method Details

    • getBackground

      public Image getBackground()
      Retrieves the background image for the component based on its current state. The image is fetched from a cache if available, otherwise it is generated and added to the cache.
      Returns:
      The background image, or null if no spritesheet is set.
    • rescaleImage

      public void rescaleImage()
      Rescales the base image according to the component's dimensions and the specified image scale mode. The rescaled image is cached to improve performance on subsequent calls.
    • getImage

      public BufferedImage getImage()
      Retrieves the image to be displayed by the component. If a scaled image is available, it returns the scaled image; otherwise, it returns the base image.
      Returns:
      The image to be displayed by the component.
    • getImageAlign

      public Align getImageAlign()
      Gets the horizontal alignment of the image within the component.
      Returns:
      The horizontal alignment of the image.
    • getImageScaleMode

      public ImageScaleMode getImageScaleMode()
      Gets the scale mode for the image.
      Returns:
      The scale mode for the image.
    • getSpritesheetScaleMode

      public ImageScaleMode getSpritesheetScaleMode()
      Gets the scale mode for the spritesheet.
      Returns:
      The scale mode for the spritesheet.
    • getSpritesheetScaleFactor

      public float getSpritesheetScaleFactor()
      Gets the scale factor for the spritesheet.
      Returns:
      The scale factor for the spritesheet.
    • getHorizontalImagePadding

      public double getHorizontalImagePadding()
      Gets the horizontal padding for the image.
      Returns:
      The horizontal padding for the image.
    • setHorizontalImagePadding

      public void setHorizontalImagePadding(double horizontalImagePadding)
      Sets the horizontal padding for the image.
      Parameters:
      horizontalImagePadding - The horizontal padding to set.
    • getVerticalImagePadding

      public double getVerticalImagePadding()
      Gets the vertical padding for the image.
      Returns:
      The vertical padding for the image.
    • setVerticalImagePadding

      public void setVerticalImagePadding(double verticalImagePadding)
      Sets the vertical padding for the image.
      Parameters:
      verticalImagePadding - The vertical padding to set.
    • getImageValign

      public Valign getImageValign()
      Gets the vertical alignment of the image within the component.
      Returns:
      The vertical alignment of the image.
    • render

      public void render(Graphics2D g)
      Renders the component using the provided Graphics2D context. If the component is suspended or not visible, the method returns immediately. Otherwise, it renders the background image (if available) and the main image (if available), and then calls the superclass's render method.
      Specified by:
      render in interface IRenderable
      Overrides:
      render in class GuiComponent
      Parameters:
      g - The Graphics2D context used for rendering.
      See Also:
    • setImage

      public void setImage(Image image)
      Sets the image to be displayed by the component and rescales it.
      Parameters:
      image - The image to be displayed by the component.
    • setImageScaleMode

      public void setImageScaleMode(ImageScaleMode imageScaleMode)
      Sets the scale mode for the image and rescales it.
      Parameters:
      imageScaleMode - The scale mode for the image.
    • getImageScaleInterpolation

      public int getImageScaleInterpolation()
      Gets the interpolation type used for scaling the image. The interpolation type is one of the AffineTransformOp integer constants: - AffineTransformOp.TYPE_NEAREST_NEIGHBOR - AffineTransformOp.TYPE_BILINEAR - AffineTransformOp.TYPE_BICUBIC
      Returns:
      The interpolation type used for scaling the image.
    • setImageScaleInterpolation

      public void setImageScaleInterpolation(int imageScaleInterpolation)
      Sets the interpolation type used for scaling the image and rescales it. The interpolation type should be one of the AffineTransformOp integer constants: - AffineTransformOp.TYPE_NEAREST_NEIGHBOR - AffineTransformOp.TYPE_BILINEAR - AffineTransformOp.TYPE_BICUBIC
      Parameters:
      imageScaleInterpolation - The interpolation type used for scaling the image.
    • setSpritesheetScaleMode

      public void setSpritesheetScaleMode(ImageScaleMode spritesheetScaleMode)
      Sets the scale mode for the spritesheet and rescales the image.
      Parameters:
      spritesheetScaleMode - The scale mode for the spritesheet.
    • setSpritesheetScaleFactor

      public void setSpritesheetScaleFactor(float spritesheetScaleFactor)
      Sets the scale factor for the spritesheet.
      Parameters:
      spritesheetScaleFactor - The scale factor for the spritesheet.
    • getSpritesheetScaleInterpolation

      public int getSpritesheetScaleInterpolation()
      Gets the interpolation type used for scaling the spritesheet. The interpolation type should be one of the AffineTransformOp interpolation constants:
      Returns:
      The interpolation type used for scaling the spritesheet.
    • setSpritesheetScaleInterpolation

      public void setSpritesheetScaleInterpolation(int spritesheetScaleInterpolation)
      Sets the interpolation type used for scaling the spritesheet and rescales the image. The interpolation type should be one of the AffineTransformOp interpolation constants:
      Parameters:
      spritesheetScaleInterpolation - The interpolation type used for scaling the spritesheet.
    • setSpritesheet

      public void setSpritesheet(Spritesheet spr)
      Sets the spritesheet to be used by the component.
      Parameters:
      spr - The spritesheet to be used by the component.
    • setSpritesheet

      public void setSpritesheet(Spritesheet spr, ImageScaleMode scaleMode)
      Sets the spritesheet and its scale mode to be used by the component.
      Parameters:
      spr - The spritesheet to be used by the component.
      scaleMode - The scale mode for the spritesheet.
    • setSpritesheet

      public void setSpritesheet(Spritesheet spr, ImageScaleMode scaleMode, float scaleFactor)
      Sets the spritesheet, its scale mode, and scale factor to be used by the component.
      Parameters:
      spr - The spritesheet to be used by the component.
      scaleMode - The scale mode for the spritesheet.
      scaleFactor - The scale factor for the spritesheet.
    • setImageAlign

      public void setImageAlign(Align imageAlign)
      Sets the horizontal alignment of the image within the component.
      Parameters:
      imageAlign - The horizontal alignment of the image.
    • setImageValign

      public void setImageValign(Valign imageValign)
      Sets the vertical alignment of the image within the component.
      Parameters:
      imageValign - The vertical alignment of the image.
    • setHeight

      public void setHeight(double height)
      Sets the height of the component and rescales the image.
      Overrides:
      setHeight in class GuiComponent
      Parameters:
      height - The new height of the component.
    • setWidth

      public void setWidth(double width)
      Sets the width of the component and rescales the image.
      Overrides:
      setWidth in class GuiComponent
      Parameters:
      width - The new width of the component.
    • getSpritesheet

      protected Spritesheet getSpritesheet()
      Retrieves the spritesheet used by the component.
      Returns:
      The spritesheet used by the component.