org.tbee.swing
Class ImageUtils

java.lang.Object
  extended by nl.knowledgeplaza.util.ImageUtils
      extended by org.tbee.swing.ImageUtils

public class ImageUtils
extends nl.knowledgeplaza.util.ImageUtils

This is a small class providing some basic and very often used image functions associated with Swing. For example the gradient image which is often used in conjunction with JPanelWithBackground.

Version:
$Revision: 1.59 $

Field Summary
static int DIAGONAL_LTBR
           
static int DIAGONAL_RTBL
           
static int HORIZONTAL
           
static java.lang.String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
static int VERTICAL
           
 
Constructor Summary
ImageUtils()
           
 
Method Summary
static java.awt.image.BufferedImage addBlur(java.awt.image.BufferedImage image)
          Add a blur over the image (using GaussianBlur).
static java.awt.image.BufferedImage addGaussianBlur(java.awt.image.BufferedImage image, double radius)
          Add a blur over the image (using linear blur).
static java.awt.image.BufferedImage addLinearBlur(java.awt.image.BufferedImage image, int blurIndex)
          Add a blur over the image (using linear blur).
static java.awt.image.BufferedImage addMargin(java.awt.image.BufferedImage image, int margin)
          This method is used to easy add a margin for blurring.
static java.awt.image.BufferedImage addNoise(java.awt.image.BufferedImage image)
          add a bit of noise to the whole image
static java.awt.image.BufferedImage addNoise(double noiseFactor, java.awt.image.BufferedImage image)
          add a bit of noise to the whole image
static java.awt.image.BufferedImage addNoise(double noiseFactor, int x, int y, int width, int height, java.awt.image.BufferedImage image)
          add a bit of noise
static java.awt.image.BufferedImage addReflectionImage(java.awt.image.BufferedImage image)
           
static java.awt.image.BufferedImage addReflectionImage(java.awt.image.BufferedImage image, float compressionFactor, int shadowOffset)
          Create a copy of the image with or without a shadow.
static void addText(java.awt.image.BufferedImage image, javax.swing.JLabel label)
          Add a contrasted text to an image
static void addText(java.awt.Graphics2D graphics2D, double x, double y, java.awt.font.TextLayout lTitle, java.awt.Color foreground, java.awt.Color background)
          Draw a contrasted text
static java.awt.image.BufferedImage brighter(java.awt.image.BufferedImage image)
           
static java.awt.image.BufferedImage brighter(java.awt.image.BufferedImage image, int times)
          make brigher Note: the image itself is altered, use createBufferedImage to work on a copy.
static java.awt.image.BufferedImage captureScreen()
          Capture the current screen
static java.awt.image.BufferedImage createBufferedImage(java.awt.Image image)
          Create a buffered image from a regular image
static java.awt.image.BufferedImage createBufferedImage(int width, int height)
          Although any BufferedImage can be painted, it is wise to created images that are compatible with the current screen device.
static java.awt.image.BufferedImage createBufferedImage(int width, int height, int transparency)
          Although any BufferedImage can be painted, it is wise to created images that are compatible with the current screen device.
static java.awt.Image createDisabled(java.awt.image.BufferedImage image)
          Return a new image that appears disabled
static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1, java.awt.Color color2, java.awt.Color color3, int direction)
           
static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1, java.awt.Color color2, java.awt.Color color3, int direction, int width, int height)
           
static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1, java.awt.Color color2, int direction)
           
static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1, java.awt.Color color2, int direction, int width, int heigth)
           
static java.awt.Image createImage(int[] pixels, int width, int height)
          Create image from pixels
static java.awt.image.BufferedImage createImageFromComponent(java.awt.Component component)
          Create a buffered image from a component
static java.awt.image.BufferedImage createImageFromIcon(javax.swing.Icon icon)
          Create a buffered image from a component
static java.awt.image.BufferedImage createJLabelImage(javax.swing.JLabel label, int margin, boolean blur)
          Deprecated. use the method which has a double blur parameter
static java.awt.image.BufferedImage createJLabelImage(javax.swing.JLabel label, int margin, double blur)
          Convert JLabel to Image
static int[] createPixels(java.awt.image.BufferedImage bufferedImage)
          Convert a buffered image into an array of rows of pixels The grabPixels method of the PixelGrabber class extracts the pixel data from an Image object into a one-dimensional array of type int.
