Class Utils2D

java.lang.Object
nl.colorize.util.swing.Utils2D

public final class Utils2D extends Object
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 Details

    • loadImage

      public static BufferedImage loadImage(InputStream input) throws IOException
      Throws:
      IOException
    • loadImage

      public static BufferedImage loadImage(File file) throws IOException
      Throws:
      IOException
    • loadImage

      public static BufferedImage loadImage(ResourceFile file)
    • savePNG

      public static void savePNG(BufferedImage image, OutputStream output) throws IOException
      Throws:
      IOException
    • savePNG

      public static void savePNG(BufferedImage image, File dest) throws IOException
      Throws:
      IOException
    • saveJPEG

      public static void saveJPEG(BufferedImage image, OutputStream output) throws IOException
      Throws:
      IOException
    • saveJPEG

      public static void saveJPEG(BufferedImage image, File dest) throws IOException
      Throws:
      IOException
    • toDataURL

      public static String toDataURL(BufferedImage image)
      Returns a data URL that represents the specified image's contents in PNG format.
    • fromDataURL

      public static BufferedImage fromDataURL(String dataURL) throws IOException
      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

      public static BufferedImage makeImageCompatible(BufferedImage original)
      Converts a BufferedImage to the pixel format and color model preferred by the graphics environment.
    • scaleImage

      public 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.

      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

      public static 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.
    • createGraphics

      public static Graphics2D createGraphics(Graphics g, boolean antialias, boolean bilinear)
      Convenience method that casts an AWT Graphics instance to Graphics2D, 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

      public static Graphics2D createGraphics(BufferedImage image, boolean antialias, boolean bilinear)
      Returns a Graphics2D 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

      public static void drawStringCentered(Graphics2D g2, String text, int x, int y)
    • drawStringRight

      public static void drawStringRight(Graphics2D g2, String text, int x, int y)
    • drawMultiLineString

      public static int drawMultiLineString(Graphics2D g2, String text, int x, int startY, int width)
      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

      public 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.
    • applyTint

      public static BufferedImage applyTint(BufferedImage image, Color tint)
      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

      public static Color withAlpha(Color color, int alpha)
      Returns a color with the same RGB values as color, but with a different alpha value. The provided alpha value is expected to be in the range 0 - 255.
    • withAlpha

      public static Color withAlpha(Color color, float alpha)
      Returns a color with the same RGB values as color, but with a different alpha value. The provided alpha value is expected to be in the range 0.0 - 1.0.
    • withAlpha

      public static Color withAlpha(String hexColor, int alpha)
      Returns a color with the same RGB values as hexColor, but with a different alpha value. The provided alpha value is expected to be in the range 0 - 255.
    • withAlpha

      public static Color withAlpha(String hexColor, float alpha)
      Returns a color with the same RGB values as hexColor, but with a different alpha value. The provided alpha value is expected to be in the range 0.0 - 1.0.
    • toHexColor

      public static String toHexColor(Color color)
      Converts a Color object to a hex string. For example, the color red will produce "#FF0000".
    • parseHexColor

      public static Color parseHexColor(String hex)
      Converts a hex string to a Color. For example, the string "#FF0000" will produce red.
    • interpolateColor

      public 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.
      Parameters:
      delta - Number between 0 and 1, where 0.0 indicates the "from" color and 1.0 indicates the "to" color.
    • applyGaussianBlur

      public static BufferedImage applyGaussianBlur(BufferedImage original, int amount)
      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

      public 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. 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

      public static BufferedImage createTestImage(int width, int height, Color color)
      Returns an image that consists of a circle using the specified color. This image is intended to be used for testing purposes.
    • createTestImage

      public static BufferedImage createTestImage(int width, int height)
      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.