org.haphazard.gui.engine
Class Sprite

java.lang.Object
  extended byorg.haphazard.gui.engine.Sprite
All Implemented Interfaces:
java.lang.Cloneable

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

A class for handling animated or non-animated graphics Note: Sprite = a non-moving object (though it can be moved, now) that may be animated. xPos and yPos defines its position, i.e., the upper-left corner starting position, in isometric coordinates


Field Summary
 java.lang.String mLink
          A String denoting where this Sprite were loaded from
 
Constructor Summary
Sprite(java.lang.String name, boolean displayLoop)
          This constructor can be used for XML-parsing.
Sprite(java.lang.String name, Frame[] frames, boolean displayLoop)
          The constructor that inilize the variables.
 
Method Summary
 java.lang.Object clone()
           
 Sprite combine(Sprite spriteToBeInFront, java.lang.String theNameOfTheNewSprite)
          Combine this sprite with a new sprite.
 Sprite flipBoth()
          Creates a new sprite that is flipped both ways
 Sprite flipHorizontally()
          Creates a new sprite that is horizontally flipped
 Sprite flipVertically()
          Creates a new sprite that is vertically flipped
 java.awt.Rectangle getBoundingBox(java.awt.Point middleTileCoordinate)
          Return the bounding box on the screen of this image.
 java.util.Vector getFrames()
          Return the array of frames that this Sprite has.
 java.lang.String getMirrorName(java.lang.String alignment)
          Gets this images mirror name, or null if it has none
 java.lang.String getName()
          Gets this sprites name
 boolean hasMirror()
          Checks if this sprite has a mirror image
 boolean isAnimated()
          A check to se if this sprites contains a animation ( = more than one frame )
 boolean isAnimationTerminated()
          Checks if the animation is finished (i.e. the sprited displays the last frame)
 boolean isLoop()
           
 boolean isMirror()
          Checks if this sprite is a generated mirror image If it is generated the sprite will not generate any XML info on save
 boolean isMirror(boolean isMirror)
          Sets if this sprite is a generated mirror image
 void optimize()
          Optimizes this Sprite for better graphics performance
 boolean paintComponent(java.awt.Graphics2D g, java.awt.Point middleTileCoordinate)
          Paint this Sprite on the screen-coordinate
 void resetAnimation()
          Resets this sprites animation.
 void setFrames(java.util.Vector frames)
          This is used in the XML-parser
 void setLoop(boolean loop)
          Sets if this animation loops or not
 void setMirror(java.lang.String mirrorDirection, java.lang.String name)
          Sets that this sprite has a mirror The actual mirror is generated when loading the sprite, if a mirror sprite is needed right away also use GlobalImageUtilities.flipHorizontally and GlobalImageUtilities.flipVertically
 void setName(java.lang.String name)
          Sets this sprites name;
 java.lang.String toString()
          Transforms this sprite into a string notation
 java.lang.String toXML(int indent)
          Transforms this object into XML-notation
 void update()
          Calculate what frame should be displayed
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mLink

public java.lang.String mLink
A String denoting where this Sprite were loaded from

Constructor Detail

Sprite

public Sprite(java.lang.String name,
              Frame[] frames,
              boolean displayLoop)
The constructor that inilize the variables. This constructor should be used!!!

Parameters:
name - The name of the sprite
frames - a array of Frames
displayLoop - a boolean that sets if this sprite-animation should loop of it should stop when all the frames have been displayed

Sprite

public Sprite(java.lang.String name,
              boolean displayLoop)
This constructor can be used for XML-parsing.

Parameters:
name - the name of the sprite. This is used as a hash-key in DynamicGraphicalObject
displayLoop - if this Sprite-animation should be a loop
Method Detail

setFrames

public void setFrames(java.util.Vector frames)
This is used in the XML-parser

Parameters:
frames - the Frames-array that this Sprite contains

setLoop

public void setLoop(boolean loop)
Sets if this animation loops or not

Parameters:
loop - True if it should loop

