Package generic.util.image
Class ImageUtils
- java.lang.Object
-
- generic.util.image.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 colorstatic 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 componentstatic 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 boundsstatic 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 alphastatic javax.swing.Icon
makeTransparent(javax.swing.Icon icon, float alpha)
Make the specified icon semi-transparent using the specified transparency alphastatic 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 filestatic java.awt.image.RenderedImage
toRenderedImage(java.awt.Image image)
Turns the given image into aRenderedImage
static boolean
waitForImage(java.lang.String imageName, java.awt.Image image)
Waits a reasonable amount of time for the given image to loadstatic void
writeFile(java.awt.image.RenderedImage i, java.io.File imageFile)
Write the specified image to file in PNG formatstatic void
writeFile(java.awt.Image i, java.io.File imageFile)
Write the specified image to file in PNG formatstatic 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.
-
-
-
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 padc
- the color to use for the padding backgroundtop
- the top paddingleft
- the left paddingright
- the right paddingbottom
- 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 cropbounds
- 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 imageheight
- 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 imageright
- 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 aRenderedImage
- 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 imageimage
- 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 saveimageFile
- 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 saveimageFile
- 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 writefilename
- the filename denoting the write destination- Throws:
java.io.IOException
- seeImageIO.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-transparentalpha
- 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 scalewidth
- the new widthheight
- the new heighthints
-RenderingHints
used byGraphics2D
- 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 disablebrightnessPercent
- 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 changeoldColor
- the color to replacenewColor
- the color to use- Returns:
- the new image
-
-