com.hdcookbook.grin.util
Class HeadlessManagedImage

java.lang.Object
  extended by com.hdcookbook.grin.util.ManagedImage
      extended by com.hdcookbook.grin.util.HeadlessManagedImage

public class HeadlessManagedImage
extends ManagedImage

This class implements the ManagedImage contract for a headless Java program. It is used in the grin compiler.


Constructor Summary
HeadlessManagedImage(java.lang.String name)
          Create a HeadlessManagedImage instance.
 
Method Summary
 void draw(java.awt.Graphics2D gr, int x, int y, java.awt.Component comp)
          Draw this image into the given graphics context
 void drawClipped(java.awt.Graphics2D gr, int x, int y, java.awt.Rectangle subsection, java.awt.Component comp)
          Draw the the given subsection of the image into a graphics context, without scaling.
 void drawScaled(java.awt.Graphics2D gr, java.awt.Rectangle bounds, java.awt.Component comp)
          Draw this image into the given graphics context, scaled to fit within the given bounds.
 int getHeight()
          Get the height of this image.
 java.lang.String getName()
           
 int getWidth()
          Get the width of this image.
 boolean hadErrorLoading()
          Tells whether or not the image had an error loading, e.g.
 boolean isLoaded()
          Determine whether or not the image is currently loaded.
 void load(java.awt.Component comp)
          Load this image for display in the given component, or any other component for the same graphics device.
 void prepare()
          Prepare this image for display, by registering interest in having the image be loaded.
 void startLoading(java.awt.Component comp)
          Start loading an image.
 void unprepare()
          Undo a prepare.
 
Methods inherited from class com.hdcookbook.grin.util.ManagedImage
equals, makeSticky, toString, unmakeSticky
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

HeadlessManagedImage

public HeadlessManagedImage(java.lang.String name)
Create a HeadlessManagedImage instance. These instances do not have their lifecycle managed by ImageManager.

Method Detail

getName

public java.lang.String getName()
Specified by:
getName in class ManagedImage

getWidth

public int getWidth()
Description copied from class: ManagedImage
Get the width of this image. This may return 0 if the image has not yet been loaded.

Specified by:
getWidth in class ManagedImage

getHeight

public int getHeight()
Description copied from class: ManagedImage
Get the height of this image. This may return 0 if the image has not yet been loaded.

Specified by:
getHeight in class ManagedImage

prepare

public void prepare()
Prepare this image for display, by registering interest in having the image be loaded. In order to actually load the image, load(Component) must be called.

See ManagedImage's main class documentation under "ManagedImage contract - image loading and unloading".

Specified by:
prepare in class ManagedImage
See Also:
ManagedImage.isLoaded(), ManagedImage.load(Component), ManagedImage.unprepare(), ManagedImage

isLoaded

public boolean isLoaded()
Determine whether or not the image is currently loaded. After a call to prepare(), this method can be used to query whether or not it's necessary to arrange for load(Component) to be called.

See ManagedImage's main class documentation under "ManagedImage contract - image loading and unloading".

Specified by:
isLoaded in class ManagedImage
See Also:
ManagedImage

hadErrorLoading

public boolean hadErrorLoading()
Tells whether or not the image had an error loading, e.g. because the path didn't refer to a valid image. This is always false if isLoaded() is false.

Specified by:
hadErrorLoading in class ManagedImage

load

public void load(java.awt.Component comp)
Load this image for display in the given component, or any other component for the same graphics device. The image will only be loaded if an interest in loading this ManagedImage has been registered by calling prepare(). If no interest has been registered, or if this image is already loaded, then this method will return immediately. If another thread is loading this same image, this method will wait until that image load is complete before it returns.

See ManagedImage's main class documentation under "ManagedImage contract - image loading and unloading". This version works with a null component.

Specified by:
load in class ManagedImage
Parameters:
comp - A component to use for loading the image. Clients using ManagedImage should never pass in null.
See Also:
ManagedImage.prepare(), ManagedImage.unprepare(), ManagedImage

startLoading

public void startLoading(java.awt.Component comp)
Start loading an image. This is just like load(Component), except that it doesn't block until the image is loaded. If the image has a postive prepare() count, then sometime after startLoading(Component) is called, isLoaded() will return true (unless, of course, the caller loses interest in the image and calls unprepare()

This method is useful for loading an image asynchronously when the threading model makes polling for image load a natural thing to do. For example, if one wants to load an image while a show is running, one good way to do that is to start the loading, then poll for the completion of the loading in a once-per-frame "heartbeat" method.

Specified by:
startLoading in class ManagedImage
See Also:
ManagedImage.prepare(), ManagedImage.unprepare(), ManagedImage.load(Component), ManagedImage.isLoaded(), ManagedImage

unprepare

public void unprepare()
Undo a prepare. We do reference counting; when the number of active prepares hits zero, and the "sticky" count reaches zero, we flush the image.

See ManagedImage's main class documentation under "ManagedImage contract - image loading and unloading".

Specified by:
unprepare in class ManagedImage
See Also:
ManagedImage.prepare(), ManagedImage.load(Component), ManagedImage.makeSticky(), ManagedImage.unmakeSticky(), ManagedImage

draw

public void draw(java.awt.Graphics2D gr,
                 int x,
                 int y,
                 java.awt.Component comp)
Draw this image into the given graphics context

Specified by:
draw in class ManagedImage

drawScaled

public void drawScaled(java.awt.Graphics2D gr,
                       java.awt.Rectangle bounds,
                       java.awt.Component comp)
Draw this image into the given graphics context, scaled to fit within the given bounds.

Specified by:
drawScaled in class ManagedImage

drawClipped

public void drawClipped(java.awt.Graphics2D gr,
                        int x,
                        int y,
                        java.awt.Rectangle subsection,
                        java.awt.Component comp)
Draw the the given subsection of the image into a graphics context, without scaling.

Specified by:
drawClipped in class ManagedImage