Class ImageUtils


  • public class ImageUtils
    extends java.lang.Object
    Collection of static image manipulation utilities. Only generic routines belong here, anything specific to FreeCol belongs in ImageLibrary or ImageResource.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage createBufferedImage​(java.awt.Image image)
      Creates a buffered image from an input Image.
      static java.awt.image.BufferedImage createCenteredImage​(java.awt.image.BufferedImage image, int width, int height)
      Creates a new image of the given size with the provided image centered.
      static java.awt.image.BufferedImage createCenteredImage​(java.awt.image.BufferedImage image, java.awt.Dimension size)
      Creates a new image of the given size with the provided image centered.
      static java.awt.image.BufferedImage createGrayscaleImage​(java.awt.Image image)
      Create a grayscale buffered image version of an input Image.
      static java.awt.image.BufferedImage createHalvedImage​(java.awt.Image image)
      Create a buffered image that is roughly half the size in both dimensions of an input Image.
      static java.awt.image.BufferedImage createMirroredImage​(java.awt.Image image)
      Create a buffered image that mirrors an input Image.
      static java.awt.image.BufferedImage createResizedImage​(java.awt.Image image, int width, int height)
      Create a buffered image resized from a plain Image.
      static java.awt.image.BufferedImage createResizedImage​(java.awt.Image image, int width, int height, boolean interpolation)
      Create a buffered image resized from a plain Image.
      static void drawTiledImage​(java.awt.image.BufferedImage image, java.awt.Graphics g, javax.swing.JComponent c, java.awt.Insets insets)
      Draw a (usually small) background image into a (usually larger) space specified by a component, tiling the image to fill up the space.
      static java.awt.image.BufferedImage fadeImage​(java.awt.Image img, float fade, float target)
      Create a faded version of an image.
      static void fillTexture​(java.awt.Graphics2D g2d, java.awt.image.BufferedImage img, int x, int y, int width, int height)
      Fill a rectangle with a texture image.
      static java.awt.image.BufferedImage imageWithAlphaFromMask​(java.awt.image.BufferedImage image, java.awt.image.BufferedImage mask)
      Returns the result where the given mask has been applied to the given image.
      static java.awt.Dimension wildcardDimension​(java.awt.Dimension d, java.awt.Dimension nominal)
      Given a dimension with potential wildcard (non-positive) parts, and a nominal dimension, use the nominal dimension to remove any wildcard parts.
      • Methods inherited from class java.lang.Object

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

      • ImageUtils

        public ImageUtils()
    • Method Detail

      • createBufferedImage

        public static java.awt.image.BufferedImage createBufferedImage​(java.awt.Image image)
        Creates a buffered image from an input Image.
        Parameters:
        image - The Image.
        Returns:
        The new BufferedImage.
      • createGrayscaleImage

        public static java.awt.image.BufferedImage createGrayscaleImage​(java.awt.Image image)
        Create a grayscale buffered image version of an input Image.
        Parameters:
        image - The Image.
        Returns:
        The new halved BufferedImage.
      • createHalvedImage

        public static java.awt.image.BufferedImage createHalvedImage​(java.awt.Image image)
        Create a buffered image that is roughly half the size in both dimensions of an input Image. Note: perhaps this should be createQuarteredImage?
        Parameters:
        image - The Image.
        Returns:
        The new halved BufferedImage.
      • createMirroredImage

        public static java.awt.image.BufferedImage createMirroredImage​(java.awt.Image image)
        Create a buffered image that mirrors an input Image.
        Parameters:
        image - The Image object.
        Returns:
        The new mirrored BufferedImage object.
      • createResizedImage

        public static java.awt.image.BufferedImage createResizedImage​(java.awt.Image image,
                                                                      int width,
                                                                      int height)
        Create a buffered image resized from a plain Image. Note that while the result will fit in width x height, it tends to retain its original aspect ratio.
        Parameters:
        image - The Image object.
        width - The desired width.
        height - The desired height.
        Returns:
        The new resized BufferedImage object.
      • createResizedImage

        public static java.awt.image.BufferedImage createResizedImage​(java.awt.Image image,
                                                                      int width,
                                                                      int height,
                                                                      boolean interpolation)
        Create a buffered image resized from a plain Image. Note that while the result will fit in width x height, it tends to retain its original aspect ratio.
        Parameters:
        image - The Image object.
        width - The desired width.
        height - The desired height.
        interpolation - Use interpolation while resizing.
        Returns:
        The new resized BufferedImage object.
      • drawTiledImage

        public static void drawTiledImage​(java.awt.image.BufferedImage image,
                                          java.awt.Graphics g,
                                          javax.swing.JComponent c,
                                          java.awt.Insets insets)
        Draw a (usually small) background image into a (usually larger) space specified by a component, tiling the image to fill up the space.
        Parameters:
        image - A BufferedImage to tile with (null fills with background color).
        g - The Graphics to draw to.
        c - The JComponent that defines the space.
        insets - Optional Insets to apply.
      • fillTexture

        public static void fillTexture​(java.awt.Graphics2D g2d,
                                       java.awt.image.BufferedImage img,
                                       int x,
                                       int y,
                                       int width,
                                       int height)
        Fill a rectangle with a texture image.
        Parameters:
        g2d - The Graphics2D used for painting the border.
        img - The BufferedImage to fill the texture.
        x - The x-component of the offset to start filling at.
        y - The y-component of the offset to start filling at.
        width - The width of the rectangle.
        height - The height of the rectangle.
      • createCenteredImage

        public static java.awt.image.BufferedImage createCenteredImage​(java.awt.image.BufferedImage image,
                                                                       java.awt.Dimension size)
        Creates a new image of the given size with the provided image centered.
        Parameters:
        image - The image to be drawn in the center (both vertically and horizontally) of the new image.
        size - The size of the new image.
        Returns:
        A new image.
      • createCenteredImage

        public static java.awt.image.BufferedImage createCenteredImage​(java.awt.image.BufferedImage image,
                                                                       int width,
                                                                       int height)
        Creates a new image of the given size with the provided image centered.
        Parameters:
        image - The image to be drawn in the center (both vertically and horizontally) of the new image.
        width - The width of the new image.
        height - The height of the new image.
        Returns:
        A new image.
      • wildcardDimension

        public static java.awt.Dimension wildcardDimension​(java.awt.Dimension d,
                                                           java.awt.Dimension nominal)
        Given a dimension with potential wildcard (non-positive) parts, and a nominal dimension, use the nominal dimension to remove any wildcard parts.
        Parameters:
        d - The Dimension to consider.
        nominal - The nominal Dimension.
        Returns:
        The conformning wildcard-free Dimension.
      • fadeImage

        public static java.awt.image.BufferedImage fadeImage​(java.awt.Image img,
                                                             float fade,
                                                             float target)
        Create a faded version of an image.
        Parameters:
        img - The Image to fade.
        fade - The amount of fading.
        target - The offset.
        Returns:
        The faded image.
      • imageWithAlphaFromMask

        public static java.awt.image.BufferedImage imageWithAlphaFromMask​(java.awt.image.BufferedImage image,
                                                                          java.awt.image.BufferedImage mask)
        Returns the result where the given mask has been applied to the given image.
        Parameters:
        image - The image.
        mask - The mask. Only the alpha channel from the mask is used.
        Returns:
        An image with the opacity from the mask.