sprite
Class Sprite

java.lang.Object
  extended by sprite.Sprite
Direct Known Subclasses:
Bullet, Explosion, ImageSprite, OvalSprite, SimpleGun, TextSprite

public class Sprite
extends java.lang.Object

A Sprite represents a small graphical element that can draw itself in a SpritePanel. This class is not ordinarily used directly for creating objects. Use subclasses instead. It does proved a large number of methods and properties for working with sprites. Note that this class works closely with the SpritePanel class to implement sprite behavior. A Sprite must be added to a SpritePanel for it to do anything useful.


Field Summary
static int EXIT_POLICY_BOUNCE
          A value for the exitPolicy property, indicating that the sprite should bounce off the edge of the SpritePanel when it hits it.
static int EXIT_POLICY_DIE
          A value for the exitPolicy property, indicating that the sprite should die when it moves outside the SpritePanel.
static int EXIT_POLICY_STOP
          A value for the exitPolicy property, indicating that the sprite should stop moving if it hits the edge of the SpritPanel.
static int EXIT_POLICY_WRAP
          A value for the exitPolicy property, indicating that the sprite should wrap around to the opposite edge of the panel when it hits an edge of the SpritePanel.
 
Constructor Summary
Sprite()
           
 
Method Summary
 boolean contains(double a, double b)
          Tests whether the sprite contains the point (a,b).
 void die()
          Calling this method kills the sprite.
 void draw(java.awt.Graphics g)
          This method is called by the SpritePanel to tell the sprite to draw itself.
 double getDx()
           
 double getDy()
           
 int getExitPolicy()
           
 SpritePanel getHome()
          Gets the SpritePanel that contains this sprite.
 int getSpriteHeight()
           
 int getSpriteWidth()
           
 double getX()
           
 double getY()
           
 boolean isDead()
          Tells whether the sprite is dead.
 boolean isImmuneToBullets()
           
 void onDeath()
          This method is called by the SpritePanel when the sprite dies for any reason.
 void prepareForNextFrame(int frameNumber)
          This method is called by the SpritePanel to tell that sprite that a new frame is about to be drawn.
 void randomVelocity(double minimumSpeed, double maximumSpeed)
          Sets the velocity of the sprite to a random value.
 void setDx(double dx)
          Sets the horizontal (x) component of the sprite's velocity, given in pixels per frame.
 void setDy(double dy)
          Sets the vertical (y) component of the sprite's velocity, given in pixels per frame.
 void setExitPolicy(int policyCode)
          Tells what happens when a moving sprite reaches the edge of the SpritePanel that contains it.
 void setImmuneToBullets(boolean immune)
          Says whether this sprite should be immune to bullets.
 void setLocation(double newX, double newY)
          Sets the location of the center of the sprite.
 void setSpriteHeight(int height)
          Sets the height of the sprite.
 void setSpriteWidth(int width)
          Sets the width of the sprite.
 void setUseOvalForContains(boolean useOvalForContains)
          Tells whether the shape of this sprite is oval or rectangular.
 void setVelocity(double newDx, double newDy)
          Sets the velocity of the sprite.
 void setX(double x)
          Sets the x-coordinate of the center of the sprite.
 void setY(double y)
          Sets the y-coordinate of the center of the sprite.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXIT_POLICY_BOUNCE

public static final int EXIT_POLICY_BOUNCE
A value for the exitPolicy property, indicating that the sprite should bounce off the edge of the SpritePanel when it hits it. The sprite will not move outside the panel. This is the default behavior for a sprite.

See Also:
Constant Field Values

EXIT_POLICY_DIE

public static final int EXIT_POLICY_DIE
A value for the exitPolicy property, indicating that the sprite should die when it moves outside the SpritePanel. The sprite does not die until it has moved completely outside the panel.

See Also:
Constant Field Values

EXIT_POLICY_STOP

public static final int EXIT_POLICY_STOP
A value for the exitPolicy property, indicating that the sprite should stop moving if it hits the edge of the SpritPanel. The sprite will not move outside the panel.

See Also:
Constant Field Values

EXIT_POLICY_WRAP

public static final int EXIT_POLICY_WRAP
A value for the exitPolicy property, indicating that the sprite should wrap around to the opposite edge of the panel when it hits an edge of the SpritePanel. After the sprite moves completely out of the panel, it reappears on the opposite side.

See Also:
Constant Field Values
Constructor Detail

Sprite

public Sprite()
Method Detail

contains

public boolean contains(double a,
                        double b)
Tests whether the sprite contains the point (a,b). The test uses the width and height of the sprite as set by setSpriteWidth() and setSpriteHeight(). These values are not guaranteed to represent the actual size of the picture drawn by the sprite; it is up to subclasses to make sure that is true. This method is used by the SpritePanel to test whether a bullet has hit the sprite. Note that the sprite is assumed to be a rectangle, unless you call sprite.setUseOvalForContains(true).


die

public void die()
Calling this method kills the sprite. It is called automatically in some cases, such as when a moving sprite with exitPolicy = Sprite.EXIT_POLICY_EXIT moves outside the SpritePanel. When a sprite dies, it is automatically removed from the SpritePanel that contained it, and it is no longer drawn on the screen. A dead sprite cannot be brought back to life.


draw

