tipgame
Class Sprite

java.lang.Object
  extended by tipgame.Sprite
Direct Known Subclasses:
BlurSprite, ImageSprite, LightSprite, StarSprite, StringSprite, TriangleSprite, YourSprite

public class Sprite
extends java.lang.Object

this class provides a structure for representing sprites which have size, shape, orientation, and color. These sprites will appear when add to the AnimationCanvas. Sprites also optionally have a tracker which will update the Sprite's properties at each time interval.

Author:
Jam Jenkins

Field Summary
protected  java.awt.Color color
          the fill color of the shape, black by default
private  boolean destroy
          true indicates the Sprite will be removed from the AnimationCanvas
private  boolean enabled
          whether the sprite should be displayed and tracked
private  java.awt.geom.GeneralPath shape
          shape should initially be centered at (0, 0)
private  java.awt.geom.GeneralPath shapeTransformed
          transformed shape
protected  Tracker tracker
          determines the generic movement of the sprite
private  java.awt.geom.AffineTransform transform
          applied to the shape prior to drawing it
private  boolean useBoundingBox
          use bounding box for bounce geometry
private  boolean visible
          whether the sprite should be displayed.
 
Constructor Summary
Sprite()
          initialize to an empty shape and the default values for scale, rotation, an rotation.
Sprite(java.awt.Shape shape)
          initializes to a given shape and the default values for scale and rotation.
 
Method Summary
 void flipAlongHorizontalAxis()
          flips along the horizontal axis.
 void flipAlongVerticalAxis()
          flips along the vertical axis.
 java.awt.geom.Rectangle2D getBounds2D()
          returns 2-D bounding box of sprites
 java.awt.Color getColor()
          returns the fill color of the Sprite
 java.awt.geom.Point2D.Double getLocation()
          the location of the shape as measured from its original location at (0, 0)
static double getNormalVector(java.awt.Shape stationary, java.awt.Shape moving)
          Simulates the surface normal used for bouncing the moving object off of the stationary object.
 double getRotation()
          gets the current amount of rotation in radians
 double getScale()
          the scaling factor
 java.awt.Shape getShape()
          gets the shape in its current position, size, and orientation
 Tracker getTracker()
          gets the Tracker which has been attached to the Sprite
 boolean getUseBoundingBox()
           
 boolean intersects(Sprite sprite)
          determines if another Sprite intersects this Sprite
protected  boolean isDestroyed()
          determines if the sprite should be removed from the AnimationCanvas
 boolean isEnabled()
          determines the Sprite's ability to display and update
 boolean isVisible()
          returns whether the Sprite is currently displayable
 void kill()
          destroys the Sprite immediately
protected  void normalize()
          resizes to be 1 by 1 and centers around origin (0, 0)
 void paint(java.awt.Graphics2D brush)
          draws the shape in the proper location, orientation, and size
protected  void paintInternal(java.awt.Graphics2D brush)
          provides a mechanism for enabling and visibility
 void rotate(double rotation)
          sets the orienation with regard to the current orientation
 void scale(double scale)
          scale with regard to the current scaling.
 void setColor(java.awt.Color c)
          determines the fill color of the shape.
 void setEnabled(boolean able)
          sets the Sprite's ability to display and update
 void setLocation(double x, double y)
          sets the location without regard to its current location
 void setLocation(java.awt.geom.Point2D.Double location)
          sets the location without regard to its current location
 void setRotation(double rotation)
          sets the orientation without regard to the current orientation
 void setScale(double scale)
          sets the absolute scale without regard to its current value, must be nonzero
 void setShape(java.awt.Shape s)
          sets shape to shape s
 void setTracker(Tracker t)
          attaches the Tracker to this Sprite.
 void setUseBoundingBox(boolean box)
          Sets the parameter which determines if the actual shape or its minimal bounding box is used in tests for intersection.
 void setVisible(boolean vis)
          sets if the Sprite will be displayed when added to an AnimationCanvas
 void translate(java.awt.geom.Point2D.Double delta)
          moves the shape wiht regard to its current location
 void update(double interval)
          Calls advanceTime on the Sprite's Tracker (if the Tracker has been set).
protected  void updateInternal(double interval)
          provides a mechanism for disabling Sprites
private  void updateShape()
          updates shape of Sprite
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

shape

private java.awt.geom.GeneralPath shape
shape should initially be centered at (0, 0)


shapeTransformed

private java.awt.geom.GeneralPath shapeTransformed
transformed shape


transform

private java.awt.geom.AffineTransform transform
applied to the shape prior to drawing it


color

protected java.awt.Color color
the fill color of the shape, black by default


tracker

protected Tracker tracker
determines the generic movement of the sprite


useBoundingBox

private boolean useBoundingBox
use bounding box for bounce geometry


enabled

private boolean enabled
whether the sprite should be displayed and tracked


destroy

private boolean destroy
true indicates the Sprite will be removed from the AnimationCanvas


visible

private boolean visible
whether the sprite should be displayed. Note: a Sprite's visibility has no effect its tracking.

Constructor Detail

Sprite

public Sprite()
initialize to an empty shape and the default values for scale, rotation, an rotation. The Sprite must be centered around (0, 0) and width and height 1.


Sprite

public Sprite(java.awt.Shape shape)
initializes to a given shape and the default values for scale and rotation. The Sprite must be centered around (0, 0) and width and height 1.

Parameters:
shape -
Method Detail

kill

