Package net.sf.freecol.common.resources
Class ImageCache
- java.lang.Object
- 
- net.sf.freecol.common.resources.ImageCache
 
- 
 public class ImageCache extends java.lang.ObjectWrapper around ResourceManager for caching image resources.
- 
- 
Field SummaryFields Modifier and Type Field Description private java.util.Map<java.lang.Long,java.awt.image.BufferedImage>cacheA cache of scaled (and possibly greyed) images.private longcacheSizeprivate static booleanDEBUG_PRINT_CACHE_SIZES_TO_STDOUTprivate static java.util.logging.Loggerloggerprivate java.util.Map<java.lang.Long,java.awt.image.BufferedImage>lowPriorityCacheprivate longlowPriorityCacheSizestatic java.lang.StringREPLACEMENT_IMAGE
 - 
Constructor SummaryConstructors Constructor Description ImageCache()Initialize this image cache.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clear this cache.voidclearLowPriorityCache()private voiddebugPrintCacheSizes()java.awt.image.BufferedImagegetCachedImage(ImageResource ir, java.awt.Dimension size, boolean grayscale, int variation)Get an image, checking first if it is in the cache.java.awt.image.BufferedImagegetCachedImageOrGenerate(java.lang.String key, java.awt.Dimension size, boolean grayscale, int variation, java.util.concurrent.Callable<java.awt.image.BufferedImage> imageCreator)static ImageResourcegetImageResource(java.lang.String key)Get an image resource by key from the resource manager.java.awt.image.BufferedImagegetScaledImage(java.lang.String key, float scale, boolean grayscale)Get the image specified by the given name, scale and grayscale.java.awt.image.BufferedImagegetSizedImage(java.lang.String key, java.awt.Dimension size, boolean grayscale)Get the image specified by the given name, size and grayscale.java.awt.image.BufferedImagegetSizedImage(java.lang.String key, java.awt.Dimension size, boolean grayscale, int seed)Get the image specified by the given name, size and grayscale.static longimageHash(java.lang.String key, java.awt.Dimension size, boolean grayscale, int variation)Hash function for images.private voidplaceImageInCache(long hashKey, java.awt.image.BufferedImage image)private voidplaceImageInLowPriorityCache(long hashKey, java.awt.image.BufferedImage image)private java.awt.image.BufferedImagesearchCaches(long cacheKey)
 
- 
- 
- 
Field Detail- 
loggerprivate static final java.util.logging.Logger logger 
 - 
DEBUG_PRINT_CACHE_SIZES_TO_STDOUTprivate static final boolean DEBUG_PRINT_CACHE_SIZES_TO_STDOUT - See Also:
- Constant Field Values
 
 - 
REPLACEMENT_IMAGEpublic static final java.lang.String REPLACEMENT_IMAGE - See Also:
- Constant Field Values
 
 - 
cacheprivate final java.util.Map<java.lang.Long,java.awt.image.BufferedImage> cache A cache of scaled (and possibly greyed) images.
 - 
lowPriorityCacheprivate final java.util.Map<java.lang.Long,java.awt.image.BufferedImage> lowPriorityCache 
 - 
cacheSizeprivate long cacheSize 
 - 
lowPriorityCacheSizeprivate long lowPriorityCacheSize 
 
- 
 - 
Method Detail- 
getImageResourcepublic static ImageResource getImageResource(java.lang.String key) Get an image resource by key from the resource manager. Fall back to a replacement image on failure. Failure to find that is fatal. Image resource failure has already been logged.- Parameters:
- key- The key to look up.
- Returns:
- The ImageResourcefound.
 
 - 
imageHashpublic static long imageHash(java.lang.String key, java.awt.Dimension size, boolean grayscale, int variation)Hash function for images. We accept the standard 32-bit int hashCode of the key, and or it with 12 bits of the width and height, 6 bits for variations and one more bit for the grayscale boolean.- Parameters:
- key- The image key.
- size- The size of the image.
- grayscale- True if grayscale.
- variation- The image variation.
- Returns:
- A unique hash of these parameters.
 
 - 
getCachedImagepublic java.awt.image.BufferedImage getCachedImage(ImageResource ir, java.awt.Dimension size, boolean grayscale, int variation) Get an image, checking first if it is in the cache. All requests for scaled images come through here, so this is where to maintain the image cache.- Parameters:
- ir- The- ImageResourceto load from.
- size- The size of the requested image. Rescaling will be performed if necessary.
- grayscale- If true return a grayscale image.
- variation- The image variation.
- Returns:
- The image identified by resource.
 
 - 
getCachedImageOrGeneratepublic java.awt.image.BufferedImage getCachedImageOrGenerate(java.lang.String key, java.awt.Dimension size, boolean grayscale, int variation, java.util.concurrent.Callable<java.awt.image.BufferedImage> imageCreator)
 - 
searchCachesprivate java.awt.image.BufferedImage searchCaches(long cacheKey) 
 - 
placeImageInCacheprivate void placeImageInCache(long hashKey, java.awt.image.BufferedImage image)
 - 
placeImageInLowPriorityCacheprivate void placeImageInLowPriorityCache(long hashKey, java.awt.image.BufferedImage image)
 - 
debugPrintCacheSizesprivate void debugPrintCacheSizes() 
 - 
getScaledImagepublic java.awt.image.BufferedImage getScaledImage(java.lang.String key, float scale, boolean grayscale)Get the image specified by the given name, scale and grayscale.- Parameters:
- key- The name of the resource to return.
- scale- A scaling to apply to the base image size.
- grayscale- If true return a grayscale image.
- Returns:
- The image found.
 
 - 
getSizedImagepublic java.awt.image.BufferedImage getSizedImage(java.lang.String key, java.awt.Dimension size, boolean grayscale)Get the image specified by the given name, size and grayscale. Please, avoid using too many different sizes! For each is a scaled image cached here for a long time, which wastes memory if you are not careful.- Parameters:
- key- The name of the resource to return.
- size- The size of the requested image.
- grayscale- If true return a grayscale image.
- Returns:
- The image found.
 
 - 
getSizedImagepublic java.awt.image.BufferedImage getSizedImage(java.lang.String key, java.awt.Dimension size, boolean grayscale, int seed)Get the image specified by the given name, size and grayscale. Please, avoid using too many different sizes! For each is a scaled image cached here for a long time, which wastes memory if you are not careful.- Parameters:
- key- The name of the resource to return.
- size- The size of the requested image.
- grayscale- If true return a grayscale image.
- seed- A seed used for getting the same "random" picture every time.
- Returns:
- The image found.
 
 - 
clearpublic void clear() Clear this cache.
 - 
clearLowPriorityCachepublic void clearLowPriorityCache() 
 
- 
 
-