Class Spritesheet
- All Implemented Interfaces:
Comparable<Spritesheet>
-
Constructor Summary
ConstructorsConstructorDescriptionSpritesheet(BufferedImage image, String path, int spriteWidth, int spriteHeight) Instantiates a newSpritesheetinstance. -
Method Summary
Modifier and TypeMethodDescriptionintcompareTo(Spritesheet obj) intGets the sprites per row.getImage()Gets the full underlying spritesheet image.Gets the image format the spritesheet was loaded from.getName()The unique name of this spritesheet.getPreview(int dimension) Returns a square preview image of the first sprite, scaled to the requested dimension.Returns a randomly chosen sprite from the cached sprite array.intgetRows()Gets the number of sprite rows in this spritesheet.getSprite(int index) Returns the sprite at the supplied index.getSprite(int index, int margin, int spacing) Returns the sprite at the supplied index, accounting for the supplied margin/spacing.intGets the sprite height.intGets the sprite width.intGets the total sprites.booleanisLoaded()Returns whether this spritesheet is currently registered with the global spritesheet resources.voidsetSpriteHeight(int spriteHeight) Sets the sprite height (in pixels) and rebuilds the row/column counts.voidsetSpriteWidth(int spriteWidth) Sets the sprite width (in pixels) and rebuilds the row/column counts.
-
Constructor Details
-
Spritesheet
Instantiates a newSpritesheetinstance. Depending on the givenspriteWidthandspriteHeight, the sub-images will be cropped from the spritesheet image when accessing individual sprites.- Parameters:
image- the spritesheet imagepath- the path (or name) of the spritesheet imagespriteWidth- the width in pixels of each sprite in the spritesheet.spriteHeight- the height in pixels of each sprite in the spritesheet.
-
-
Method Details
-
compareTo
- Specified by:
compareToin interfaceComparable<Spritesheet>
-
getColumns
public int getColumns()Gets the sprites per row.- Returns:
- the sprites per row
-
getPreview
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
Gets the full underlying spritesheet image.- Returns:
- the spritesheet image
-
getImageFormat
Gets the image format the spritesheet was loaded from.- Returns:
- the image format
-
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
Returns a randomly chosen sprite from the cached sprite array.- Returns:
- a random sprite, or
nullif no sprites are cached
-
getSprite
Returns the sprite at the supplied index.- Parameters:
index- the sprite index- Returns:
- the sprite image, or
nullif the index is invalid or the sprite is empty
-
getSprite
Returns the sprite at the supplied index, accounting for the supplied margin/spacing.- Parameters:
index- the sprite indexmargin- the outer margin of the spritesheet image, in pixelsspacing- the spacing between adjacent sprites, in pixels- Returns:
- the sprite image, or
nullif 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:
trueif 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
-