com.miginfocom.util.gfx
Class XtdImage

java.lang.Object
  extended by com.miginfocom.util.gfx.ScaleableImage
      extended by com.miginfocom.util.gfx.XtdImage
All Implemented Interfaces:
java.awt.Transparency, java.io.Serializable, javax.swing.Icon

public class XtdImage
extends ScaleableImage
implements java.io.Serializable

A implmentation of an image (aggregated) that can be realized in many ways. Mainly based around GfxUtil.getImageFromString(String, StringBuffer, Integer) but also optionally caches scaled images and implements Icon so it's easy to paint with.
Aggregates a BufferedImage and saves how the image was created for serialization.
Also caches a number if scaled instances for fast blitting.

See Also:
MRUSoftCacheMap, Serialized Form

Field Summary
static boolean produceReport
          A public flag that indicates wether the image should produce a reprot of the loading of the image.
static java.util.HashMap SCALE_QUALITY_HINT_MAP
          Maps scale quality used by this class to Java2D RenderingHints.
 
Fields inherited from interface java.awt.Transparency
BITMASK, OPAQUE, TRANSLUCENT
 
Constructor Summary
XtdImage(java.awt.image.BufferedImage image, int cacheScaledImages)
          Constructor for a opaque image and no background thread.
XtdImage(java.awt.image.BufferedImage image, int cacheScaledImages, java.lang.Integer forceTransparency)
          Constructor.
XtdImage(java.awt.image.BufferedImage image, int cacheScaledImages, java.lang.Integer forceTransparency, java.lang.Integer scaleQuality)
          Constructor.
XtdImage(java.lang.String imageContext)
          Constructor for a opaque image and no background thread and no cached scaled images.
XtdImage(java.lang.String imageContext, int cacheScaledImages)
          Constructor for a opaque image and no background thread.
XtdImage(java.lang.String imageContext, int cacheScaledImages, java.lang.Integer forceTransparency)
          Constructor.
XtdImage(java.lang.String imageContext, int cacheScaledImages, java.lang.Integer forceTransparency, java.lang.Integer scaleQuality)
          Constructor.
 
Method Summary
 void drawImage(java.awt.Graphics2D g, int x, int y, int width, int height)
          Paints the internal buffered image (is loaded yet) scaled into the rectangle.
 void drawImage(java.awt.Graphics2D g, java.awt.Rectangle r, PlaceRect placeRect)
           
 void drawImageTile(java.awt.Graphics2D g, java.awt.Rectangle bounds, PlaceRect oneTile)
          Used to draw the image like a texture paint but with more flexibility and much higher probability of getting an acceleraded blit since no PaintContext generalizes the image data.
 boolean equals(java.lang.Object o)
           
 void flush()
          Releases the imageContext used for creating this image.
 java.awt.image.BufferedImage getBufferedImage()
          Returns the wrapped buffered image that eventually, or already, will be created.
 int getCacheScaledImages()
          Returns the number of scaled instances if the image to cache.
 java.lang.Integer getForceTransparency()
          Returns default transparency if the image provides no info.
 int getIconHeight()
           
 int getIconWidth()
           
 java.lang.String getImageContext()
          Returns the context string used for creating this image or null if is has been flushed and there are no contained BufferedImage (null).
 java.lang.StringBuffer getImageLoadReport()
          Returns a concise report on how the image/images was loaded.
 java.awt.Image getScaledInstance(int width, int height, int hints)
          Overridden to return a scaled BufferedImage with the same transparency as the wrapped one.
 java.lang.Integer getScaleQuality()
          Returns how the image is scaled (quality wise).
 java.awt.Dimension getSize()
          Returns the size of the image or null if not loaded yet or loaded with errors.
 int getTransparency()
          Returns the transparency of the image.
 void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
           
 java.lang.String toString()
           
 
Methods inherited from class com.miginfocom.util.gfx.ScaleableImage
drawImage
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SCALE_QUALITY_HINT_MAP

public static final java.util.HashMap SCALE_QUALITY_HINT_MAP
Maps scale quality used by this class to Java2D RenderingHints. New values can be added and old values can be changed. Maps an Integer to a RenderingHints.KEY_INTERPOLATION value.


produceReport

public static final transient boolean produceReport
A public flag that indicates wether the image should produce a reprot of the loading of the image. Default is false. Set to true to get a post load report.

See Also:
getImageLoadReport(), produceReport, Constant Field Values
Constructor Detail

XtdImage

public XtdImage(java.lang.String imageContext)
Constructor for a opaque image and no background thread and no cached scaled images.

