wyvern.common.util
Class ClientImageCache

java.lang.Object
  extended bywyvern.common.util.ClientImageCache
Direct Known Subclasses:
ImageCache

public class ClientImageCache
extends java.lang.Object

Client-side cache mapping tile numbers to images.

The cache provides methods for extracting 32x32 pieces of an image, and it will cache the pieces.

Version:
1.0, Oct 4, 1998
Author:
Steve Yegge

Field Summary
protected  IntHashtable cache_
          This table caches Images; the keys are the tile numbers for the images (from the tile registry).
protected  java.util.Hashtable images_
          Maps image names to tile numbers.
protected static ClientImageCache instance_
           
protected  wyvern.common.util.LinkedQueue itemList_
           
protected  int maxSize_
           
protected  int QMARK
           
static int TILE_WIDTH
           
protected  IntHashtable tiles_
          Maps tile numbers to image names.
 
Constructor Summary
protected ClientImageCache()
          Prevent instantiation.
 
Method Summary
static void addTileNumber(int tile, java.lang.String path)
          Called by client when an image/tile mapping comes in.
static void clear()
          Clears all the images and mappings - used when Client disconnects, so connecting to a new server doesn't dork the artwork.
static java.awt.Image extractImage(int tile, int x, int y)
          Loads (and caches) a piece of an image given its tile number and (x,y) offsets.
protected  java.awt.Image extractPiece(java.awt.Image img, int xoffset, int yoffset)
          Returns a 32x32 piece of the specified image, at the requested relative x & y offsets.
protected static java.awt.Image extractSpecial(java.awt.Image img, int x, int y)
          Extracts the bottom half of a 1x2 image - the CropImageFilter seems to choke on this, as of JDK1.2beta4 (it returns top half).
static java.awt.Image getImage(int tile)
          Loads an image from its tile number.
 java.lang.String getImageNameFromTile(int tile)
          Looks up the image name in our cache.
static ClientImageCache getInstance()
           
protected  java.awt.Image iextractImage(int tile, int x, int y)
          Loads (and caches) a piece of an image given its tile number and (x,y) offsets.
protected  java.awt.Image igetImage(int tile)
          Maps a tile ID to its Image.
protected  java.awt.Image loadImageResource(java.lang.String path)
          Calls Wyvern.loadImage(), but subclasses can use a different way to load the image.
static void setInitialTileMappings(IntHashtable tiles)
          Sets the initial tile/image mappings, received from the server.
static void setMaxSize(int max)
          Sets the maximum size for the cache, after which we'll start aging out images.
static void storeImage(java.lang.String path, java.awt.Image img)
          Stores an image in the cache - called by wyvern.client.core.Timestamps for the Applet version, since it can't write the image to disk.
static void uncacheImage(java.lang.String path)
          Uncaches an image by name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TILE_WIDTH

public static final int TILE_WIDTH
See Also:
Constant Field Values

instance_

protected static ClientImageCache instance_

QMARK

protected int QMARK

cache_

protected IntHashtable cache_
This table caches Images; the keys are the tile numbers for the images (from the tile registry).


tiles_

protected IntHashtable tiles_
Maps tile numbers to image names.


images_

protected java.util.Hashtable images_
Maps image names to tile numbers.


maxSize_

protected int maxSize_

itemList_

protected wyvern.common.util.LinkedQueue itemList_
Constructor Detail

ClientImageCache

protected ClientImageCache()
Prevent instantiation. This class is a Singleton.

Method Detail

getInstance

public static ClientImageCache getInstance()

getImage

public static java.awt.Image getImage(int tile)
Loads an image from its tile number.

Parameters:
tile - the database tile number
Returns:
the Image for that tile, or null if not found

extractImage

public static java.awt.Image extractImage(int tile,
                                          int x,
                                          int y)
Loads (and caches) a piece of an image given its tile number and (x,y) offsets.

The (x,y) offsets correspond to the tile location within the image to extract. For instance, if the image is 64x64, i.e. 2x2 tiles, then an (x,y) offset of (0,1) will extract and return the lower-left corner of the image.

Parameters:
tile - the TileDatabase number of the image
x - the x tile offset into the image
y - the y tile offset into the image
Returns:
the cropped 32x32 image, or null if the original image wasn't found.
Throws:
java.lang.IllegalArgumentException - if either offset is invalid

setMaxSize

public static void setMaxSize(int max)
Sets the maximum size for the cache, after which we'll start aging out images. Call this method before you start adding items to the cache.

Parameters:
max - the max number of images to keep in the cache, or -1 to make it unlimited.

igetImage

protected java.awt.Image igetImage(int tile)
Maps a tile ID to its Image.

Parameters:
tile - the tile database number
Returns:
the Image for that tile, or null if not available yet

loadImageResource

protected java.awt.Image loadImageResource(java.lang.String path)
Calls Wyvern.loadImage(), but subclasses can use a different way to load the image.

Parameters:
path - the relative path to the image, using the naming conventions required by wyvern.common.config.Wyvern.loadImage()

storeImage

public static void storeImage(java.lang.String path,
                              java.awt.Image img)
Stores an image in the cache - called by wyvern.client.core.Timestamps for the Applet version, since it can't write the image to disk. Only happens when the server sends us an image because our copy was out of date or missing from the jar file (e.g. wiz art).

Parameters:
img - the image
path - the relative image path

uncacheImage

public static void uncacheImage(java.lang.String path)
Uncaches an image by name.


getImageNameFromTile

public java.lang.String getImageNameFromTile(int tile)
Looks up the image name in our cache. If it's not available yet (hasn't been assigned by the server and passed in by the client), returns null.


iextractImage

protected java.awt.Image iextractImage(int tile,
                                       int x,
                                       int y)
Loads (and caches) a piece of an image given its tile number and (x,y) offsets.

The (x,y) offsets correspond to the tile location within the image to extract. For instance, if the image is 64x64, i.e. 2x2 tiles, then an (x,y) offset of (0,1) will extract and return the lower-left corner of the image.

Parameters:
tile - the TileDatabase number of the image
x - the x tile offset into the image
y - the y tile offset into the image
Returns:
the cropped 32x32 image, or null if the original image wasn't found.
Throws:
java.lang.IllegalArgumentException - if either offset is invalid

extractPiece

protected java.awt.Image extractPiece(java.awt.Image img,
                                      int xoffset,
                                      int yoffset)
Returns a 32x32 piece of the specified image, at the requested relative x & y offsets.

Parameters:
img - the image to crop
xoffset - the x tile-offset
yoffset - the y tile-offset

extractSpecial

protected static java.awt.Image extractSpecial(java.awt.Image img,
                                               int x,
                                               int y)
Extracts the bottom half of a 1x2 image - the CropImageFilter seems to choke on this, as of JDK1.2beta4 (it returns top half).


addTileNumber

public static void addTileNumber(int tile,
                                 java.lang.String path)
Called by client when an image/tile mapping comes in. We keep a table mapping tiles to image names.


setInitialTileMappings

public static void setInitialTileMappings(IntHashtable tiles)
Sets the initial tile/image mappings, received from the server.

Parameters:
tiles - a hashtable of Integers to Strings

clear

public static void clear()
Clears all the images and mappings - used when Client disconnects, so connecting to a new server doesn't dork the artwork.