Record Class SpriteSheet

java.lang.Object
java.lang.Record
cit244.api.image.SpriteSheet
Record Components:
image - source image
tileW - width of each cell in pixels
tileH - height of each cell in pixels
margin - pixels before the first row and column
spacing - pixels between adjacent cells

public record SpriteSheet(Image image, int tileW, int tileH, int margin, int spacing) extends Record
A regular grid of sprite regions within an image.

Columns and rows are zero-based, starting at the top-left cell. Margins are measured from the image edges, and spacing is the number of pixels between adjacent cells. For example:


 SpriteSheet sheet = image.grid(16, 16).margin(1).spacing(1);
 Sprite pose = sheet.at(2, 0);  // third cell in the first row
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    SpriteSheet(Image image, int tileSize)
    Creates a grid of square cells without margins or spacing.
    SpriteSheet(Image image, int tileW, int tileH)
    Creates a grid without margins or spacing.
    SpriteSheet(Image image, int tileW, int tileH, int margin, int spacing)
    Checks the dimensions, margin, and spacing.
  • Method Summary

    Modifier and Type
    Method
    Description
    at(int col, int row)
    Selects a cell by column and row, starting from (0, 0).
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns the value of the image record component.
    int
    Returns the value of the margin record component.
    margin(int pixels)
    Returns a sheet with a different margin.
    int
    Returns the value of the spacing record component.
    spacing(int pixels)
    Returns a sheet with different spacing between cells.
    int
    Returns the value of the tileH record component.
    int
    Returns the value of the tileW record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SpriteSheet

      public SpriteSheet(Image image, int tileW, int tileH)
      Creates a grid without margins or spacing.
      Parameters:
      image - source image
      tileW - cell width in pixels
      tileH - cell height in pixels
    • SpriteSheet

      public SpriteSheet(Image image, int tileSize)
      Creates a grid of square cells without margins or spacing.
      Parameters:
      image - source image
      tileSize - cell side length in pixels
    • SpriteSheet

      public SpriteSheet(Image image, int tileW, int tileH, int margin, int spacing)
      Checks the dimensions, margin, and spacing.
      Throws:
      NullPointerException - if image is null
      IllegalArgumentException - if cell dimensions are not positive, or margin or spacing is negative
  • Method Details

    • margin

      public SpriteSheet margin(int pixels)
      Returns a sheet with a different margin.
      Parameters:
      pixels - margin before the first row and column (nonnegative)
      Returns:
      the updated sheet
    • spacing

      public SpriteSheet spacing(int pixels)
      Returns a sheet with different spacing between cells.
      Parameters:
      pixels - space between adjacent cells (nonnegative)
      Returns:
      the updated sheet
    • at

      public Sprite at(int col, int row)
      Selects a cell by column and row, starting from (0, 0).
      Parameters:
      col - zero-based column
      row - zero-based row
      Returns:
      sprite for the selected cell
      Throws:
      IllegalArgumentException - if the cell is outside the image
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • image

      public Image image()
      Returns the value of the image record component.
      Returns:
      the value of the image record component
    • tileW

      public int tileW()
      Returns the value of the tileW record component.
      Returns:
      the value of the tileW record component
    • tileH

      public int tileH()
      Returns the value of the tileH record component.
      Returns:
      the value of the tileH record component
    • margin

      public int margin()
      Returns the value of the margin record component.
      Returns:
      the value of the margin record component
    • spacing

      public int spacing()
      Returns the value of the spacing record component.
      Returns:
      the value of the spacing record component