isLoop

public boolean isLoop()

getName

public java.lang.String getName()
Gets this sprites name

Returns:
The sprites name

setName

public void setName(java.lang.String name)
Sets this sprites name;

Parameters:
name - the new name

getFrames

public java.util.Vector getFrames()
Return the array of frames that this Sprite has.

Returns:
array of frames. The array return is NOT a copy!

getBoundingBox

public java.awt.Rectangle getBoundingBox(java.awt.Point middleTileCoordinate)
Return the bounding box on the screen of this image.

Parameters:
middleTileCoordinate - The middle coordinate of the tile the sprite is residing on
Returns:
the Rectangle with the bounding box

hasMirror

public boolean hasMirror()
Checks if this sprite has a mirror image

Returns:
True if the sprite has a mirror image

getMirrorName

public java.lang.String getMirrorName(java.lang.String alignment)
Gets this images mirror name, or null if it has none

Parameters:
alignment - The alignment of the mirror (vertical, horizontal or both)
Returns:
The mirrors name, null if none exists

setMirror

public void setMirror(java.lang.String mirrorDirection,
                      java.lang.String name)
Sets that this sprite has a mirror The actual mirror is generated when loading the sprite, if a mirror sprite is needed right away also use GlobalImageUtilities.flipHorizontally and GlobalImageUtilities.flipVertically

Parameters:
mirrorDirection - "both" if mirror in both directions
"vertical" if a vertical mirror should exist
"horizontal" if a horizontal mirror should exist
name - The name of the mirror image

isMirror

public boolean isMirror(boolean isMirror)
Sets if this sprite is a generated mirror image

Parameters:
isMirror - This sprite is generated and should not be saved
Returns:
If this sprite is generated or not

isMirror

public boolean isMirror()
Checks if this sprite is a generated mirror image If it is generated the sprite will not generate any XML info on save

Returns:
If this sprite is generated or not

flipVertically

public Sprite flipVertically()
Creates a new sprite that is vertically flipped

Returns:
A new sprite vertically flipped

flipHorizontally

public Sprite flipHorizontally()
Creates a new sprite that is horizontally flipped

Returns:
A new sprite horizontally flipped

flipBoth

public Sprite flipBoth()
Creates a new sprite that is flipped both ways

Returns:
A new sprite horizontally flipped

combine

public Sprite combine(Sprite spriteToBeInFront,
                      java.lang.String theNameOfTheNewSprite)
Combine this sprite with a new sprite. This sprite will be behind the new sprite.

Parameters:
spriteToBeInFront - the sprite that should be in front.
theNameOfTheNewSprite - the new name of the sprite.
Returns:
the new Sprite. If one of the parameters are null then it returns null.

optimize

public void optimize()
Optimizes this Sprite for better graphics performance


paintComponent

public boolean paintComponent(java.awt.Graphics2D g,
                              java.awt.Point middleTileCoordinate)
Paint this Sprite on the screen-coordinate

Parameters:
middleTileCoordinate - The middle coordinate of the tile the Sprite is residing on
g - the Graphics where to be drawn
Returns:
If the frame that is painted has changed or not

update

public void update()
Calculate what frame should be displayed


isAnimationTerminated

public boolean isAnimationTerminated()
Checks if the animation is finished (i.e. the sprited displays the last frame)

Returns:
boolean true is is terminated, false otherwise. If this sprite is a loop then it always returns false.

isAnimated

public boolean isAnimated()
A check to se if this sprites contains a animation ( = more than one frame )

Returns:
true if the sprite is not animated

resetAnimation

public void resetAnimation()
Resets this sprites animation. (i.e. it sets the frameNr to point to the first frame);


toXML

public java.lang.String toXML(int indent)
Transforms this object into XML-notation

Parameters:
indent - The level of indentation to use
Returns:
The object in a String notation

toString

public java.lang.String toString()
Transforms this sprite into a string notation

Returns:
A String notation of this sprite

clone

public java.lang.Object clone()