static java.awt.image.BufferedImage createReflectionImage(java.awt.image.BufferedImage image)
           
static java.awt.image.BufferedImage createReflectionImage(java.awt.image.BufferedImage image, float compressionFactor)
          Create a copy of the image with or without a shadow.
static java.awt.image.BufferedImage createShadedImage(java.awt.image.BufferedImage image, boolean addShade)
          Create a copy of the image with or without a shadow.
static java.awt.image.BufferedImage createShadedImage(java.awt.image.BufferedImage image, int shadeOffset)
          Create a copy of the image with a shadow.
static java.awt.image.BufferedImage darker(java.awt.image.BufferedImage image)
          make darker Note: the image itself is altered, use createBufferedImage to work on a copy.
static void drawTextWithGlow(java.lang.String text, java.awt.Graphics g, java.awt.Font font, int glowSize, java.awt.Color foreground, java.awt.Color glow, float opacity)
          Draw a glowing text on an image
static java.awt.image.BufferedImage flipHorizontal(java.awt.image.BufferedImage image)
           
static java.awt.image.BufferedImage flipVertical(java.awt.image.BufferedImage image)
           
static java.awt.image.ConvolveOp getGaussianBlurOp(double stddev)
          Get a ConvolveOp that blurs
static java.util.Map<java.awt.RenderingHints.Key,java.lang.Object> getHighQualityRenderingHints()
           
static java.awt.image.ConvolveOp getLinearBlurOp(int size)
          Get a ConvolveOp that blurs
static java.awt.image.BufferedImage makeColorTransparent(java.awt.image.BufferedImage image, java.awt.Color color)
          This method erases a color form an image with a completely transparency color and thus effectively erasing it Note: the image itself is altered, use createBufferedImage to work on a copy.
static void makeGlow(int[] pixels, int width, int height)
          Add a glow to an image http://www.curious-creature.org/2007/02/20/fast-image-processing-with-jogl Does not work at this time
static java.awt.image.BufferedImage makeGray(java.awt.image.BufferedImage image)
          make gray Note: the image itself is altered, use createBufferedImage to work on a copy.
static java.awt.image.BufferedImage morph(java.awt.image.BufferedImage from, java.awt.image.BufferedImage to, double percentage)
           
static void paintReflectionImage(java.awt.image.BufferedImage canvasImage, java.awt.image.BufferedImage sourceImage, float compressionFactor, int offsetX, int offsetY)
          Paint the reflection onto the canvas
static java.awt.image.BufferedImage putInPerspective(java.awt.image.BufferedImage image)
           
static java.awt.image.BufferedImage readThumbnailMaintainRatio(java.io.File imageFile, int maxWidth, int maxHeight)
           
static java.awt.image.BufferedImage removeMargin(java.awt.image.BufferedImage image, int margin)
          This method is used to easy add a margin for blurring.
static java.awt.image.BufferedImage replaceColor(java.awt.image.BufferedImage image, java.awt.Color originalColor, java.awt.Color newColor)
          This method replace a color from an image with another color Note: the image itself is returned, use createBufferedImage to work on a copy.
static java.awt.image.BufferedImage resize(java.awt.image.BufferedImage image, double scale)
          Resize by scale
static java.awt.image.BufferedImage resize(java.awt.image.BufferedImage image, double scaleX, double scaleY)
          Resize by scale
static java.awt.image.BufferedImage resize(java.awt.image.BufferedImage image, int width, int height)
          Resize to W & H
static java.awt.image.BufferedImage rotate90(java.awt.image.BufferedImage image, boolean clockwise)
           
static java.awt.image.BufferedImage textWithGlow(java.lang.String text, java.awt.Font font, int glowSize, java.awt.Color foreground, java.awt.Color glow, float opacity)
          Create an transparent image with glowing text
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final java.lang.String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values

HORIZONTAL

public static final int HORIZONTAL
See Also:
Constant Field Values

VERTICAL

public static final int VERTICAL
See Also:
Constant Field Values

DIAGONAL_LTBR

public static final int DIAGONAL_LTBR
See Also:
Constant Field Values

DIAGONAL_RTBL