Parameters:
imageContext - Where to fins the image. Filename can be absolute or relative to execution folder. Also accepts URL strings. If the string is a BASE64 encoded image of some supported (by ImageIO that is) kind it will be decoded and created as an image.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String)

XtdImage

public XtdImage(java.lang.String imageContext,
                int cacheScaledImages)
Constructor for a opaque image and no background thread.

Parameters:
imageContext - Where to fins the image. Filename can be absolute or relative to execution folder. Also accepts URL strings. If the string is a BASE64 encoded image of some supported (by ImageIO that is) kind it will be decoded and created as an image.
cacheScaledImages - The number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String)

XtdImage

public XtdImage(java.lang.String imageContext,
                int cacheScaledImages,
                java.lang.Integer forceTransparency)
Constructor.

Parameters:
imageContext - Where to fins the image. Filename can be absolute or relative to execution folder. Also accepts URL strings. If the string is a BASE64 encoded image of some supported (by ImageIO that is) kind it will be decoded and created as an image.
cacheScaledImages - The number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.
forceTransparency - Default transparency if the image provides no info. E.g. Transparency.BITMASK. If null the transparency is read from the loaded image.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String)

XtdImage

public XtdImage(java.lang.String imageContext,
                int cacheScaledImages,
                java.lang.Integer forceTransparency,
                java.lang.Integer scaleQuality)
Constructor.

Parameters:
imageContext - Where to fins the image. Filename can be absolute or relative to execution folder. Also accepts URL strings. If the string is a BASE64 encoded image of some supported (by ImageIO that is) kind it will be decoded and created as an image.
cacheScaledImages - The number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.
forceTransparency - Default transparency if the image provides no info. E.g. Transparency.BITMASK. If null the transparency is read from the loaded image.
scaleQuality - How the image is scaled (quality wise). Currently:
0 maps to RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR.
1 maps to RenderingHints.VALUE_INTERPOLATION_BILINEAR.
2 maps to RenderingHints.VALUE_INTERPOLATION_BICUBIC.
If null the hint is not changed and uses the hint sent in when painting.
The reson these hints arenšt used directly is because they aren't serializeable.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String), SCALE_QUALITY_HINT_MAP

XtdImage

public XtdImage(java.awt.image.BufferedImage image,
                int cacheScaledImages)
Constructor for a opaque image and no background thread.

Parameters:
image - The image that this class should Wrap. Not null. This class' XML serialisation delegate will write this image to an .png and convert that to BASE64 for serialisation.
cacheScaledImages - The number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String), GfxUtil.getBufferedImage(java.lang.Object, int)

XtdImage

public XtdImage(java.awt.image.BufferedImage image,
                int cacheScaledImages,
                java.lang.Integer forceTransparency)
Constructor.

Parameters:
image - The image that this class should Wrap. Not null. This class' XML serialisation delegate will write this image to an .png and convert that to BASE64 for serialisation.
cacheScaledImages - The number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.
forceTransparency - Default transparency if the image provides no info. E.g. Transparency.BITMASK. If null the transparency is read from the loaded image.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String), GfxUtil.getBufferedImage(java.lang.Object, int)

XtdImage

public XtdImage(java.awt.image.BufferedImage image,
                int cacheScaledImages,
                java.lang.Integer forceTransparency,
                java.lang.Integer scaleQuality)
Constructor.

Parameters:
image - The image that this class should Wrap. This class' XML serialisation delegate will write this image to an .png and convert that to BASE64 for serialisation.
cacheScaledImages - The number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.
forceTransparency - Default transparency if the image provides no info. E.g. Transparency.BITMASK. If null the transparency is read from the loaded image.
scaleQuality - How the image is scaled (quality wise). Currently:
0 maps to RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR.
1 maps to RenderingHints.VALUE_INTERPOLATION_BILINEAR.
2 maps to RenderingHints.VALUE_INTERPOLATION_BICUBIC.
If null the hint is not changed and uses the hint sent in when painting.
The reson these hints aren't used directly is because they aren't serializeable.
See Also:
GfxUtil.getImageFromString(String, StringBuffer, Integer), GfxUtil.repaintAllFrames(), IOUtil.isProbableURL(java.lang.String), IOUtil.isProbableBase64(String), SCALE_QUALITY_HINT_MAP, GfxUtil.getBufferedImage(java.lang.Object, int)
Method Detail

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage()
Returns the wrapped buffered image that eventually, or already, will be created. When the image is loaded this buffered image will be the result.

Note! The returned image should NOT be altered since it might be cached locally in other sizes, and changing it might put it in a non-accelerated mode.

Returns:
A buffered image or null if not loaded yet.

getImageContext

