Genuts API

com.genuts.gameui
Class Sprite

java.lang.Object
  |
  +--com.genuts.gameui.Sprite
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
ImageSequenceSprite, ScrollingImageSprite, SpotSprite, SpriteLabel, SpriteWrapper

public class Sprite
extends java.lang.Object
implements java.lang.Cloneable

A sprite is an object having a graphical representation that have its own behavior.

Class Sprite can be extended directly to specify completly the behavior.
To know if a sprite is in collision with another one, the method checkCollision(Sprite) is called by the playfield for each sprite which is not a background sprite.
After that, when the collision event is fired, the method collisionWith(Sprite) is called to perform the action for the collision.

A sprite can be identified by its ID, to avoid specialization of a sprite to identify it.

For special behavior, user interaction and other modifications, please to use a SpriteWrapper. In that way, a sprite on which a sprite wrapper interacts is called an action sprite.

See Also:
setId(int), getId()

Constructor Summary
Sprite()
          Initialize a simple sprite.
Sprite(java.awt.Image image)
          Initializes the sprite with an image.
Sprite(int w, int h)
          Initializes the sprite with a size.
 
Method Summary
 boolean checkCollision(Sprite sprite)
          Checks collision of this sprite with another sprite or with the edges of the playfield.
 Sprite cloneSprite()
          Creates a new sprite of the same class as this sprite.
protected  void collisionWith(Sprite s)
          Receives the event that this sprite is collision with another sprite.
 Sprite getActionSprite()
          Returns the action sprite.
 java.awt.Color getBackground()
          Gets the background color of this sprite.
 int getBottomCollisionOffset()
          Gets the offset on the bottom side for which the collision must not be considered.
 java.awt.Rectangle getBounds()
          Gets the bounds of this sprite in the form of a Rectangle object.
 Sprite getFinalActionSprite()
          Returns the first action sprite contained in this sprite that is not a SpriteWrapper.
 Sprite getFinalWrapper()
          Gets the final wrapper sprite of this sprite.
 int getHeight()
          Gets the height of the sprite.
 int getId()
          Gets the id of this sprite.
 java.awt.Image getImage()
          Gets the image of this sprite.
 int getLeftCollisionOffset()
          Gets the offset on the left side for which the collision must not be considered.
 PlayField getParent()
          Gets the parent playfield of this sprite.
 java.awt.Point getPosition()
          Gets the position of this sprite in the playfield.
 int getRightCollisionOffset()
          Gets the offset on the right side for which the collision must not be considered.
 java.awt.Dimension getSize()
          Returns the size of this sprite.
 int getTopCollisionOffset()
          Gets the offset on the top side for which the collision must not be considered.
 int getWidth()
          Gets the width of the sprite.
 Sprite getWrapper()
          Gets the wrapper sprite of this sprite.
 int getX()
          Gets the X coordinate of the sprite.
 int getY()
          Gets the Y coordinate of the sprite.
 void invalidate()
          Invalidates this sprite.
 boolean isBackgroundSprite()
          Is this sprite a piece of the background?
 boolean isPremode()
          Returns the pre-collision mode state for this sprite.
 boolean isSpriteAt(int x, int y)
          Indicates if this sprite is under (x,y) coordinates.
 boolean isValidate()
          Indicates if this sprite as been valideted by its parent.
 boolean isVisible()
          Indicates if the sprite is visible.
 void paint(java.awt.Graphics g)
          Draws the sprite.
protected  boolean preCollisionWith(Sprite s)
          Receives the event that this sprite is in pre-collision with another sprite.
 void repaint()
          Repaints this component.
 void setBackground(java.awt.Color color)
          Sets the background color for this sprite.
 void setBackgroundSprite(boolean v)
          Sets if this sprite is a piece of the background.
 void setBottomCollisionOffset(int offset)
          Sets the offset on the bottom side for which the collision must not be considered.
 boolean setBounds(int x, int y, int width, int height)
          Moves and resizes this sprite to conform to the new bounding rectangle.
 boolean setBounds(java.awt.Rectangle r)
          Moves and resizes this sprite to conform to the new bounding rectangle r.
 void setId(int id)
          Sets this sprite's id.
 void setImage(java.awt.Image img)
          Sets the image for this sprite.
 void setLeftCollisionOffset(int offset)
          Sets the offset on the left side for which the collision must not be considered.
protected  void setParent(PlayField parent)
          Sets the parent playfield of this sprite.
 boolean setPosition(int x, int y)
          Sets the position of this sprite in the playfield.
 void setPremode(boolean state)
          Sets the pre-collision mode for this sprite.
 void setRightCollisionOffset(int offset)
          Sets the offset on the right side for which the collision must not be considered.
 boolean setSize(int w, int h)
          Sets the size of this sprite.
 void setTopCollisionOffset(int offset)
          Sets the offset on the top side for which the collision must not be considered.
 void setVisible(boolean v)
          Sets if the sprite is visible.
