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 BufferedImageaddPadding(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 BufferedImageapplyDropShadow(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 BufferedImageapplyGaussianBlur(BufferedImage original, int amount) Applies a Gaussian blur filter to the specified image, and returns the result as a new image.static BufferedImageapplyTint(BufferedImage image, Color tint) Creates a new image by applying a color tint to an existing image.static Graphics2DcreateGraphics(Graphics g, boolean antialias, boolean bilinear) Convenience method that casts an AWTGraphicsinstance toGraphics2D, then configures its rendering hints based on the provided parameters.static Graphics2DcreateGraphics(BufferedImage image, boolean antialias, boolean bilinear) Returns aGraphics2Dinstance for the specified image's graphics, configuring its rendering hints based on the provided parameters.static BufferedImagecreateTestImage(int width, int height) Returns an image that consists of a red circle with the specified width and height.static BufferedImagecreateTestImage(int width, int height, Color color) Returns an image that consists of a circle using the specified color.static voiddrawCircle(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 intdrawMultiLineString(Graphics2D g2, String text, int x, int startY, int width) Draws a string that will word-wrap around a set width.static voiddrawStringCentered(Graphics2D g2, String text, int x, int y) static voiddrawStringRight(Graphics2D g2, String text, int x, int y) static BufferedImagefromDataURL(String dataURL) Parses a data URL and returns the resulting image.static ColorinterpolateColor(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 BufferedImagestatic BufferedImageloadImage(InputStream input) static BufferedImageloadImage(ResourceFile file) static BufferedImagemakeImageCompatible(BufferedImage original) Converts aBufferedImageto the pixel format and color model preferred by the graphics environment.static ColorparseHexColor(String hex) Converts a hex string to aColor.static voidsaveJPEG(BufferedImage image, File dest) static voidsaveJPEG(BufferedImage image, OutputStream output) static voidsavePNG(BufferedImage image, File dest) static voidsavePNG(BufferedImage image, OutputStream output) static BufferedImagescaleImage(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 StringtoDataURL(BufferedImage image) Returns a data URL that represents the specified image's contents in PNG format.static StringtoHexColor(Color color) Converts aColorobject to a hex string.static ColorwithAlpha(int rgba, float alpha) Returns a color with the same RGB values asrgba, but with a different alpha value.static ColorwithAlpha(int rgba, int alpha) Returns a color with the same RGB values ashexColor, but with a different alpha value.static ColorReturns a color with the same RGB values ascolor, but with a different alpha value.static ColorReturns a color with the same RGB values ascolor, but with a different alpha value.static ColorReturns a color with the same RGB values ashexColor, but with a different alpha value.static ColorReturns 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 aBufferedImageto 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
highQualityparameter 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 AWTGraphicsinstance 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 aGraphics2Dinstance 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
-
withAlpha
-
withAlpha
-
withAlpha
-
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 asrgba, but with a different alpha value. The provided alpha value is expected to be in the range 0.0 - 1.0. -
toHexColor
-
parseHexColor
-
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.
-