Class SwingGraphicsUtils

java.lang.Object
edu.cnu.mdi.swing.SwingGraphicsUtils

public final class SwingGraphicsUtils extends Object
Utility methods for creating offscreen images of Swing/AWT components.

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 Details

    • createComponentImageBuffer

      public static BufferedImage createComponentImageBuffer(Component c)
      Creates an offscreen opaque RGB buffer sized to match the component.

      This method does not paint anything; it only allocates a BufferedImage of type BufferedImage.TYPE_INT_RGB. Use paintComponentOnImage(Component, BufferedImage) to draw the component.

      Parameters:
      c - the component whose size determines the buffer dimensions
      Returns:
      a new opaque image buffer, or null if the component is null or has zero/negative width or height
    • createComponentTranslucentImageBuffer

      public static BufferedImage createComponentTranslucentImageBuffer(Component c)
      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 null if the component is invalid
    • paintComponentOnImage

      public static void paintComponentOnImage(Component c, BufferedImage image)
      Paints a component into an existing BufferedImage.

      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 render
      image - the target image buffer
    • getComponentImage

      public static BufferedImage getComponentImage(Component c)
      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 null on error
    • getComponentTranslucentImage

      public static BufferedImage getComponentTranslucentImage(Component c)
      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 null if invalid