Class Spritesheet

All Implemented Interfaces:
Comparable<Spritesheet>

public final class Spritesheet extends Object implements Comparable<Spritesheet>
Represents a single sprite sheet image carved up into individual sprite frames of equal width and height. Sprite frames are lazily cached on first access and can be retrieved by index.
  • Constructor Details

    • Spritesheet

      public Spritesheet(BufferedImage image, String path, int spriteWidth, int spriteHeight)
      Instantiates a new Spritesheet instance. Depending on the given spriteWidth and spriteHeight, the sub-images will be cropped from the spritesheet image when accessing individual sprites.
      Parameters:
      image - the spritesheet image
      path - the path (or name) of the spritesheet image
      spriteWidth - the width in pixels of each sprite in the spritesheet.
      spriteHeight - the height in pixels of each sprite in the spritesheet.
  • Method Details

    • compareTo

      public int compareTo(Spritesheet obj)
      Specified by:
      compareTo in interface Comparable<Spritesheet>
    • getColumns

      public int getColumns()
      Gets the sprites per row.
      Returns:
      the sprites per row
    • getPreview

      public BufferedImage getPreview(int dimension)
      Returns a square preview image of the first sprite, scaled to the requested dimension. The scaled image is cached in the global image resource registry so subsequent calls with the same dimension are cheap.
      Parameters:
      dimension - the side length of the preview image, in pixels
      Returns:
      the preview image
    • getImage

      public BufferedImage getImage()
      Gets the full underlying spritesheet image.
      Returns:
      the spritesheet image
    • getImageFormat

      public ImageFormat getImageFormat()
      Gets the image format the spritesheet was loaded from.
      Returns:
      the image format
    • getName

      public String getName()
      The unique name of this spritesheet. A spritesheet can always be identified by this name within a game project.
      Returns:
      The name of the spritesheet.
    • getRows

      public int getRows()
      Gets the number of sprite rows in this spritesheet.
      Returns:
      the row count
    • getRandomSprite

      public BufferedImage getRandomSprite()
      Returns a randomly chosen sprite from the cached sprite array.
      Returns:
      a random sprite, or null if no sprites are cached
    • getSprite

      public BufferedImage getSprite(int index)
      Returns the sprite at the supplied index.
      Parameters:
      index - the sprite index
      Returns:
      the sprite image, or null if the index is invalid or the sprite is empty
    • getSprite

      public BufferedImage getSprite(int index, int margin, int spacing)
      Returns the sprite at the supplied index, accounting for the supplied margin/spacing.
      Parameters:
      index - the sprite index
      margin - the outer margin of the spritesheet image, in pixels
      spacing - the spacing between adjacent sprites, in pixels
      Returns:
      the sprite image, or null if the index is invalid, the sprite is empty, or the sub-image could not be extracted
    • getSpriteHeight

      public int getSpriteHeight()
      Gets the sprite height.
      Returns:
      the sprite height
    • getSpriteWidth

      public int getSpriteWidth()
      Gets the sprite width.
      Returns:
      the sprite width
    • getTotalNumberOfSprites

      public int getTotalNumberOfSprites()
      Gets the total sprites.
      Returns:
      the total sprites
    • isLoaded

      public boolean isLoaded()
      Returns whether this spritesheet is currently registered with the global spritesheet resources.
      Returns:
      true if loaded
    • setSpriteHeight

      public void setSpriteHeight(int spriteHeight)
      Sets the sprite height (in pixels) and rebuilds the row/column counts.
      Parameters:
      spriteHeight - the new sprite height
    • setSpriteWidth

      public void setSpriteWidth(int spriteWidth)
      Sets the sprite width (in pixels) and rebuilds the row/column counts.
      Parameters:
      spriteWidth - the new sprite width