public java.lang.String getImageContext()
Returns the context string used for creating this image or null if is has been flushed and there are no contained BufferedImage (null). If the XtdImage was created with a BufferedImage or the imageContext has been flushed with flush() the image context will be recreated as a BASE64 encoded string of a .png image and returned.

Returns:
The context string used for creating this image or null if is has been flushed.
See Also:
flush(), GfxUtil.imageToBase64(java.awt.Image, String)

getCacheScaledImages

public int getCacheScaledImages()
Returns the number of scaled instances if the image to cache. This is to prevent "scaling on the fly" and to increase performance, but it consumes more memory.

Returns:
The number of scaled instances if the image to cache.

getForceTransparency

public java.lang.Integer getForceTransparency()
Returns default transparency if the image provides no info. E.g. Transparency.BITMASK.

Returns:
The default transparency if the image provides no info. null means that the image of default value (GfxUtil.defaultImageTransparency should be used.

getScaleQuality

public java.lang.Integer getScaleQuality()
Returns how the image is scaled (quality wise). Currently:
0 maps to RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR.
1 maps to RenderingHints.VALUE_INTERPOLATION_BILINEAR.
2 maps to RenderingHints.VALUE_INTERPOLATION_BICUBIC.
If null the hint not changed and uses the hint sent in when painting.
The reson these hints arenšt used directly is because they aren't serializeable.

Returns:
How the image is scaled

getTransparency

public int getTransparency()
Returns the transparency of the image. If the image hasn't been loaded yet, or was loaded with an error this will return the same value as getForceTransparency(). After loading the transparency will change if it is optainable from the image.

Specified by:
getTransparency in interface java.awt.Transparency
Returns:
The transparency of the image.

getImageLoadReport

public java.lang.StringBuffer getImageLoadReport()
Returns a concise report on how the image/images was loaded.

Returns:
A concise report on how the image/images was loaded. Never null.

getSize

public java.awt.Dimension getSize()
Returns the size of the image or null if not loaded yet or loaded with errors.

Returns:
The size of the image

getScaledInstance

public java.awt.Image getScaledInstance(int width,
                                        int height,
                                        int hints)
Overridden to return a scaled BufferedImage with the same transparency as the wrapped one. One scaled image is cached so getting the same one again will just return that one directly. It should NOT be changed!

Parameters:
width - The width of the new image. -1 means same as the wrapped image's width.
height - The height of the new image -1 means same as the wrapped image's height.
hints - Disregarded since specified in the constructor.
Returns:
A buffered Image. null if the image hasn't been loaded yet. Or the resulting width or height is less than or equals 0.
See Also:
SCALE_QUALITY_HINT_MAP

drawImage

public void drawImage(java.awt.Graphics2D g,
                      int x,
                      int y,
                      int width,
                      int height)
Description copied from class: ScaleableImage
Paints the internal buffered image (is loaded yet) scaled into the rectangle. One cached image will be retained and used if the same width/height is used again. If the image hasn't been loaded yet nothing is drawn.

Specified by:
drawImage in class ScaleableImage
Parameters:
g - The Graphics object to draw in
x - Upper left corner.
y - Upper left corner.
width - The width. If -1 the image's width is used.
height - The height. If -1 the image's height is used.
See Also:
Icon.paintIcon(java.awt.Component, java.awt.Graphics, int, int)

drawImage

public void drawImage(java.awt.Graphics2D g,
                      java.awt.Rectangle r,
                      PlaceRect placeRect)

drawImageTile

public void drawImageTile(java.awt.Graphics2D g,
                          java.awt.Rectangle bounds,
                          PlaceRect oneTile)
Used to draw the image like a texture paint but with more flexibility and much higher probability of getting an acceleraded blit since no PaintContext generalizes the image data.

Parameters:
g - The object to draw in.
bounds - The bounds of the area to paint in. Never paints outside.
oneTile - The location of one tile, the rest will be adjacent, before and after both vertically and horizontally, to this one.

paintIcon

public void paintIcon(java.awt.Component c,
                      java.awt.Graphics g,
                      int x,
                      int y)
Specified by:
paintIcon in interface javax.swing.Icon

getIconWidth

public int getIconWidth()
Specified by:
getIconWidth in interface javax.swing.Icon

getIconHeight

public int getIconHeight()
Specified by:
getIconHeight in interface javax.swing.Icon

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

flush

public void flush()
Releases the imageContext used for creating this image. If this is done the image can no longer be serialized (saved) since only the pixel data is left.
Also calls flush in the wrapped image if it is created and released any cached scaled images in the cache.


equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object


Copyright © 2009 MiG InfoCom AB. All Rights Reserved.