net.lucidviews.util.image
Class BaseImageObserver

java.lang.Object
  extended by net.lucidviews.util.image.BaseImageObserver
All Implemented Interfaces:
ImageObserver
Direct Known Subclasses:
DimensionImageObserver, RepaintImageObserver, SimpleImageObserver

public abstract class BaseImageObserver
extends Object
implements ImageObserver

A base class for ImageObservers that observe (react to) a specific image.

To prevent confusion between the imageUpdate method of the ImageObserver interface and the observedImageUpdate method of this class, the former has been made final.
Sub classes should implement the observedImageUpdate method in exactly the same way they would implement the imageUpdate method. When making the switch from implements ImageObserver to extends BaseImageObserver you should only need to change the name of your processing method from imageUpdate to observedImageUpdate.

This base class also implements functionality to make image observers compatible with JRE 1.5.0_06 (see keepObserverAlive).

Since:
1.0
Version:
$Revision: 1.3 $
Author:
Stephen Battey
See Also:
keepObserverAlive(), ImageObserver, Image

Nested Class Summary
protected static class BaseImageObserver.RespawnTimerTask
          A task that respawns an observer.
 
Field Summary
protected  Image _image
          The specific image being monitored by this observer.
private  boolean _needMoreData
          Flag indicating if this observer is still waiting for more data to load into the image.
private  TimerTask _respawnTask
          The current re-spawn task running on the respawn timer.
private  Timer _respawnTimer
          A timer used to run a re-spawn task.
protected static long FORCE_RESPAWN_DELAY
          The amount of time, in mill-seconds, an observer can go un
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
BaseImageObserver()
          Create an observer that is monitoring no specific image.
BaseImageObserver(Image image)
          Create an observer that is monitoring the specified image.
 
Method Summary
private  void cancelRespawn()
          Cancel the respawn task, if such a task is active.
 boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
           
private  void keepObserverAlive()
          Ensure this observer is kept alive until the image it is monitoring has been loaded completely.
protected abstract  boolean observedImageUpdate(Image img, int infoflags, int x, int y, int width, int height)
          This method is called when information about the image that is being observed, and was previously requested using an asynchronous interface, becomes available.
private  void respawn()
          This method should be called when it is thought the observer has been 'lost' in the system.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FORCE_RESPAWN_DELAY

protected static final long FORCE_RESPAWN_DELAY
The amount of time, in mill-seconds, an observer can go un

See Also:
Constant Field Values

_image

protected Image _image
The specific image being monitored by this observer. Leave as null if not monitoring a particular image.


_respawnTimer

private Timer _respawnTimer
A timer used to run a re-spawn task.

See Also:
keepObserverAlive()

_respawnTask

private TimerTask _respawnTask
The current re-spawn task running on the respawn timer.

See Also:
_respawnTimer

_needMoreData

private boolean _needMoreData
Flag indicating if this observer is still waiting for more data to load into the image.

Constructor Detail

BaseImageObserver

public BaseImageObserver()
Create an observer that is monitoring no specific image.


BaseImageObserver

public BaseImageObserver(Image image)
Create an observer that is monitoring the specified image.

Parameters:
image - the image that is to be monitored
Method Detail

observedImageUpdate

protected abstract boolean observedImageUpdate(Image img,
                                               int infoflags,
                                               int x,
                                               int y,
                                               int width,
                                               int height)
This method is called when information about the image that is being observed, and was previously requested using an asynchronous interface, becomes available.

This method should return true if further updates are needed or false if the required information has been acquired. The image which was being tracked is passed in using the img argument. Various constants are combined to form the infoflags argument which indicates what information about the image is now available. The interpretation of the x, y, width, and height arguments depends on the contents of the infoflags argument.

The infoflags argument should be the bitwise inclusive OR of the following flags: WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT.

Parameters:
img - the image being observed
infoflags - the bitwise inclusive OR of the following flags: WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT
x - the x coordinate
y - the y coordinate
width - the width
height - the height
Returns:
false if the infoflags indicate that the image is completely loaded; true otherwise

imageUpdate

public final boolean imageUpdate(Image img,
                                 int infoflags,
                                 int x,
                                 int y,
                                 int width,
                                 int height)
Specified by:
imageUpdate in interface ImageObserver

keepObserverAlive

private void keepObserverAlive()
Ensure this observer is kept alive until the image it is monitoring has been loaded completely.
This is done by scheduling a respawn task a second (or so) into the future. If this observer is forgotten then the task will timeout and call the respawn method of this observer, which will ensure this observer stays alive.
This technique was found to be necessary when running JImageTest in JRE 1.5.0_06. Other JREs (e.g. JRE 1.4) do not require this monitor-respawn functionality but it is included to ensure maximum compatibility.
This method should be called from the constructor and from the imageUpdate method whenever the observer is still waiting for more data.


cancelRespawn

private void cancelRespawn()
Cancel the respawn task, if such a task is active.


respawn

private void respawn()
This method should be called when it is thought the observer has been 'lost' in the system.