Package com.tagtraum.core.image
Class ImageFX
- java.lang.Object
-
- com.tagtraum.core.image.ImageFX
-
public final class ImageFX extends Object
Image special effects.- Author:
- Hendrik Schreiber
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ImageFX.GrayOp
Lightens an image, i.e.static class
ImageFX.LightenOp
Lightens an image, i.e.static class
ImageFX.NegativeOp
Lets you create a negative image of the given source image.static class
ImageFX.ReflectionOp
static class
ImageFX.ScreenCompatibleOp
Converts an image to its screen compatible counterpart.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BufferedImage
addDropShadow(Image image, int width, int height)
static BufferedImage
addDropShadow(Image image, int width, int height, float angle, int distance, int shadowSize, float opacity, Color color)
static BufferedImage
addFrame(Image image, int width, int height, Paint paint)
Creates a new Image with the specified width and height and a rounded picture frame.static BufferedImage
addReflection(Image image, int width)
Creates a new image from the source image that has a width ofside
and a height of1.5 x side
(no matter what).static void
addReflection(Image sourceImage, BufferedImage imageWithReflection)
Paints the given image to a destination image and adds a reflection below.static BufferedImage
addShape(Image image, int width, int height, Paint paint, Shape shape)
Creates a new Image with the specified width and height and the specified shape.static Color
adjustToPanelBackground(Color color)
static Color
adjustToUIBackground(Color color, String uiKey)
static Color
averageColor(BufferedImage image, int step)
Compute an average color.static Color
averageColor(BufferedImage image, int x0, int y0, int w, int h, int step)
Compute an average color.static BufferedImage
createARGBScratchBuffer(int width, int height)
static BufferedImage
createCompatibleImage(int width, int height)
Creates a screen compatible image.static BufferedImage
createCompatibleImage(int width, int height, float scaleFactor)
Creates a translucentBufferedImage
of the specified size, modified by the given scale factor.static BufferedImage
createCompatibleImage(Component component, int width, int height, int transparency, float scaleFactor)
static Icon
createDisabledIcon(Icon icon)
Creates a disabled (light, gray) version of the given icon, taking HiDPI issues into account.static Icon
createGrayIcon(Icon icon)
Creates a gray version of the given icon.static BufferedImage
createIntARGBScratchBuffer(int width, int height)
static Icon
createNegativeIcon(Icon icon)
Creates a negative version of the given icon.static BufferedImage
createPictureFrame(Image image, int width, int height)
Creates a new Image with the specified width and height and a rounded picture frame.static BufferedImage
createPictureFrame(Image image, int width, int height, Paint paint, Stroke stroke, Map<?,?> renderingHints)
Creates a new Image with the specified width and height and a rounded picture frame.static Image
createReflection(int width, int height, Image image)
Creates a reflection of an image (without the image itself).static GraphicsDevice
getDefaultScreenDevice()
Because of JDK-8152683, which causes a rare hang when callingGraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
, we are only calling this method from the EDT.static float
getHiDPIScaleFactor()
Return the system's content (HiDPI, aka Retina) scale factor.static float
getHiDPIScaleFactor(Graphics g)
Returns the graphics' object scale factor.static int
getScaleFactor()
Scale factor that components should be made bigger to appear normal on Windows HiDPI displays.static boolean
isLight(Color color)
static boolean
isMultiResolutionImage(Image image)
static BufferedImage
lighten(Image image)
Lightens an image, i.e.static BufferedImage
negative(Image image)
Negative image.static BufferedImage
toARGBImage(Image image)
Converts an image that is either pre-multiplied or does not even have an alpha channel to ARGB.static ImageIcon
toARGBImageIcon(ImageIcon imageIcon)
Make sure the icons are screen compatible to make drawing them faster.static BufferedImage
toBufferedImage(Image image)
static BufferedImage
toBufferedImage(RenderedImage img)
static BufferedImage
toBufferedImage(Icon icon)
static BufferedImage
toBufferedImage(Icon icon, float scaleFactor)
static byte[]
toByteArray(Image image, String format)
Convert image to byte array using the specified format.static BufferedImage
toGray(Image image)
Useful for creating grayed versions of images (e.g.static BufferedImage
toScreenCompatibleImage(Image image)
Make sure the image is screen compatible to make drawing them faster.static ImageIcon
toScreenCompatibleImageIcon(ImageIcon imageIcon)
Make sure the icons are screen compatible to make drawing them faster.static boolean
useHiDPIScaling()
Indicates whether we need to take HiDPI scaling into account.static boolean
useHiDPIScaling(Graphics g)
Indicates whether we need to take HiDPI scaling into account.
-
-
-
Method Detail
-
isMultiResolutionImage
public static boolean isMultiResolutionImage(Image image)
-
averageColor
public static Color averageColor(BufferedImage image, int x0, int y0, int w, int h, int step)
Compute an average color.- Parameters:
image
- imagex0
- start point of sub-imagey0
- start point of sub-imagew
- width of sub-imageh
- height of sub-imagestep
- sampling step
-
averageColor
public static Color averageColor(BufferedImage image, int step)
Compute an average color.- Parameters:
image
- imagestep
- sampling step
-
toByteArray
public static byte[] toByteArray(Image image, String format) throws IOException
Convert image to byte array using the specified format.- Parameters:
image
- imageformat
- format, e.g. "png"- Returns:
- byte array
- Throws:
IOException
-
toGray
public static BufferedImage toGray(Image image)
Useful for creating grayed versions of images (e.g. disabled icons).- Parameters:
image
- image- Returns:
- grayed image
-
negative
public static BufferedImage negative(Image image)
Negative image.- Parameters:
image
- image- Returns:
- negative image
- See Also:
ImageFX.LightenOp
-
isLight
public static boolean isLight(Color color)
-
lighten
public static BufferedImage lighten(Image image)
Lightens an image, i.e. increases brightness. This can be used to create 'disabled' image buttons.- Parameters:
image
- image- Returns:
- brightened image
- See Also:
ImageFX.LightenOp
-
createPictureFrame
public static BufferedImage createPictureFrame(Image image, int width, int height)
Creates a new Image with the specified width and height and a rounded picture frame.- Parameters:
image
- imagewidth
- widthheight
- height- Returns:
- framed image
-
createPictureFrame
public static BufferedImage createPictureFrame(Image image, int width, int height, Paint paint, Stroke stroke, Map<?,?> renderingHints)
Creates a new Image with the specified width and height and a rounded picture frame.- Parameters:
image
- imagewidth
- widthheight
- heightpaint
- paint for the picture framestroke
- strokerenderingHints
- rendering hints @return framed image
-
addFrame
public static BufferedImage addFrame(Image image, int width, int height, Paint paint)
Creates a new Image with the specified width and height and a rounded picture frame.- Parameters:
image
- imagewidth
- widthheight
- height- Returns:
- framed image
-
addShape
public static BufferedImage addShape(Image image, int width, int height, Paint paint, Shape shape)
Creates a new Image with the specified width and height and the specified shape.- Parameters:
image
- imagewidth
- widthheight
- heightshape
- shape- Returns:
- framed image
-
addDropShadow
public static BufferedImage addDropShadow(Image image, int width, int height)
-
addDropShadow
public static BufferedImage addDropShadow(Image image, int width, int height, float angle, int distance, int shadowSize, float opacity, Color color)
-
toBufferedImage
public static BufferedImage toBufferedImage(RenderedImage img)
-
toBufferedImage
public static BufferedImage toBufferedImage(Icon icon)
-
toBufferedImage
public static BufferedImage toBufferedImage(Icon icon, float scaleFactor)
-
toBufferedImage
public static BufferedImage toBufferedImage(Image image)
-
toScreenCompatibleImageIcon
public static ImageIcon toScreenCompatibleImageIcon(ImageIcon imageIcon)
Make sure the icons are screen compatible to make drawing them faster.- Parameters:
imageIcon
- an imageicon to transform- Returns:
- image icon the screen compatible image icon
-
toScreenCompatibleImage
public static BufferedImage toScreenCompatibleImage(Image image)
Make sure the image is screen compatible to make drawing them faster.- Parameters:
image
- an image to transform- Returns:
- image the screen compatible image
-
toARGBImageIcon
public static ImageIcon toARGBImageIcon(ImageIcon imageIcon)
Make sure the icons are screen compatible to make drawing them faster.- Parameters:
imageIcon
- an imageicon to transform- Returns:
- image icon the screen compatible image icon
-
toARGBImage
public static BufferedImage toARGBImage(Image image)
Converts an image that is either pre-multiplied or does not even have an alpha channel to ARGB.- Parameters:
image
- image- Returns:
- ARGB image
-
getHiDPIScaleFactor
public static float getHiDPIScaleFactor()
Return the system's content (HiDPI, aka Retina) scale factor. On macOS this corresponds to theToolkit.getDesktopProperty(String)
apple.awt.contentScaleFactor
.- Returns:
- usually
1.0f
for regular content and2.0f
for HiDPI/Retina content.
-
getScaleFactor
public static int getScaleFactor()
Scale factor that components should be made bigger to appear normal on Windows HiDPI displays. This is needed because current Java 8 (as of u72) behaves differently than macOS. It uses screen, not virtual coordinates.- Returns:
- scale factor
-
getHiDPIScaleFactor
public static float getHiDPIScaleFactor(Graphics g)
Returns the graphics' object scale factor.- Parameters:
g
- graphics object- Returns:
- scale factor
-
useHiDPIScaling
public static boolean useHiDPIScaling()
Indicates whether we need to take HiDPI scaling into account.- Returns:
- true of
getHiDPIScaleFactor()
!= 1f
-
useHiDPIScaling
public static boolean useHiDPIScaling(Graphics g)
Indicates whether we need to take HiDPI scaling into account.- Parameters:
g
- graphics object- Returns:
- true of
getHiDPIScaleFactor(java.awt.Graphics)
!= 1f
-
createARGBScratchBuffer
public static BufferedImage createARGBScratchBuffer(int width, int height)
-
createIntARGBScratchBuffer
public static BufferedImage createIntARGBScratchBuffer(int width, int height)
-
createCompatibleImage
public static BufferedImage createCompatibleImage(int width, int height, float scaleFactor)
Creates a translucentBufferedImage
of the specified size, modified by the given scale factor.- Parameters:
width
- widthheight
- heightscaleFactor
- scale factor- Returns:
- buffered image compatible for the default device with width and height of pixel x scaleFactor
-
createCompatibleImage
public static BufferedImage createCompatibleImage(Component component, int width, int height, int transparency, float scaleFactor)
-
getDefaultScreenDevice
public static GraphicsDevice getDefaultScreenDevice()
Because of JDK-8152683, which causes a rare hang when callingGraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
, we are only calling this method from the EDT. It is our hope that this may fix the issue. If not, it will at least let us monitor the calls.- Returns:
- default screen device
-
createCompatibleImage
public static BufferedImage createCompatibleImage(int width, int height)
Creates a screen compatible image.- Parameters:
width
- widthheight
- height- Returns:
- image screen compatible image
-
addReflection
public static BufferedImage addReflection(Image image, int width)
Creates a new image from the source image that has a width ofside
and a height of1.5 x side
(no matter what). The lower third of the resulting image is a reflection of the upper two thirds. If the source image is not square, the resulting image has appropriate margins. I.e. if the source image is wider than high, at the top and the bottom of the scaled and reflected image you'll find some empty space.The resulting image is a HiDPI image, if possible.
- Parameters:
image
- source imagewidth
- width, 1.5 x height- Returns:
- image
-
addReflection
public static void addReflection(Image sourceImage, BufferedImage imageWithReflection)
Paints the given image to a destination image and adds a reflection below.- Parameters:
sourceImage
- source imageimageWithReflection
- image to paint the result to
-
createReflection
public static Image createReflection(int width, int height, Image image)
Creates a reflection of an image (without the image itself).- Parameters:
width
- widthheight
- heightimage
- source image- Returns:
- result image
-
createDisabledIcon
public static Icon createDisabledIcon(Icon icon)
Creates a disabled (light, gray) version of the given icon, taking HiDPI issues into account.- Parameters:
icon
- icon- Returns:
- disabled icon
- See Also:
toGray(java.awt.Image)
,lighten(java.awt.Image)
-
createGrayIcon
public static Icon createGrayIcon(Icon icon)
Creates a gray version of the given icon.- Parameters:
icon
- icon- Returns:
- gray version
-
-