java.lang.Objectsprite.Sprite
public class Sprite
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 |
---|
public static final int EXIT_POLICY_BOUNCE
public static final int EXIT_POLICY_DIE
public static final int EXIT_POLICY_STOP
public static final int EXIT_POLICY_WRAP
Constructor Detail |
---|
public Sprite()
Method Detail |
---|
public boolean contains(double a, double b)
public void die()
public void draw(java.awt.Graphics g)
public double getDx()
public double getDy()
public int getExitPolicy()
public SpritePanel getHome()
public int getSpriteHeight()
public int getSpriteWidth()
public double getX()
public double getY()
public boolean isDead()
public boolean isImmuneToBullets()
public void onDeath()
public void prepareForNextFrame(int frameNumber)
public void randomVelocity(double minimumSpeed, double maximumSpeed)
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.)public void setDx(double dx)
public void setDy(double dy)
public void setExitPolicy(int policyCode)
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),public void setImmuneToBullets(boolean immune)
public void setLocation(double newX, double newY)
public void setSpriteHeight(int height)
width
- the new width of the sprite. Must be greater than or equal to zero.public void setSpriteWidth(int width)
width
- the new width of the sprite. Must be greater than or equal to zero.public void setUseOvalForContains(boolean useOvalForContains)
useOvalForContains
- true says that the shape is oval.public void setVelocity(double newDx, double newDy)
newDx
- the horizontal component of the velocitynewDy
- the vertical component of the velocitypublic void setX(double x)
public void setY(double y)