Package nl.colorize.util.swing
Class Utils2D
java.lang.Object
nl.colorize.util.swing.Utils2D
Utility methods for Java 2D, mainly focused on image manipulation. Some of
the graphical effects provided by this class are based on the (brilliantly
titled) book Filthy Rich Clients.
-
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedImage
addPadding
(BufferedImage original, int padding) Returns a new image that contains the original in the center, but is surrounded by the specified amount of empty padding.static BufferedImage
applyDropShadow
(BufferedImage image, Color color, int size, int blur) Applies a drop shadow filter to the specified image, and returns the result as a new image.static BufferedImage
applyGaussianBlur
(BufferedImage original, int amount) Applies a Gaussian blur filter to the specified image, and returns the result as a new image.static BufferedImage
applyTint
(BufferedImage image, Color tint) Creates a new image by applying a color tint to an existing image.static Graphics2D
createGraphics
(Graphics g, boolean antialias, boolean bilinear) Convenience method that casts an AWTGraphics
instance toGraphics2D
, then configures its rendering hints based on the provided parameters.static Graphics2D
createGraphics
(BufferedImage image, boolean antialias, boolean bilinear) Returns aGraphics2D
instance for the specified image's graphics, configuring its rendering hints based on the provided parameters.static BufferedImage
createTestImage
(int width, int height) Returns an image that consists of a red circle with the specified width and height.static BufferedImage
createTestImage
(int width, int height, Color color) Returns an image that consists of a circle using the specified color.static void
drawCircle
(Graphics2D g2, int centerX, int centerY, int radius) Convenience method for drawing a circle from its center, rather than from its top left corner.static int
drawMultiLineString
(Graphics2D g2, String text, int x, int startY, int width) Draws a string that will word-wrap around a set width.static void
drawStringCentered
(Graphics2D g2, String text, int x, int y) static void
drawStringRight
(Graphics2D g2, String text, int x, int y) static BufferedImage
fromDataURL
(String dataURL) Parses a data URL and returns the resulting image.static Color
interpolateColor
(Color from, Color to, float delta) Creates a new color that calculates its red/green/blue/alpha components by interpolating between two other colors.static BufferedImage
static BufferedImage
loadImage
(InputStream input) static BufferedImage
loadImage
(ResourceFile file) static BufferedImage
makeImageCompatible
(BufferedImage original) Converts aBufferedImage
to the pixel format and color model preferred by the graphics environment.static Color
parseHexColor
(String hex) Converts a hex string to aColor
.static void
saveJPEG
(BufferedImage image, File dest) static void
saveJPEG
(BufferedImage image, OutputStream output) static void
savePNG
(BufferedImage image, File dest) static void
savePNG
(BufferedImage image, OutputStream output) static BufferedImage
scaleImage
(Image original, int width, int height, boolean highQuality) Returns a new image that contains the same contents as the original, but is scaled to the specified width and height.static String
toDataURL
(BufferedImage image) Returns a data URL that represents the specified image's contents in PNG format.static String
toHexColor
(Color color) Converts aColor
object to a hex string.static Color
Returns a color with the same RGB values ascolor
, but with a different alpha value.static Color
Returns a color with the same RGB values ascolor
, but with a different alpha value.static Color
Returns a color with the same RGB values ashexColor
, but with a different alpha value.static Color
Returns a color with the same RGB values ashexColor
, but with a different alpha value.
-
Method Details
-
loadImage
- Throws:
IOException
-
loadImage
- Throws:
IOException
-
loadImage
-
savePNG
- Throws:
IOException
-
savePNG
- Throws:
IOException
-
saveJPEG
- Throws:
IOException
-
saveJPEG
- Throws:
IOException
-
toDataURL
Returns a data URL that represents the specified image's contents in PNG format. -
fromDataURL
Parses a data URL and returns the resulting image.- Throws:
IOException
- if the data URL is invalid or uses an image format that is not supported by Java2D.
-
makeImageCompatible
Converts aBufferedImage
to the pixel format and color model preferred by the graphics environment. -
scaleImage
Returns a new image that contains the same contents as the original, but is scaled to the specified width and height.If the
highQuality
parameter is set to true, this method will use a scaling algorithm that improves visual quality at the cost of performance. The difference in visual quality will be more pronounced if the original image is significantly larger or smaller than the target size. -
addPadding
Returns a new image that contains the original in the center, but is surrounded by the specified amount of empty padding. -
createGraphics
Convenience method that casts an AWTGraphics
instance toGraphics2D
, then configures its rendering hints based on the provided parameters.- Parameters:
antialias
- Enables anti-aliasing for both graphics and text rendering.bilinear
- Enables bilinear filtering when scaling graphics. When false, nearest neighbor scaling will be used.
-
createGraphics
Returns aGraphics2D
instance for the specified image's graphics, configuring its rendering hints based on the provided parameters.- Parameters:
antialias
- Enables anti-aliasing for both graphics and text rendering.bilinear
- Enables bilinear filtering when scaling graphics. When false, nearest neighbor scaling will be used.
-
drawStringCentered
-
drawStringRight
-
drawMultiLineString
Draws a string that will word-wrap around a set width. Any line breaks already in the string will be preserved.- Parameters:
width
- The maximum width of the text rectangle.- Returns:
- The height of the text rectangle.
-
drawCircle
Convenience method for drawing a circle from its center, rather than from its top left corner. -
applyTint
Creates a new image by applying a color tint to an existing image. All pixels in the image will retain their existing alpha value, but the pixel's RGB values will be replaced with the tint color. -
withAlpha
Returns a color with the same RGB values ascolor
, but with a different alpha value. The provided alpha value is expected to be in the range 0 - 255. -
withAlpha
Returns a color with the same RGB values ascolor
, but with a different alpha value. The provided alpha value is expected to be in the range 0.0 - 1.0. -
withAlpha
Returns a color with the same RGB values ashexColor
, but with a different alpha value. The provided alpha value is expected to be in the range 0 - 255. -
withAlpha
Returns a color with the same RGB values ashexColor
, but with a different alpha value. The provided alpha value is expected to be in the range 0.0 - 1.0. -
toHexColor
Converts aColor
object to a hex string. For example, the color red will produce "#FF0000". -
parseHexColor
Converts a hex string to aColor
. For example, the string "#FF0000" will produce red. -
interpolateColor
Creates a new color that calculates its red/green/blue/alpha components by interpolating between two other colors.- Parameters:
delta
- Number between 0 and 1, where 0.0 indicates the "from" color and 1.0 indicates the "to" color.
-
applyGaussianBlur
Applies a Gaussian blur filter to the specified image, and returns the result as a new image.- Parameters:
amount
- The amount of blur, in pixels.
-
applyDropShadow
Applies a drop shadow filter to the specified image, and returns the result as a new image. The shadow will be cast from the non-transparent contents from the orginal image. Because the shadow will also take up some space, the image should ideally contain some empty padding to accommodate for the shadow.addPadding(BufferedImage, int)
can be used to add additional padding, if necessary.- Parameters:
color
- Drop shadow color.size
- The offset between the shadow and the original image.blur
- Amount of blur to apply to the shadow.
-
createTestImage
Returns an image that consists of a circle using the specified color. This image is intended to be used for testing purposes. -
createTestImage
Returns an image that consists of a red circle with the specified width and height. This image is intended to be used for testing purposes.
-