Task.Support.GUISupport
Class ImageUtils

java.lang.Object
  extended by Task.Support.GUISupport.ImageUtils

public class ImageUtils
extends java.lang.Object

ImageUtils allows icons stored in jar files on the classpaths to be loaded as ImageIcon objects and caches them

Since:
Jan 3, 2007, 2:06:54 PM
Author:
Nazmul Idris

Field Summary
static java.util.HashMap<java.lang.String,java.awt.image.BufferedImage> bufferedImageCache
           
static java.util.concurrent.locks.ReadWriteLock bufferedImageCacheLock
           
 
Constructor Summary
ImageUtils()
           
 
Method Summary
 java.awt.image.BufferedImage createBufferedImage(java.awt.Image image)
          Utility method provided to convert Images to BufferedImages.
static java.awt.image.BufferedImage createCompatibleImage(int width, int height, boolean opaque)
          convenience method for creating an opaque or transparent compatible BufferedImage
static java.awt.image.BufferedImage createCompatibleImage(int width, int height, int transparency)
          convenience constructor for creating a compatible BufferedImage given the Transparency setting
static java.awt.image.BufferedImage createFastResizedCopy(java.awt.image.BufferedImage originalImage, int scaledWidth, int scaledHeight, boolean keepAspectRatio)
          Resizes the originalImage to fit the scaled Width and Height.
static java.awt.image.BufferedImage createHighResThumbnail(java.awt.image.BufferedImage image, int newWidth, int newHeight, boolean keepAspectRatio)
          This creates a high quality thumbnail of the given image.
static java.awt.image.BufferedImage decodeBufferedImage(ZipObject<ByteBuffer> zipObject)
          Decodes the PNG-encoded-ByteBuffer into a BufferedImage.
static ZipObject<ByteBuffer> encodeBufferedImage(java.awt.image.BufferedImage img)
          Encodes the BufferedImage into a PNG file and writes that to a ByteBuffer.
static java.awt.Graphics2D g2(java.awt.image.BufferedImage img)
           
static java.awt.Graphics2D g2(java.awt.Graphics g)
           
static java.awt.Graphics2D g2(java.awt.SplashScreen img)
           
static java.awt.Graphics2D g2plain(java.awt.Graphics g)
           
static java.awt.image.BufferedImage getCopyOfBufferedImage(java.awt.image.BufferedImage img)
          simply returns a copy of the given BufferedImage