protected  void setWrapper(SpriteWrapper wrapper)
          Sets the wrapper sprite of this sprite.
 java.lang.String toString()
          Returns a string representation of this Sprite.
 void validate()
          Validates this sprite by its parent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Sprite

public Sprite()
Initialize a simple sprite.


Sprite

public Sprite(int w,
              int h)
Initializes the sprite with a size.

Parameters:
w - Width of the sprite.
h - Height of the sprite.

Sprite

public Sprite(java.awt.Image image)
Initializes the sprite with an image.

Parameters:
image - Image to display for the sprite.
Method Detail

getWrapper

public Sprite getWrapper()
Gets the wrapper sprite of this sprite.
If this sprite has no wrapper, it return itself.

Returns:
The wrapper sprite of this sprite.

getFinalWrapper

public Sprite getFinalWrapper()
Gets the final wrapper sprite of this sprite.
If this sprite has no wrapper, it return itself.

Returns:
The final wrapper sprite of this sprite.

setWrapper

protected void setWrapper(SpriteWrapper wrapper)
Sets the wrapper sprite of this sprite.

Parameters:
wrapper - Wrapper sprite of this sprite.

getFinalActionSprite

public Sprite getFinalActionSprite()
Returns the first action sprite contained in this sprite that is not a SpriteWrapper.
i.e. Search the first sprite that is not a SpriteWrapper.

Returns:
The First action sprite that is not a SpriteWrapper.

getActionSprite

public Sprite getActionSprite()
Returns the action sprite.

Returns:
The action sprite.

setId

public void setId(int id)
Sets this sprite's id.
The id is usefull to make difference between sprites for collisions.

Parameters:
id - ID of this sprite

getId

public int getId()
Gets the id of this sprite.
The id is usefull to make difference between sprites for collisions.

Returns:
The ID of this sprite

getSize

public java.awt.Dimension getSize()
Returns the size of this sprite.

Returns:
An instance of Dimension that represents the size of this sprite.

setSize

public boolean setSize(int w,
                       int h)
Sets the size of this sprite.

Returns:
true if the modification was done, false otherwise.

setPosition

public boolean setPosition(int x,
                           int y)
Sets the position of this sprite in the playfield.

Parameters:
x - X position of the sprite.
y - Y position of the sprite.
Returns:
true if the modification was done, false otherwise.

getPosition

public java.awt.Point getPosition()
Gets the position of this sprite in the playfield.

Returns:
An instance of Point represent the position of the sprite in the playfield.

getBounds

public java.awt.Rectangle getBounds()
Gets the bounds of this sprite in the form of a Rectangle object.
The bounds specify this sprite's width, height, and location relative to its parent.

Returns:
A rectangle indicating this sprite's bounds.

getX

public int getX()
Gets the X coordinate of the sprite.

Returns:
The X coordinate of the sprite.

getY

public int getY()
Gets the Y coordinate of the sprite.

Returns:
The Y coordinate of the sprite.

getWidth

public int getWidth()
Gets the width of the sprite.

Returns:
The width of the sprite.

getHeight

public int getHeight()
Gets the height of the sprite.

Returns:
The height of the sprite.

setBounds

public boolean setBounds(java.awt.Rectangle r)
Moves and resizes this sprite to conform to the new bounding rectangle r.
This sprite's new position is specified by r.x and r.y, and its new size is specified by r.width and r.height.

Parameters:
r - The new bounding rectangle for this sprite.
Returns:
true if the modification was done, false otherwise.

setBounds

public boolean setBounds(int x,
                         int y,
                         int width,
                         int height)
Moves and resizes this sprite to conform to the new bounding rectangle.

Parameters:
x - X position of the sprite.
y - Y position of the sprite.
width - width of the sprite.
height - height of the sprite.
Returns:
true if the modification was done, false otherwise.

isSpriteAt

public boolean isSpriteAt(int x,
                          int y)
Indicates if this sprite is under (x,y) coordinates.
(x,y) coordinates are located in the playfield area. By default it checks if (x,y) are in the bounding box of the sprite (excluding the collision offsets of the sprite).

Parameters:
x - X coordinate in the sprite to check.
y - Y coordinate in the sprite to check.
Returns:
true if this sprite is under (x,y) coordinates, false otherwise.

setPremode

public void setPremode(boolean state)
Sets the pre-collision mode for this sprite.

The pre-collision mode allows to a sprite to detect a collision just before it happens.

When the size, or the position or bounds of this sprite changes, and if the pre-collision mode is set to true, a check for collision is made and if it happens, it call launch a preCollisionWith(Sprite) event. This event has to return true if the new setting is correct, false otherwise.

Parameters:
state - true to set the pre-collision mode to true, false otherwise.

isPremode

