public class Sprite extends java.lang.Object implements Animatable, Drawable, Loadable
A Sprite is a static image that can be drawn to a Graphics context. Like other Loadables, Sprites can be manually loaded and unloaded into and out of memory. Loading may take a moment, but while a Sprite is not loaded, it cannot be drawn. A Sprite may also be treated as an Animatable with exactly one frame, namely itself, that has a duration of 0.
A Sprite has a fixed set of Filters, specified upon its creation, that can have an effect on it when applied to it with its draw() method. The Sprite makes this possible by making a copy of its image data with each Filter applied upon being loaded. Thus, the amount of memory that a loaded Sprite occupies is proportional to its number of applicable Filters plus 1.
Filter
,
SpriteSheet
,
Animation
Modifier and Type | Field and Description |
---|---|
static Sprite |
BLANK
A blank Sprite with no appearance.
|
Constructor and Description |
---|
Sprite(Image image,
Filter... filters)
Constructs a Sprite from a
Celick
Image.
|
Sprite(java.lang.String path,
int originX,
int originY,
boolean load,
Filter... filters)
Constructs a Sprite from an image file.
|
Sprite(java.lang.String path,
int originX,
int originY,
Color transColor,
boolean load,
Filter... filters)
Constructs a Sprite from an image file.
|
Sprite(java.lang.String path,
int originX,
int originY,
int transR,
int transG,
int transB,
boolean load,
Filter... filters)
Constructs a Sprite from an image file.
|
Modifier and Type | Method and Description |
---|---|
void |
draw(Graphics g,
int x,
int y)
Draws this Drawable's image to the specified Graphics context.
|
void |
draw(Graphics g,
int x,
int y,
boolean xFlip,
boolean yFlip,
double angle,
double alpha,
Filter filter)
Draws this Drawable's image to the specified Graphics context.
|
void |
draw(Graphics g,
int x,
int y,
double scale,
boolean xFlip,
boolean yFlip,
double alpha,
Filter filter)
Draws this Drawable's image to the specified Graphics context.
|
void |
draw(Graphics g,
int x,
int y,
int left,
int right,
int top,
int bottom)
Draws a rectangular region of this Drawable's image to the specified
Graphics context.
|
void |
draw(Graphics g,
int x,
int y,
int left,
int right,
int top,
int bottom,
boolean xFlip,
boolean yFlip,
double angle,
double alpha,
Filter filter)
Draws a rectangular region of this Drawable's image to the specified
Graphics context.
|
void |
draw(Graphics g,
int x,
int y,
int left,
int right,
int top,
int bottom,
double scale,
boolean xFlip,
boolean yFlip,
double alpha,
Filter filter)
Draws a rectangular region of this Drawable's image to the specified
Graphics context.
|
boolean |
framesAreCompatible(int index1,
int index2)
Returns whether the two frames at the specified indices are compatible
for AnimationInstances' transitioning purposes.
|
Sprite |
getFilteredCopy(Filter filter,
boolean load)
Returns a new Animatable created by applying a Filter to all of the
Sprites of which this Animatable is ultimately composed.
|
java.util.Set<Filter> |
getFilters()
Returns an unmodifiable Set view of the Filters that will have an effect
on this Sprite when applied to it with draw().
|
Animatable |
getFrame(int index)
Returns this Animatable's frame at the specified index.
|
long |
getFrameDuration(int index)
Returns the duration in fracunits of this Animatable's frame at the
specified index.
|
int |
getHeight()
Returns this Sprite's height in pixels.
|
Drawable |
getInstance()
Returns a Drawable instantiation of this Sprite - which is simply this
Sprite itself, since Sprites are already Drawables.
|
int |
getLevel()
Returns how many levels of Animations this Animatable and its frames and
sub-frames comprise.
|
int |
getNumFrames()
Returns how many frames this Animatable has.
|
int |
getOriginX()
Returns the x-coordinate in pixels of this Sprite's origin.
|
int |
getOriginY()
Returns the y-coordinate in pixels of this Sprite's origin.
|
java.util.Set<Sprite> |
getSprites()
Returns an unmodifiable Set view of all of the Sprites that ultimately
compose this Animatable.
|
SpriteSheet |
getSpriteSheet()
Returns the SpriteSheet that this Sprite is part of, or null if it is not
part of one.
|
int |
getWidth()
Returns this Sprite's width in pixels.
|
boolean |
isLoaded()
Returns whether this Loadable is loaded.
|
boolean |
load()
Loads this Sprite if it is not already loaded.
|
boolean |
unload()
Unloads this Sprite if it is currently loaded.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getRenderableRegion
public static final Sprite BLANK
public Sprite(java.lang.String path, int originX, int originY, boolean load, Filter... filters)
path
- The relative path to the image fileoriginX
- The x-coordinate in pixels of the origin on the imageoriginY
- The y-coordinate in pixels of the origin on the imageload
- Whether this Sprite should load upon creationfilters
- The Filters that should have an effect on this
Sprite when applied to it with draw()public Sprite(java.lang.String path, int originX, int originY, Color transColor, boolean load, Filter... filters)
path
- The relative path to the image fileoriginX
- The x-coordinate in pixels of the origin on the imageoriginY
- The y-coordinate in pixels of the origin on the imagetransColor
- This Sprite's transparent color, or null if there
should be noneload
- Whether this Sprite should load upon creationfilters
- The Filters that should have an effect on this
Sprite when applied to it with draw()public Sprite(java.lang.String path, int originX, int originY, int transR, int transG, int transB, boolean load, Filter... filters)
path
- The relative path to the image fileoriginX
- The x-coordinate in pixels of the origin on the imageoriginY
- The y-coordinate in pixels of the origin on the imagetransR
- The red value (0-255) of this Sprite's transparent colortransG
- The green value (0-255) of this Sprite's transparent colortransB
- The blue value (0-255) of this Sprite's transparent colorload
- Whether this Sprite should load upon creationfilters
- The Filters that should have an effect on this
Sprite when applied to it with draw()public Sprite(Image image, Filter... filters)
image
- The Image to create this Sprite fromfilters
- The Filters that should have an effect on this Sprite when
applied to it with draw()public final boolean isLoaded()
Loadable
public final boolean load()
public final boolean unload()
public final int getLevel()
Animatable
getLevel
in interface Animatable
public final int getNumFrames()
Animatable
getNumFrames
in interface Animatable
public final Animatable getFrame(int index)
Animatable
getFrame
in interface Animatable
index
- The index of the frame to be returnedpublic final long getFrameDuration(int index)
Animatable
getFrameDuration
in interface Animatable
index
- The index of the frame whose duration is to be returnedpublic final boolean framesAreCompatible(int index1, int index2)
Animatable
framesAreCompatible
in interface Animatable
index1
- The index of the first frameindex2
- The index of the second framepublic final java.util.Set<Sprite> getSprites()
Animatable
getSprites
in interface Animatable
public final Sprite getFilteredCopy(Filter filter, boolean load)
Animatable
getFilteredCopy
in interface Animatable
filter
- The Filter to apply to this Animatable's Sprites to create
the new Animatableload
- Whether the new Animatable should load its Sprites upon its
creationpublic final Drawable getInstance()
getInstance
in interface Animatable
public final SpriteSheet getSpriteSheet()
public final java.util.Set<Filter> getFilters()
public final int getOriginX()
public final int getOriginY()
public final int getWidth()
public final int getHeight()
public final void draw(Graphics g, int x, int y)
Drawable
public final void draw(Graphics g, int x, int y, boolean xFlip, boolean yFlip, double angle, double alpha, Filter filter)
Drawable
draw
in interface Drawable
g
- The Graphics context to draw the image tox
- The x-coordinate on the Graphics context of the drawn image's
originy
- The y-coordinate on the Graphics context of the drawn image's
originxFlip
- If true, the drawn image is flipped along a vertical line
through its originyFlip
- If true, the drawn image is flipped along a horizontal line
through its originangle
- The angle in degrees by which to rotate the drawn image and
its xFlip and yFlip lines counterclockwise around its originalpha
- The drawn image's alpha (opacity) value from 0 to 1filter
- The Filter to apply to the drawn image, or null if none
should be applied. Not every Filter has an effect on every Drawable.public final void draw(Graphics g, int x, int y, double scale, boolean xFlip, boolean yFlip, double alpha, Filter filter)
Drawable
draw
in interface Drawable
g
- The Graphics context to draw the image tox
- The x-coordinate on the Graphics context of the drawn image's
originy
- The y-coordinate on the Graphics context of the drawn image's
originscale
- The factor by which to scale the drawn image around its
originxFlip
- If true, the drawn image is flipped along a vertical line
through its originyFlip
- If true, the drawn image is flipped along a horizontal line
through its originalpha
- The drawn image's alpha (opacity) value from 0 to 1filter
- The Filter to apply to the drawn image, or null if none
should be applied. Not every Filter has an effect on every Drawable.public final void draw(Graphics g, int x, int y, int left, int right, int top, int bottom)
Drawable
draw
in interface Drawable
g
- The Graphics context to draw the region tox
- The x-coordinate on the Graphics context of the image's originy
- The y-coordinate on the Graphics context of the image's originleft
- The x-coordinate on the image, relative to its origin, of the
region's left edgeright
- The x-coordinate on the image, relative to its origin, of
the region's right edgetop
- The y-coordinate on the image, relative to its origin, of the
region's top edgebottom
- The y-coordinate on the image, relative to its origin, of
the region's bottom edgepublic final void draw(Graphics g, int x, int y, int left, int right, int top, int bottom, boolean xFlip, boolean yFlip, double angle, double alpha, Filter filter)
Drawable
draw
in interface Drawable
g
- The Graphics context to draw the region tox
- The x-coordinate on the Graphics context of the image's originy
- The y-coordinate on the Graphics context of the image's originleft
- The x-coordinate on the image, relative to its origin, of the
region's left edgeright
- The x-coordinate on the image, relative to its origin, of
the region's right edgetop
- The y-coordinate on the image, relative to its origin, of the
region's top edgebottom
- The y-coordinate on the image, relative to its origin, of
the region's bottom edgexFlip
- If true, the drawn region is flipped along a vertical line
through the image's originyFlip
- If true, the drawn region is flipped along a horizontal line
through the image's originangle
- The angle in degrees by which to rotate the drawn region and
its xFlip and yFlip lines counterclockwise around the image's originalpha
- The drawn region's alpha (opacity) value from 0 to 1filter
- The Filter to apply to the drawn region, or null if none
should be applied. Not every Filter has an effect on every Drawable.public final void draw(Graphics g, int x, int y, int left, int right, int top, int bottom, double scale, boolean xFlip, boolean yFlip, double alpha, Filter filter)
Drawable
draw
in interface Drawable
g
- The Graphics context to draw the region tox
- The x-coordinate on the Graphics context of the image's originy
- The y-coordinate on the Graphics context of the image's originleft
- The x-coordinate on the image, relative to its origin, of the
region's left edgeright
- The x-coordinate on the image, relative to its origin, of
the region's right edgetop
- The y-coordinate on the image, relative to its origin, of the
region's top edgebottom
- The y-coordinate on the image, relative to its origin, of
the region's bottom edgescale
- The factor by which to scale the drawn region around the
image's originxFlip
- If true, the drawn region is flipped along a vertical line
through the image's originyFlip
- If true, the drawn region is flipped along a horizontal line
through the image's originalpha
- The drawn region's alpha (opacity) value from 0 to 1filter
- The Filter to apply to the drawn region, or null if none
should be applied. Not every Filter has an effect on every Drawable.