public void draw(java.awt.Graphics g)
This method is called by the SpritePanel to tell the sprite to draw itself. It is not meant to be called directly. Subclasses should override this method to draw an appropriate picture. The method in this class just draws a tiny black dot at the location of the sprite.


getDx

public double getDx()

getDy

public double getDy()

getExitPolicy

public int getExitPolicy()

getHome

public SpritePanel getHome()
Gets the SpritePanel that contains this sprite. That panel is called the "home" of the sprite. The value of home is set automatically when the sprite is added to a SpritePanel.

Returns:
the panel, or null if this sprite is not in a SpritePanel

getSpriteHeight

public int getSpriteHeight()

getSpriteWidth

public int getSpriteWidth()

getX

public double getX()

getY

public double getY()

isDead

public boolean isDead()
Tells whether the sprite is dead.


isImmuneToBullets

public boolean isImmuneToBullets()

onDeath

public void onDeath()
This method is called by the SpritePanel when the sprite dies for any reason. It is not meant to be called directly. The method in this class does nothing. Subclasses that need to perform some action when the sprite dies can redefine this method.


prepareForNextFrame

public void prepareForNextFrame(int frameNumber)
This method is called by the SpritePanel to tell that sprite that a new frame is about to be drawn. It gives the sprite a chance to update its variables, if necessary. Note that the movement of the sprite, as required by its velocity setting, is done elsewhere, not in this method. This method is not meant to be called directly. Subclasses can override this method if they need to do something for each frame; however, that is probably relatively rare. The method in this clas does nothing.


randomVelocity

public void randomVelocity(double minimumSpeed,
                           double maximumSpeed)
Sets the velocity of the sprite to a random value. The direction in which the sprite should move is picked at random. The speed is also set at random, based on the parameters.

Parameters:
minimumSpeed - the minimum possible speed. Must be greater than or equal to zero.
maximumSpeed - the maximum possible speed. Must be greater than or equal to the minimum speed. (If they are equal, then the speed is not random, but the direction is still random.)

setDx

public void setDx(double dx)
Sets the horizontal (x) component of the sprite's velocity, given in pixels per frame. The velocity is zero by default.


setDy

public void setDy(double dy)
Sets the vertical (y) component of the sprite's velocity, given in pixels per frame. The velocity is zero by default.


setExitPolicy

public void setExitPolicy(int policyCode)
Tells what happens when a moving sprite reaches the edge of the SpritePanel that contains it. This applies only to sprites that have non-zero velocity. The exitPolicy does not stop you from placing a living, non-moving sprite outside the boundaries of the SpritePanel.

Parameters:
policyCode - the code for the policy. It must be one of the constants: Sprite.EXIT_POLICY_BOUNCE (the sprite bounces off the edge of the SpritePanel), Sprite.EXIT_POLICY_STOP (the sprite stops moving when it hits the edge of the SpritePanel), Sprite.EXIT_POLICY_DIE (the sprite dies when it leaves the SpritePanel), or Sprite.EXIT_POLICY_WRAP (the sprite re-emerges at the opposite edge when it leaves the SpritePanel),

setImmuneToBullets

public void setImmuneToBullets(boolean immune)
Says whether this sprite should be immune to bullets. A sprite that is not immune to bullets dies when it is hit by a bullet.


setLocation

public void setLocation(double newX,
                        double newY)
Sets the location of the center of the sprite. Note that there is nothing to stop you from setting the location to be outside the panel,


setSpriteHeight

public void setSpriteHeight(int height)
Sets the height of the sprite. This should be equal to the height of the picture that the sprite draws, but that is not enforced. The width and height are used in two cases: For testing whether the sprite contains a given point (which is used to tell whether the sprite has been hit by a bullet) and for doing the "exitPolicy" calculations (that is, testing whether the sprite has hit or passed the boundary of the SpritePanel). Of course, the width and height might be used for other purposes in subclasses. But if the height isn't used for anything, then there is no need to set it! The default value is zero.

Parameters:
width - the new width of the sprite. Must be greater than or equal to zero.

setSpriteWidth

public void setSpriteWidth(int width)
Sets the width of the sprite. This should be equal to the width of the picture that the sprite draws, but that is not enforced. The width and height are used in two cases: For testing whether the sprite contains a given point (which is used to tell whether the sprite has been hit by a bullet) and for doing the "exitPolicy" calculations (that is, testing whether the sprite has hit or passed the boundary of the SpritePanel). Of course, the width and height might be used for other purposes in subclasses. But if the width isn't used for anything, then there is no need to set it! The default value is zero.

Parameters:
width - the new width of the sprite. Must be greater than or equal to zero.

setUseOvalForContains

public void setUseOvalForContains(boolean useOvalForContains)
Tells whether the shape of this sprite is oval or rectangular. The value is used only in the method contains(a,b).

Parameters:
useOvalForContains - true says that the shape is oval.

setVelocity

public void setVelocity(double newDx,
                        double newDy)
Sets the velocity of the sprite. The default velocity is zero. If the sprite is going to move by itself, a non-zero velocity must be assigned to it.

Parameters:
newDx - the horizontal component of the velocity
newDy - the vertical component of the velocity

setX

public void setX(double x)
Sets the x-coordinate of the center of the sprite.


setY

public void setY(double y)
Sets the y-coordinate of the center of the sprite.