public static final int DIAGONAL_RTBL
See Also:
Constant Field Values
Constructor Detail

ImageUtils

public ImageUtils()
Method Detail

createBufferedImage

public static java.awt.image.BufferedImage createBufferedImage(int width,
                                                               int height)
Although any BufferedImage can be painted, it is wise to created images that are compatible with the current screen device.


createBufferedImage

public static java.awt.image.BufferedImage createBufferedImage(int width,
                                                               int height,
                                                               int transparency)
Although any BufferedImage can be painted, it is wise to created images that are compatible with the current screen device.

See Also:
Transparency

createJLabelImage

public static java.awt.image.BufferedImage createJLabelImage(javax.swing.JLabel label,
                                                             int margin,
                                                             double blur)
Convert JLabel to Image

Parameters:
label - this labels contains the text, font, color (foreground) and contrast color (background)
margin -
blur - 0.5 is a good value

createJLabelImage

public static java.awt.image.BufferedImage createJLabelImage(javax.swing.JLabel label,
                                                             int margin,
                                                             boolean blur)
Deprecated. use the method which has a double blur parameter


addText

public static void addText(java.awt.image.BufferedImage image,
                           javax.swing.JLabel label)
Add a contrasted text to an image

Parameters:
image - image to add the text to
label - this labels contains the text, font, color (foreground) and contrast color (background)

addText

public static void addText(java.awt.Graphics2D graphics2D,
                           double x,
                           double y,
                           java.awt.font.TextLayout lTitle,
                           java.awt.Color foreground,
                           java.awt.Color background)
Draw a contrasted text

Parameters:
image - image to add the text to
label - this labels contains the text, font, color (foreground) and contrast color (background)

textWithGlow

public static java.awt.image.BufferedImage textWithGlow(java.lang.String text,
                                                        java.awt.Font font,
                                                        int glowSize,
                                                        java.awt.Color foreground,
                                                        java.awt.Color glow,
                                                        float opacity)
Create an transparent image with glowing text

Parameters:
text -
font -
glowSize -
foreground -
glow -
opacity -
Returns:

drawTextWithGlow

public static void drawTextWithGlow(java.lang.String text,
                                    java.awt.Graphics g,
                                    java.awt.Font font,
                                    int glowSize,
                                    java.awt.Color foreground,
                                    java.awt.Color glow,
                                    float opacity)
Draw a glowing text on an image

Parameters:
text -
g -
font -
glowSize -
foreground -
glow -
opacity -

createGradientImage

public static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1,
                                                               java.awt.Color color2,
                                                               int direction)
Parameters:
color1 -
color2 -
direction -
Returns:

createGradientImage

public static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1,
                                                               java.awt.Color color2,
                                                               int direction,
                                                               int width,
                                                               int heigth)
Parameters:
color1 -
color2 -
direction -
width - of the to be generated image
height - of the to be generated image
Returns:

createGradientImage

public static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1,
                                                               java.awt.Color color2,
                                                               java.awt.Color color3,
                                                               int direction)
Parameters:
color1 -
color2 -
color3 -
direction -
Returns:

createGradientImage

public static java.awt.image.BufferedImage createGradientImage(java.awt.Color color1,
                                                               java.awt.Color color2,
                                                               java.awt.Color color3,
                                                               int direction,
                                                               int width,
                                                               int height)
Parameters:
color1 -
color2 -
color3 -
direction -
width - of the to be generated image
height - of the to be generated image
Returns:

addNoise

public static java.awt.image.BufferedImage addNoise(java.awt.image.BufferedImage image)
add a bit of noise to the whole image

Parameters:
image - the image to add noise to
noiseFactor - how much noise should be added, something between 3.0 and 10.0 is usually a good value.

addNoise

public static java.awt.image.BufferedImage addNoise(double noiseFactor,
                                                    java.awt.image.BufferedImage image)
add a bit of noise to the whole image

Parameters:
noiseFactor - how much noise should be added, something between 3.0 and 10.0 is usually a good value.
image - the image to add noise to

addNoise

public static java.awt.image.BufferedImage addNoise(double noiseFactor,
                                                    int x,
                                                    int y,
                                                    int width,
                                                    int height,
                                                    java.awt.image.BufferedImage image)
add a bit of noise

