Class Sprite

java.lang.Object
  extended by Sprite

public class Sprite
extends Object

A class to represent a software sprite. A Sprite has a position, velocity (in terms of steps), an image, and can be deactivated. The sprite's image is managed with an ImagesLoader object, and an ImagesPlayer object for looping. The images stored until the image 'name' can be looped through by calling loopImage(), which uses an ImagesPlayer object.


Constructor Summary
Sprite(int x, int y, int panelWidth, int panelHeight, ImagesLoader imsLd, String name)
          Sprite constructor.
 
Method Summary
 void drawSprite(Graphics g)
          Draw this Sprite at its current location if it is active.
 int getHeight()
          The height of the Sprite's image.
 BufferedImage getImage()
          Get this Sprite's image.
 int getPHeight()
          The height of the Sprite's enclosing panel.
 int getPWidth()
          The width of the Sprite's enclosing panel.
 Rectangle getRectangle()
          Get the bounding Rectangle for this Sprite.
 int getWidth()
          The width of the Sprite's image.
 int getXPosn()
          Get x location.
 int getXStep()
          Get x velocity.
 int getYPosn()
          Get y location.
 int getYStep()
          Get y velocity.
 boolean isActive()
          Check if this Sprite is active.
 void loopImage(int animPeriod, double seqDuration)
          Switch on loop playing.
 void setActive(boolean a)
          Set this Sprite's active status.
 void setImage(String name)
          Change the image for this Sprite.
 void setPosition(int x, int y)
          Set the location of this Sprite.
 void setStep(int dx, int dy)
          Change this Sprite's x and y velocity.
 void stopLooping()
          If looping, stop.
 void translate(int xDist, int yDist)
          Change the location of this Sprite.
 void updateSprite()
          If this Sprite is active the x velocity and y velocity are applied to the x location and y location.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Sprite

public Sprite(int x,
              int y,
              int panelWidth,
              int panelHeight,
              ImagesLoader imsLd,
              String name)
Sprite constructor. width and height of sprite are set equal to width and height of image for sprite. If no image is found default values are used.

Parameters:
x - x location for upper left corner of sprite
y - y loaction for upper left corner of sprite
panelWidth - width of panel that contaisn sprite
panelHeight - height of panel that contains sprite
imsLd - object to load image file for sprite
name - name of file that contains sprite image
Method Detail

setImage

public void setImage(String name)
Change the image for this Sprite. The String refers to the file without an extention that contains the Sprite. The file must be listed in the imsInfo.txt file and the file must be in the images folder associated with the ImagesLoader.

Parameters:
name - Name of the file that contains the image. Do not include the file extension.

loopImage

public void loopImage(int animPeriod,
                      double seqDuration)
Switch on loop playing. The total time for the loop is seqDuration secs. The update interval (from the enclosing panel) is animPeriod ms.

Parameters:
animPeriod - The total time for plaing the loop in seconds.
seqDuration - The update interval for the enclosing panel in milliseconds.

stopLooping

public void stopLooping()
If looping, stop. Used for sprites with more than one image.


getWidth

public int getWidth()
The width of the Sprite's image.

Returns:
the width of the Sprite's image

getHeight

public int getHeight()
The height of the Sprite's image.

Returns:
the height of the Sprite's image

getPWidth

public int getPWidth()
The width of the Sprite's enclosing panel.

Returns:
the width of the Sprite's enclosing panel

getPHeight

public int getPHeight()
The height of the Sprite's enclosing panel.

Returns:
the height of the Sprite's enclosing panel

isActive

public boolean isActive()
Check if this Sprite is active.

Returns:
true if this Sprite is active, false otherwise.

setActive

public void setActive(boolean a)
Set this Sprite's active status.

Parameters:
new - value for active

setPosition

public void setPosition(int x,
                        int y)
Set the location of this Sprite. The coordinates specify the new upper left corner for this sprite.

Parameters:
x - the new x for the upper left corner of this Sprite
y - the new y for the upper left corner of this Sprite

translate

public void translate(int xDist,
                      int yDist)
Change the location of this Sprite. The distance are added to the current position coordinates.

Parameters:
xDist - amount to add to this Sprite's x location
yDist - amount to add to this Sprite's y location

getXPosn

public int getXPosn()
Get x location.

Returns:
The x coordinate of this Sprite.

getYPosn

public int getYPosn()
Get y location.

Returns:
The y coordinate of this Sprite.

setStep

public void setStep(int dx,
                    int dy)
Change this Sprite's x and y velocity. Units are pixels per frame. Positive dy is down, negative dy is up. Positive dx is right, negative dx is left.

Parameters:
dx - The new x velocity for this Sprite in pixels per frame.
dy - The new y velocity for this Sprite in pixels per frame.

getXStep

public int getXStep()
Get x velocity.

Returns:
x velocity of this Sprite

getYStep

public int getYStep()
Get y velocity.

Returns:
y velocity of this Sprite

getRectangle

public Rectangle getRectangle()
Get the bounding Rectangle for this Sprite. The Rectangle is the same size as this Sprite image.

Returns:
the bounding box or Rectangle for this Sprite.

updateSprite

public void updateSprite()
If this Sprite is active the x velocity and y velocity are applied to the x location and y location. If the Sprite's image is a looping image, the next image is selected. If this Sprite is not active, no changes are made.


drawSprite

public void drawSprite(Graphics g)
Draw this Sprite at its current location if it is active.

Parameters:
g - The graphics object that is used to draw this Sprite.

getImage

public BufferedImage getImage()
Get this Sprite's image.

Returns:
this Sprite's image