Class SwingGraphicsUtils
These helpers simplify the process of generating snapshots of components for
thumbnails, drag images, buffered painting, effects, and testing. Unlike
Component.createImage(int, int), these methods return a
BufferedImage that can be manipulated, saved, or inspected.
All methods are null-safe and return null if the given component is
null or has an invalid size.
Important: The component must have a meaningful size (usually after layout or manual sizing) before rendering can occur.
-
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedImageCreates an offscreen opaque RGB buffer sized to match the component.static BufferedImageCreates a translucent offscreen buffer sized to match the component.static BufferedImageConvenience method that creates an opaque buffer and immediately paints the component into it.static BufferedImageConvenience method that creates a translucent ARGB buffer and immediately paints the component into it.static voidpaintComponentOnImage(Component c, BufferedImage image) Paints a component into an existingBufferedImage.
-
Method Details
-
createComponentImageBuffer
Creates an offscreen opaque RGB buffer sized to match the component.This method does not paint anything; it only allocates a
BufferedImageof typeBufferedImage.TYPE_INT_RGB. UsepaintComponentOnImage(Component, BufferedImage)to draw the component.- Parameters:
c- the component whose size determines the buffer dimensions- Returns:
- a new opaque image buffer, or
nullif the component isnullor has zero/negative width or height
-
createComponentTranslucentImageBuffer
Creates a translucent offscreen buffer sized to match the component.This uses
BufferedImage.TYPE_INT_ARGB, which preserves alpha and allows semi-transparent rendering. Useful for drag images, effects, or components with non-opaque backgrounds.- Parameters:
c- the component whose size determines the buffer dimensions- Returns:
- a new ARGB image, or
nullif the component is invalid
-
paintComponentOnImage
Paints a component into an existingBufferedImage.This uses
Component.paint(java.awt.Graphics)which performs the component's full paint cycle (background, border, children, etc.). The component does not need to be visible on screen.- Parameters:
c- the component to renderimage- the target image buffer
-
getComponentImage
Convenience method that creates an opaque buffer and immediately paints the component into it.Useful for quickly generating snapshots for saving or testing.
- Parameters:
c- the component to render- Returns:
- a fully painted opaque image, or
nullon error
-
getComponentTranslucentImage
Convenience method that creates a translucent ARGB buffer and immediately paints the component into it.This preserves transparency and is appropriate for overlays, effects, or drag-and-drop representations.
- Parameters:
c- the component to render- Returns:
- a fully painted ARGB image, or
nullif invalid
-