public boolean isPremode()
Returns the pre-collision mode state for this sprite.

Returns:
true if the pre-collision mode is true, false otherwise.

checkCollision

public boolean checkCollision(Sprite sprite)
Checks collision of this sprite with another sprite or with the edges of the playfield.
By default, it checks the intersection of bounding box of sprites.

Parameters:
sprite - Sprite to check the collision with.
Returns:
true if this sprite and the other sprite are in collision, false otherwise.

getRightCollisionOffset

public int getRightCollisionOffset()
Gets the offset on the right side for which the collision must not be considered.

Returns:
The offset on the right side for which the collision must not be considered.

setRightCollisionOffset

public void setRightCollisionOffset(int offset)
Sets the offset on the right side for which the collision must not be considered.


getLeftCollisionOffset

public int getLeftCollisionOffset()
Gets the offset on the left side for which the collision must not be considered.

Returns:
The offset on the left side for which the collision must not be considered.

setLeftCollisionOffset

public void setLeftCollisionOffset(int offset)
Sets the offset on the left side for which the collision must not be considered.


getTopCollisionOffset

public int getTopCollisionOffset()
Gets the offset on the top side for which the collision must not be considered.

Returns:
The offset on the top side for which the collision must not be considered.

setTopCollisionOffset

public void setTopCollisionOffset(int offset)
Sets the offset on the top side for which the collision must not be considered.


getBottomCollisionOffset

public int getBottomCollisionOffset()
Gets the offset on the bottom side for which the collision must not be considered.

Returns:
The offset on the bottom side for which the collision must not be considered.

setBottomCollisionOffset

public void setBottomCollisionOffset(int offset)
Sets the offset on the bottom side for which the collision must not be considered.


isValidate

public boolean isValidate()
Indicates if this sprite as been valideted by its parent.

Returns:
true if this parent as valid it, false otherwise.

validate

public void validate()
Validates this sprite by its parent.


invalidate

public void invalidate()
Invalidates this sprite.


isBackgroundSprite

public boolean isBackgroundSprite()
Is this sprite a piece of the background?

Returns:
true if this sprite is a piece of the background, false otherwise.

setBackgroundSprite

public void setBackgroundSprite(boolean v)
Sets if this sprite is a piece of the background.

Parameters:
v - true if this sprite is a piece of the background, false otherwise.

setParent

protected void setParent(PlayField parent)
Sets the parent playfield of this sprite.

Parameters:
parent - Parent playfield of this sprite.

getParent

public PlayField getParent()
Gets the parent playfield of this sprite.

Returns:
The parent playfield of this sprite.

getImage

public java.awt.Image getImage()
Gets the image of this sprite.

Returns:
Image of the sprite.

setImage

public void setImage(java.awt.Image img)
Sets the image for this sprite.

Parameters:
img - Image to set.

getBackground

public java.awt.Color getBackground()
Gets the background color of this sprite.

Returns:
The background color of the sprite.

setBackground

public void setBackground(java.awt.Color color)
Sets the background color for this sprite.

Parameters:
color - Color to set.

isVisible

public boolean isVisible()
Indicates if the sprite is visible.
The visibility of a sprite has no consequence for collisions.

Returns:
true if the sprite is visible, false otherwise.

setVisible

public void setVisible(boolean v)
Sets if the sprite is visible.
The visibility of a sprite has no consequence for collisions.

Parameters:
v - true if the sprite is visible, false otherwise.

preCollisionWith

protected boolean preCollisionWith(Sprite s)
Receives the event that this sprite is in pre-collision with another sprite.
This method should only be called if the pre-collision mode of this sprite is set to true.

Coordonates and size of this sprite are temporally set, it is in charge of the developper to validate them by returning true, or to refuse them by returning false.
By default, it returns true.

Parameters:
s - Sprite with which the pre-collision is, null if it is with the edge of the playfield.
Returns:
true to accept the new position and the size of the sprite, false otherwise.

collisionWith

protected void collisionWith(Sprite s)
Receives the event that this sprite is collision with another sprite.
By default, it does nothing.

Parameters:
s - Sprite with which the collision is, null if it is with the edge of the playfield.

repaint

public void repaint()
Repaints this component.

This method causes a call to this component's paint method as soon as possible.

See Also:
paint(Graphics)

paint

public void paint(java.awt.Graphics g)
Draws the sprite.
By default, it draws the sprite image if it present.

Parameters:
g - The graphics context to use for painting.

cloneSprite

public Sprite cloneSprite()
Creates a new sprite of the same class as this sprite.
It then initializes each of the new sprite's fields by assigning it the same value as the corresponding field in this object except its parent playfield and its sprite wrapper.
No constructor is called.


toString

public java.lang.String toString()
Returns a string representation of this Sprite.
The string contains its ID number and its (x,y) position.

Overrides:
toString in class java.lang.Object

Genuts API

Genuts API