tipgame.sprite
Class ImageSprite

java.lang.Object
  extended by tipgame.Sprite
      extended by tipgame.sprite.ImageSprite

public class ImageSprite
extends Sprite

Author:
Jam Jenkins This class takes an image and transforms it into a Sprite.

Field Summary
private  java.awt.Image buffered
          the image in its scaled size
private  double bufferedScale
          the size of the currently scaled image
private  java.awt.Dimension bufferSize
          the dimensions fo the transformed image
private  boolean flipHorizontal
          true indicates the image should be flipped along the horizontal axis, false is the default
private  boolean flipVertical
          true indicates the image should be flipped along the vertical axis, false is the default
private  java.awt.Image image
          the image in its original size
(package private) static java.util.HashMap<java.net.URL,java.awt.Image> imageCache
          images are big and expensive, cache them if you can.
(package private) static java.util.HashMap<java.lang.String,java.awt.Image> resizedCache
          every time an image is resized, the image is stored so that future uses of images of this size need not be resized again.
(package private) static java.util.HashMap<java.awt.Image,java.net.URL> reverseMap
          this is used to enable removal from the cache given the Image
 
Fields inherited from class tipgame.Sprite
color, tracker
 
Constructor Summary
ImageSprite(java.net.URL filename, boolean keepAspect)
          Creates a Sprite from an image file.
 
Method Summary
private  void drawReliably(java.awt.Graphics2D brush, java.awt.Image image, double x, double y)
          if an image is not ready to be drawn, the drawImage method of graphics returns immediately false.
 void flipAlongHorizontalAxis()
          flips the image along the horizontal axis before drawing it
 void flipAlongVerticalAxis()
          flips the image along the vertical axis before drawing it
 double getRotation()
          gets the rotation
private  void initializeImage(java.net.URL filename)
          gets the image from cache if possible, from file otherwise
 void paint(java.awt.Graphics2D brush)
          draws the image
 void setRotation(double angle)
          sets the rotation
private  void updateBufferedImage()
          get the image from cache if possible, otherwise, resize and cache the scaled image
 
Methods inherited from class tipgame.Sprite
getBounds2D, getColor, getLocation, getNormalVector, getScale, getShape, getTracker, getUseBoundingBox, intersects, isDestroyed, isEnabled, isVisible, kill, normalize, paintInternal, rotate, scale, setColor, setEnabled, setLocation, setLocation, setScale, setShape, setTracker, setUseBoundingBox, setVisible, translate, update, updateInternal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

image

private java.awt.Image image
the image in its original size


buffered

private java.awt.Image buffered
the image in its scaled size


bufferedScale

private double bufferedScale
the size of the currently scaled image


bufferSize

private java.awt.Dimension bufferSize
the dimensions fo the transformed image


flipVertical

private boolean flipVertical
true indicates the image should be flipped along the vertical axis, false is the default


flipHorizontal

private boolean flipHorizontal
true indicates the image should be flipped along the horizontal axis, false is the default


imageCache

static java.util.HashMap<java.net.URL,java.awt.Image> imageCache
images are big and expensive, cache them if you can. This maps the name of an image file to the Image to avoid making multiple copies.


reverseMap

static java.util.HashMap<java.awt.Image,java.net.URL> reverseMap
this is used to enable removal from the cache given the Image


resizedCache

static java.util.HashMap<java.lang.String,java.awt.Image> resizedCache
every time an image is resized, the image is stored so that future uses of images of this size need not be resized again.

Constructor Detail

ImageSprite

public ImageSprite(java.net.URL filename,
                   boolean keepAspect)
Creates a Sprite from an image file. The image file must be located in the tipgame/sprite/images folder (directory). Like all Sprites, this one will start centered at (0, 0) and have unit width and height.

Method Detail

initializeImage

private void initializeImage(java.net.URL filename)
gets the image from cache if possible, from file otherwise

Parameters:
filename - the image file

flipAlongVerticalAxis

public void flipAlongVerticalAxis()
flips the image along the vertical axis before drawing it

Overrides:
flipAlongVerticalAxis in class Sprite

flipAlongHorizontalAxis

public void flipAlongHorizontalAxis()
flips the image along the horizontal axis before drawing it

Overrides:
flipAlongHorizontalAxis in class Sprite

updateBufferedImage

private void updateBufferedImage()
get the image from cache if possible, otherwise, resize and cache the scaled image


setRotation

public void setRotation(double angle)
sets the rotation

Overrides:
setRotation in class Sprite
Parameters:
angle - the rotation in radians
See Also:
Sprite.setRotation(double)

getRotation

public double getRotation()
gets the rotation

Overrides:
getRotation in class Sprite
Returns:
the rotation in radians
See Also:
Sprite.getRotation()

paint

public void paint(java.awt.Graphics2D brush)
draws the image

Overrides:
paint in class Sprite
Parameters:
brush - the Graphics used to draw the shape

drawReliably

private void drawReliably(java.awt.Graphics2D brush,
                          java.awt.Image image,
                          double x,
                          double y)
if an image is not ready to be drawn, the drawImage method of graphics returns immediately false. Since the image must be drawn reliably, this method tries every 1 millisecond until the image is fully loaded and ready to draw.

Parameters:
brush - the Graphics2D used to draw the image
image - the image to be drawn
x - the horizontal position to draw
y - the vertical position to draw