public void kill()
destroys the Sprite immediately


isDestroyed

protected boolean isDestroyed()
determines if the sprite should be removed from the AnimationCanvas

Returns:
true if Sprite should be removed, false otherwise

setUseBoundingBox

public void setUseBoundingBox(boolean box)
Sets the parameter which determines if the actual shape or its minimal bounding box is used in tests for intersection. Using a bounding box is the default and is usually fast and accurate for determining intersections for most roughly square objects.

Parameters:
box - true indicates a bounding box should be used, false indicates the actual shape should be used

getUseBoundingBox

public boolean getUseBoundingBox()
Returns:
true if a bounding box is being used in tests for intersection, false if the actual shape is being used

setShape

public void setShape(java.awt.Shape s)
sets shape to shape s

Parameters:
s -

normalize

protected void normalize()
resizes to be 1 by 1 and centers around origin (0, 0)


setColor

public void setColor(java.awt.Color c)
determines the fill color of the shape. All shapes are filled by default and the default color of the fill is black.

Parameters:
c - the fill color

setTracker

public void setTracker(Tracker t)
attaches the Tracker to this Sprite. The advanceTime method will be called between each Frame, and the Tracker will set the Sprite's location, orientation, and size.

Parameters:
t - the Tracker which will act upon this Sprite

getColor

public java.awt.Color getColor()
returns the fill color of the Sprite

Returns:
the fill color

getTracker

public Tracker getTracker()
gets the Tracker which has been attached to the Sprite

Returns:
the Tracker acting upon the Sprite, or null if no Tracker has been set

setScale

public void setScale(double scale)
sets the absolute scale without regard to its current value, must be nonzero

Parameters:
scale - the new scale

updateShape

private void updateShape()
updates shape of Sprite


updateInternal

protected void updateInternal(double interval)
provides a mechanism for disabling Sprites


update

public void update(double interval)
Calls advanceTime on the Sprite's Tracker (if the Tracker has been set). Then the Sprite's location, size, and orientation are determined using the Tracker.


scale

public void scale(double scale)
scale with regard to the current scaling. The resulting scale will be the product of the current scale and the scale parameter.

Parameters:
scale - the factor by which to change the current scale, must be nonzero

setRotation

public void setRotation(double rotation)
sets the orientation without regard to the current orientation

Parameters:
rotation - the new orientation in radians counter-clockwise for the original orientation

rotate

public void rotate(double rotation)
sets the orienation with regard to the current orientation

Parameters:
rotation - the additional rotation in radians

getScale

public double getScale()
the scaling factor

Returns:
the ratio of the current size to the orignal size of the shape

getLocation

public java.awt.geom.Point2D.Double getLocation()
the location of the shape as measured from its original location at (0, 0)

Returns:
the location of the shape

setLocation

public void setLocation(java.awt.geom.Point2D.Double location)
sets the location without regard to its current location

Parameters:
location - the new location

setLocation

public void setLocation(double x,
                        double y)
sets the location without regard to its current location

Parameters:
x - the horizontal location
y - the vertical location

translate

public void translate(java.awt.geom.Point2D.Double delta)
moves the shape wiht regard to its current location

Parameters:
delta - the amount and direction to move the shape from its current location

getNormalVector

public static double getNormalVector(java.awt.Shape stationary,
                                     java.awt.Shape moving)
Simulates the surface normal used for bouncing the moving object off of the stationary object. Normal is in the direction from the surface of the stationary object to the center of the moving shape's bounding box.

Parameters:
stationary - the object not in motion
moving - the object that will bounce off of the stationary object
Returns:
the radians of the normal vector

getRotation

public double getRotation()
gets the current amount of rotation in radians

Returns:
the positive rotation in radians less than 2*PI and zero or more

isVisible

public boolean isVisible()
returns whether the Sprite is currently displayable

Returns:
true if Sprite can be displayed, false otherwise

setVisible

public void setVisible(boolean vis)
sets if the Sprite will be displayed when added to an AnimationCanvas

Parameters:
vis - true indicates to display, false to hide

paintInternal

protected void paintInternal(java.awt.Graphics2D brush)
provides a mechanism for enabling and visibility

Parameters:
brush - the Graphics2D to draw on

paint

public void paint(java.awt.Graphics2D brush)
draws the shape in the proper location, orientation, and size

Parameters:
brush - the Graphics used to draw the shape

getShape

public java.awt.Shape getShape()
gets the shape in its current position, size, and orientation

Returns:
the shape

getBounds2D

public java.awt.geom.Rectangle2D getBounds2D()
returns 2-D bounding box of sprites

Returns:
BoundingBox

intersects

public boolean intersects(Sprite sprite)
determines if another Sprite intersects this Sprite

Parameters:
sprite - another Sprite
Returns:
true if the other Sprite intersects this Sprite, false if there is no intersection

isEnabled

public boolean isEnabled()
determines the Sprite's ability to display and update

Returns:
true if Sprite can display and update, false otherwise

setEnabled

public void setEnabled(boolean able)
sets the Sprite's ability to display and update

Parameters:
able - true indicates the Sprite can display and update, false otherwise

flipAlongVerticalAxis

public void flipAlongVerticalAxis()
flips along the vertical axis. The result of calling this method rotates the sprite by Math.PI radians.


flipAlongHorizontalAxis

public void flipAlongHorizontalAxis()
flips along the horizontal axis. The result of calling this method has no effect on the sprite's rotation.