Parameters:
noiseFactor - how much noise should be added, something between 3.0 and 10.0 is usually a good value.
x -
y -
width -
height -
image - the image to add noise to

addLinearBlur

public static java.awt.image.BufferedImage addLinearBlur(java.awt.image.BufferedImage image,
                                                         int blurIndex)
Add a blur over the image (using linear blur). Please not that when the contents of the image is close to the edges, the blurring might cause strange side effects. Maintain a blur margin of about upto 20 pixels (depending on the blur radius). This blur uses ConvolveOp with a static (none Guassian) kernel


getLinearBlurOp

public static java.awt.image.ConvolveOp getLinearBlurOp(int size)
Get a ConvolveOp that blurs

Parameters:
size -
Returns:

addGaussianBlur

public static java.awt.image.BufferedImage addGaussianBlur(java.awt.image.BufferedImage image,
                                                           double radius)
Add a blur over the image (using linear blur). Please not that when the contents of the image is close to the edges, the blurring might cause strange side effects. Maintain a blur margin of about upto 20 pixels (depending on the blur radius). This blur uses ConvolveOp with a static (none Guassian) kernel


getGaussianBlurOp

public static java.awt.image.ConvolveOp getGaussianBlurOp(double stddev)
Get a ConvolveOp that blurs

Parameters:
size -
Returns:

addBlur

public static java.awt.image.BufferedImage addBlur(java.awt.image.BufferedImage image)
Add a blur over the image (using GaussianBlur). Please note that when the contents of the image is close to the edges, the blurring might cause strange side effects. Maintain a blur margin of about upto 20 pixels (depending on the blur radius), see addMargin.


addMargin

public static java.awt.image.BufferedImage addMargin(java.awt.image.BufferedImage image,
                                                     int margin)
This method is used to easy add a margin for blurring.


removeMargin

public static java.awt.image.BufferedImage removeMargin(java.awt.image.BufferedImage image,
                                                        int margin)
This method is used to easy add a margin for blurring.


replaceColor

public static java.awt.image.BufferedImage replaceColor(java.awt.image.BufferedImage image,
                                                        java.awt.Color originalColor,
                                                        java.awt.Color newColor)
This method replace a color from an image with another color Note: the image itself is returned, use createBufferedImage to work on a copy.


makeColorTransparent

public static java.awt.image.BufferedImage makeColorTransparent(java.awt.image.BufferedImage image,
                                                                java.awt.Color color)
This method erases a color form an image with a completely transparency color and thus effectively erasing it Note: the image itself is altered, use createBufferedImage to work on a copy.


makeGray

public static java.awt.image.BufferedImage makeGray(java.awt.image.BufferedImage image)
make gray Note: the image itself is altered, use createBufferedImage to work on a copy.


createDisabled

public static java.awt.Image createDisabled(java.awt.image.BufferedImage image)
Return a new image that appears disabled

Parameters:
image -
Returns:

brighter

public static java.awt.image.BufferedImage brighter(java.awt.image.BufferedImage image,
                                                    int times)
make brigher Note: the image itself is altered, use createBufferedImage to work on a copy.


brighter

public static java.awt.image.BufferedImage brighter(java.awt.image.BufferedImage image)

darker

public static java.awt.image.BufferedImage darker(java.awt.image.BufferedImage image)
make darker Note: the image itself is altered, use createBufferedImage to work on a copy.


createShadedImage

public static java.awt.image.BufferedImage createShadedImage(java.awt.image.BufferedImage image,
                                                             boolean addShade)
Create a copy of the image with or without a shadow.

Parameters:
image -
addShade -
Returns:

createShadedImage

public static java.awt.image.BufferedImage createShadedImage(java.awt.image.BufferedImage image,
                                                             int shadeOffset)
Create a copy of the image with a shadow.

Parameters:
image -
shadeOffset -
Returns:

putInPerspective

public static java.awt.image.BufferedImage putInPerspective(java.awt.image.BufferedImage image)

paintReflectionImage

public static void paintReflectionImage(java.awt.image.BufferedImage canvasImage,
                                        java.awt.image.BufferedImage sourceImage,
                                        float compressionFactor,
                                        int offsetX,
                                        int offsetY)
Paint the reflection onto the canvas

