Class ImageUtils


  • public class ImageUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.Image changeColor​(java.awt.Image image, java.awt.Color oldColor, java.awt.Color newColor)
      Creates a new image that is the same as the given image but has the given colored pixels replaced with the given new color
      static java.awt.Image createDisabledImage​(java.awt.Image image, int brightnessPercent)
      Creates a disabled version of the given image.
      static java.awt.image.BufferedImage createEmptyImage​(int width, int height)
      Creates a new image of the given size.
      static java.awt.Image createImage​(java.awt.Component c)
      Creates an image of the given component
      static java.awt.Image createScaledImage​(java.awt.Image image, int width, int height, int hints)
      Creates a scaled image based upon the given image.
      static java.awt.Image crop​(java.awt.Image i, java.awt.Rectangle bounds)
      Crops the given image, keeping the given bounds
      static java.awt.image.BufferedImage getBufferedImage​(java.awt.Image image)
      Copies this image into a buffered image.
      static javax.swing.Icon makeTransparent​(javax.swing.Icon icon)
      Make the specified icon semi-transparent using the default transparency alpha
      static javax.swing.Icon makeTransparent​(javax.swing.Icon icon, float alpha)
      Make the specified icon semi-transparent using the specified transparency alpha
      static java.awt.Image padImage​(java.awt.Image i, java.awt.Color c, int top, int left, int right, int bottom)
      Pads the given image with space in the amount given.
      static java.awt.Image placeImagesSideBySide​(java.awt.Image left, java.awt.Image right)
      Places the two given images side-by-side into a new image.
      static java.awt.image.BufferedImage readFile​(java.io.File imageFile)
      Load an image from a file
      static java.awt.image.RenderedImage toRenderedImage​(java.awt.Image image)
      Turns the given image into a RenderedImage
      static boolean waitForImage​(java.lang.String imageName, java.awt.Image image)
      Waits a reasonable amount of time for the given image to load
      static void writeFile​(java.awt.image.RenderedImage i, java.io.File imageFile)
      Write the specified image to file in PNG format
      static void writeFile​(java.awt.Image i, java.io.File imageFile)
      Write the specified image to file in PNG format
      static void writeIconToPNG​(javax.swing.Icon icon, java.lang.String filename)
      Writes the given icon out to the file denoted by filename in the PNG format.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • createImage

        public static java.awt.Image createImage​(java.awt.Component c)
        Creates an image of the given component
        Parameters:
        c - the component
        Returns:
        the image
      • padImage

        public static java.awt.Image padImage​(java.awt.Image i,
                                              java.awt.Color c,
                                              int top,
                                              int left,
                                              int right,
                                              int bottom)
        Pads the given image with space in the amount given.
        Parameters:
        i - the image to pad
        c - the color to use for the padding background
        top - the top padding
        left - the left padding
        right - the right padding
        bottom - the bottom padding
        Returns:
        a new image with the given image centered inside of padding
      • crop

        public static java.awt.Image crop​(java.awt.Image i,
                                          java.awt.Rectangle bounds)
        Crops the given image, keeping the given bounds
        Parameters:
        i - the image to crop
        bounds - the new bounds
        Returns:
        a new image based on the given image, cropped to the given bounds.
      • createEmptyImage

        public static java.awt.image.BufferedImage createEmptyImage​(int width,
                                                                    int height)
        Creates a new image of the given size. This image is suitable for drawing operations.
        Parameters:
        width - the width of the new image
        height - the height of the new image
        Returns:
        a new image of the given size. This image is suitable for drawing operations.
      • placeImagesSideBySide

        public static java.awt.Image placeImagesSideBySide​(java.awt.Image left,
                                                           java.awt.Image right)
        Places the two given images side-by-side into a new image.
        Parameters:
        left - the left image
        right - the right image
        Returns:
        a new image with the two given images side-by-side into a new image.
      • toRenderedImage

        public static java.awt.image.RenderedImage toRenderedImage​(java.awt.Image image)
        Turns the given image into a RenderedImage
        Parameters:
        image - the image
        Returns:
        the rendered image
      • getBufferedImage

        public static java.awt.image.BufferedImage getBufferedImage​(java.awt.Image image)
        Copies this image into a buffered image. If this image is already a buffered image, then it will be returned.
        Parameters:
        image - the image
        Returns:
        the buffered image
      • waitForImage

        public static boolean waitForImage​(java.lang.String imageName,
                                           java.awt.Image image)
        Waits a reasonable amount of time for the given image to load
        Parameters:
        imageName - the name of the image
        image - the image for which to wait
        Returns:
        true if the wait was successful
      • writeFile

        public static void writeFile​(java.awt.Image i,
                                     java.io.File imageFile)
                              throws java.io.IOException
        Write the specified image to file in PNG format
        Parameters:
        i - the image to save
        imageFile - the file to save the image to
        Throws:
        java.io.IOException
      • writeFile

        public static void writeFile​(java.awt.image.RenderedImage i,
                                     java.io.File imageFile)
                              throws java.io.IOException
        Write the specified image to file in PNG format
        Parameters:
        i - the image to save
        imageFile - the file to save the image to
        Throws:
        java.io.IOException
      • readFile

        public static java.awt.image.BufferedImage readFile​(java.io.File imageFile)
                                                     throws java.io.IOException
        Load an image from a file
        Parameters:
        imageFile - image source-data file
        Returns:
        the image, decoded from bytes in specified file
        Throws:
        java.io.IOException
      • writeIconToPNG

        public static void writeIconToPNG​(javax.swing.Icon icon,
                                          java.lang.String filename)
                                   throws java.io.IOException
        Writes the given icon out to the file denoted by filename in the PNG format.
        Parameters:
        icon - the icon to write
        filename - the filename denoting the write destination
        Throws:
        java.io.IOException - see ImageIO.write(RenderedImage, String, File)
      • makeTransparent

        public static javax.swing.Icon makeTransparent​(javax.swing.Icon icon)
        Make the specified icon semi-transparent using the default transparency alpha
        Parameters:
        icon - The icon to make semi-transparent
        Returns:
        a new icon, based on the original, made semi-transparent
        See Also:
        DEFAULT_TRANSPARENCY_ALPHA
      • makeTransparent

        public static javax.swing.Icon makeTransparent​(javax.swing.Icon icon,
                                                       float alpha)
        Make the specified icon semi-transparent using the specified transparency alpha
        Parameters:
        icon - the icon to make semi-transparent
        alpha - the alpha value to use in making the icon transparent
        Returns:
        a new icon, based on the original, made semi-transparent
      • createScaledImage

        public static java.awt.Image createScaledImage​(java.awt.Image image,
                                                       int width,
                                                       int height,
                                                       int hints)
        Creates a scaled image based upon the given image. NOTE: Avoid invocation by a static initializer.
        Parameters:
        image - the image to scale
        width - the new width
        height - the new height
        hints - RenderingHints used by Graphics2D
        Returns:
        a scaled version of the given image
      • createDisabledImage

        public static java.awt.Image createDisabledImage​(java.awt.Image image,
                                                         int brightnessPercent)
        Creates a disabled version of the given image. The disabled version will be grayed and have the varying gray levels blended together.
        Parameters:
        image - the image to disable
        brightnessPercent - the amount of brightness to apply; 0-100
        Returns:
        the new image
      • changeColor

        public static java.awt.Image changeColor​(java.awt.Image image,
                                                 java.awt.Color oldColor,
                                                 java.awt.Color newColor)
        Creates a new image that is the same as the given image but has the given colored pixels replaced with the given new color
        Parameters:
        image - the image to change
        oldColor - the color to replace
        newColor - the color to use
        Returns:
        the new image