static java.awt.image.BufferedImage getCopyOfBufferedImage(java.awt.image.BufferedImage img, boolean opaque)
          returns a copy of the given buffered image with preferred transparency setting (regardless of whether the orginal BufferedImage has any transparency in it or not.
static java.awt.image.BufferedImage getImageOfComponent(javax.swing.JComponent comp)
           
static java.awt.image.BufferedImage getImageOfComponent(javax.swing.JComponent comp, boolean opaque)
           
static java.awt.image.BufferedImage getScaledBufferedImage(java.awt.image.BufferedImage img, int targetWidth, int targetHeight)
          This returns a scaled version of the original icon image.
static java.awt.image.BufferedImage getScaledBufferedImage(java.awt.image.BufferedImage img, int targetWidth, int targetHeight, boolean blur, float translucency)
          This returns a scaled version of the original icon image.
static java.awt.image.BufferedImage loadBufferedImage(java.lang.String imageName, boolean blur, float translucency)
          Tries to load the .png file from the classpath (should be in the JAR files for the app) or the cache (if it's avaialble in there).
static javax.swing.Icon loadScaledBufferedIcon(java.lang.String icon, int width, int height, boolean blur, float transparency)
           
static java.awt.Dimension scaleToFit(int sourceW, int sourceH, int targetW, int targetH)
          uses scaleToFit(int,int,int,int,boolean) to figure out the final width/height of the source image that should be fit in the target width/height provided, while maintaining the aspect ration of the source image.
static float scaleToFit(int sourceW, int sourceH, int targetW, int targetH, boolean respectSourceAspectRatio)
          This method gets a scaling factor that you can apply to a source image to make it fit the bounds of the target W/H, while maintaining the aspect ratio.
static java.awt.image.BufferedImage toCompatibleImage(java.awt.image.BufferedImage image)
          returns a copy of a non-compatible BufferedImage as a compatible one
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bufferedImageCache

public static java.util.HashMap<java.lang.String,java.awt.image.BufferedImage> bufferedImageCache

bufferedImageCacheLock

public static java.util.concurrent.locks.ReadWriteLock bufferedImageCacheLock
Constructor Detail

ImageUtils

public ImageUtils()
Method Detail

g2

public static java.awt.Graphics2D g2(java.awt.Graphics g)

g2plain

public static java.awt.Graphics2D g2plain(java.awt.Graphics g)

g2

public static java.awt.Graphics2D g2(java.awt.image.BufferedImage img)

g2

public static java.awt.Graphics2D g2(java.awt.SplashScreen img)

encodeBufferedImage

public static ZipObject<ByteBuffer> encodeBufferedImage(java.awt.image.BufferedImage img)
                                                 throws java.lang.IllegalArgumentException,
                                                        java.io.IOException,
                                                        ZipObjectException
Encodes the BufferedImage into a PNG file and writes that to a ByteBuffer.

Parameters:
img - source bufferedimage
Returns:
PNG encoded bytebuffer
Throws:
java.io.IOException - if there is a problem with encoding the image to PNG, this exception is thrown
java.lang.IllegalArgumentException
ZipObjectException
See Also:
decodeBufferedImage(ZipObject)

decodeBufferedImage

public static java.awt.image.BufferedImage decodeBufferedImage(ZipObject<ByteBuffer> zipObject)
                                                        throws java.lang.IllegalArgumentException,
                                                               java.io.IOException,
                                                               ZipObjectException
Decodes the PNG-encoded-ByteBuffer into a BufferedImage.

Parameters:
zipObject - PNG encoded bytebuffer
Returns:
BufferedImage
Throws:
java.io.IOException - if there is a problem with decoding the image from PNG, this exception is thrown
java.lang.IllegalArgumentException
ZipObjectException
See Also:
encodeBufferedImage(BufferedImage)

scaleToFit

public static float scaleToFit(int sourceW,
                               int sourceH,
                               int targetW,
                               int targetH,
                               boolean respectSourceAspectRatio)
This method gets a scaling factor that you can apply to a source image to make it fit the bounds of the target W/H, while maintaining the aspect ratio. If you do not need to maintain aspect ratio, then just scale the image to the target W/H, you do not need to use this method!


scaleToFit

public static java.awt.Dimension scaleToFit(int sourceW,
                                            int sourceH,
                                            int targetW,
                                            int targetH)
uses scaleToFit(int,int,int,int,boolean) to figure out the final width/height of the source image that should be fit in the target width/height provided, while maintaining the aspect ration of the source image.


getImageOfComponent

public static java.awt.image.BufferedImage getImageOfComponent(javax.swing.JComponent comp)

getImageOfComponent

public static java.awt.image.BufferedImage getImageOfComponent(javax.swing.JComponent comp,
                                                               boolean opaque)

toCompatibleImage

public static java.awt.image.BufferedImage toCompatibleImage(java.awt.image.BufferedImage image)
returns a copy of a non-compatible BufferedImage as a compatible one


createCompatibleImage

public static java.awt.image.BufferedImage createCompatibleImage(int width,
                                                                 int height,
                                                                 boolean opaque)
convenience method for creating an opaque or transparent compatible BufferedImage

Parameters:
opaque - true = opaque, false = transparent

createCompatibleImage

public static java.awt.image.BufferedImage createCompatibleImage(int width,
                                                                 int height,
                                                                 int transparency)
convenience constructor for creating a compatible BufferedImage given the Transparency setting


getCopyOfBufferedImage

public static java.awt.image.BufferedImage getCopyOfBufferedImage(java.awt.image.BufferedImage img,
                                                                  boolean opaque)
returns a copy of the given buffered image with preferred transparency setting (regardless of whether the orginal BufferedImage has any transparency in it or not.


getCopyOfBufferedImage

public static java.awt.image.BufferedImage getCopyOfBufferedImage(java.awt.image.BufferedImage img)
simply returns a copy of the given BufferedImage


createBufferedImage

public java.awt.image.BufferedImage createBufferedImage(java.awt.Image image)
Utility method provided to convert Images to BufferedImages.


loadBufferedImage

public static java.awt.image.BufferedImage loadBufferedImage(java.lang.String imageName,
                                                             boolean blur,
                                                             float translucency)
                                                      throws java.lang.ClassNotFoundException,
                                                             java.lang.IllegalArgumentException
Tries to load the .png file from the classpath (should be in the JAR files for the app) or the cache (if it's avaialble in there). Also, this method adds the image to the cache (if it has to be loaded from the classpath).

Parameters:
imageName - name of image file.
Throws:
java.lang.ClassNotFoundException - if the imageName.png file could not be found
java.lang.IllegalArgumentException - if the imageName is empty or null

getScaledBufferedImage

public static java.awt.image.BufferedImage getScaledBufferedImage(java.awt.image.BufferedImage img,
                                                                  int targetWidth,
                                                                  int targetHeight)
                                                           throws java.lang.IllegalArgumentException
This returns a scaled version of the original icon image. Some source icons are quite large. This method scales them to the appropriate width and height that is specified in the input params.

Throws:
java.lang.IllegalArgumentException

getScaledBufferedImage

public static java.awt.image.BufferedImage getScaledBufferedImage(java.awt.image.BufferedImage img,
                                                                  int targetWidth,
                                                                  int targetHeight,
                                                                  boolean blur,
                                                                  float translucency)
                                                           throws java.lang.IllegalArgumentException
This returns a scaled version of the original icon image. Some source icons are quite large. This method scales them to the appropriate width and height that is specified in the input params.

You can specify if you want the image blurred and translucent as well.

Throws:
java.lang.IllegalArgumentException

createFastResizedCopy

public static java.awt.image.BufferedImage createFastResizedCopy(java.awt.image.BufferedImage originalImage,
                                                                 int scaledWidth,
                                                                 int scaledHeight,
                                                                 boolean keepAspectRatio)
Resizes the originalImage to fit the scaled Width and Height. It preserves the alpha channel (transparency) of the original image. This is a very fast operation, and may not produce the best looking results. Look at createHighResThumbnail(java.awt.image.BufferedImage, int, int, boolean) for details on creating high res thumbnails.

Returns:
scaled BufferedImage

createHighResThumbnail

public static java.awt.image.BufferedImage createHighResThumbnail(java.awt.image.BufferedImage image,
                                                                  int newWidth,
                                                                  int newHeight,
                                                                  boolean keepAspectRatio)
This creates a high quality thumbnail of the given image. The newWidth and newHeight must be smaller than the image's width and height, otherwise an exception is thrown.


loadScaledBufferedIcon

public static javax.swing.Icon loadScaledBufferedIcon(java.lang.String icon,
                                                      int width,
                                                      int height,
                                                      boolean blur,
                                                      float transparency)
                                               throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException


Copyright © 2011. All Rights Reserved.