Parameters:
canvasImage -
sourceImage -
compressionFactor -
offsetX -
offsetY -

createReflectionImage

public static java.awt.image.BufferedImage createReflectionImage(java.awt.image.BufferedImage image,
                                                                 float compressionFactor)
Create a copy of the image with or without a shadow.

Parameters:
image -
compressionFactor - how much of the original image is drawn as the shadow (2=half)
Returns:

createReflectionImage

public static java.awt.image.BufferedImage createReflectionImage(java.awt.image.BufferedImage image)

addReflectionImage

public static java.awt.image.BufferedImage addReflectionImage(java.awt.image.BufferedImage image,
                                                              float compressionFactor,
                                                              int shadowOffset)
Create a copy of the image with or without a shadow.

Parameters:
image -
compressionFactor - the amount with which the image is reduced (2 = half)
shadowOffset - number of pixels the shadow is placed away from the original (5 is a good value)
Returns:

addReflectionImage

public static java.awt.image.BufferedImage addReflectionImage(java.awt.image.BufferedImage image)

flipVertical

public static java.awt.image.BufferedImage flipVertical(java.awt.image.BufferedImage image)

flipHorizontal

public static java.awt.image.BufferedImage flipHorizontal(java.awt.image.BufferedImage image)

rotate90

public static java.awt.image.BufferedImage rotate90(java.awt.image.BufferedImage image,
                                                    boolean clockwise)

morph

public static java.awt.image.BufferedImage morph(java.awt.image.BufferedImage from,
                                                 java.awt.image.BufferedImage to,
                                                 double percentage)

resize

public static java.awt.image.BufferedImage resize(java.awt.image.BufferedImage image,
                                                  double scale)
Resize by scale


resize

public static java.awt.image.BufferedImage resize(java.awt.image.BufferedImage image,
                                                  double scaleX,
                                                  double scaleY)
Resize by scale


resize

public static java.awt.image.BufferedImage resize(java.awt.image.BufferedImage image,
                                                  int width,
                                                  int height)
Resize to W & H

Parameters:
image -
width -
height -
Returns:

makeGlow

public static void makeGlow(int[] pixels,
                            int width,
                            int height)
Add a glow to an image http://www.curious-creature.org/2007/02/20/fast-image-processing-with-jogl Does not work at this time


createBufferedImage

public static java.awt.image.BufferedImage createBufferedImage(java.awt.Image image)
Create a buffered image from a regular image


getHighQualityRenderingHints

public static java.util.Map<java.awt.RenderingHints.Key,java.lang.Object> getHighQualityRenderingHints()
Returns:

createImageFromComponent

public static java.awt.image.BufferedImage createImageFromComponent(java.awt.Component component)
Create a buffered image from a component

Parameters:
component - Component
Returns:
BufferedImage

createImageFromIcon

public static java.awt.image.BufferedImage createImageFromIcon(javax.swing.Icon icon)
Create a buffered image from a component

Parameters:
icon - Component
Returns:
BufferedImage

createPixels

public static int[] createPixels(java.awt.image.BufferedImage bufferedImage)
Convert a buffered image into an array of rows of pixels The grabPixels method of the PixelGrabber class extracts the pixel data from an Image object into a one-dimensional array of type int. Each element in the array contains the four unsigned data bytes that represent a single pixel. The most significant byte contains the alpha or transparency data. Moving from most to least significant, the remaining bytes contain the unsigned values for red, green, and blue. If the image has N columns of pixels in each row, the first N elements in the array contain the data for the first row; the second N elements contain the data for the second row, etc.

Parameters:
bufferedImage -
Returns:

createImage

public static java.awt.Image createImage(int[] pixels,
                                         int width,
                                         int height)
Create image from pixels

Parameters:
pixels -
width -
height -
Returns:

captureScreen

public static java.awt.image.BufferedImage captureScreen()
Capture the current screen


readThumbnailMaintainRatio

public static java.awt.image.BufferedImage readThumbnailMaintainRatio(java.io.File imageFile,
                                                                      int maxWidth,
                                                                      int maxHeight)
                                                               throws java.io.IOException
Parameters:
imageFile -
maxWidth -
maxHeight -
Returns:
Throws:
java.io.IOException


Copyright © 2010. All Rights Reserved.