Class TakePicture

java.lang.Object
edu.cnu.mdi.util.TakePicture

public final class TakePicture extends Object
Utility for capturing a Swing Component as an image and either saving it as a PNG file or copying it to the system clipboard.

Design notes

  • This class is deliberately agnostic about what kind of component it is given — it used to special-case PlotPanel internally, which silently broke plot capture (it dropped the title, legend, and axis labels down to just the inner canvas, undoing work the caller had already done correctly). That knowledge now lives where it belongs: on the view itself, via BaseView.getImageComponent(). Callers should pass view.getImageComponent() rather than an arbitrary component plucked from the view's internals.
  • takePicture(Component) is the single entry point every existing caller already uses (the standard MDI toolbar's camera button, plot/histogram panels, and any custom "Save Image" action). It first asks the user to choose Save to File, Copy to Clipboard, or Cancel via DialogUtils.yesNoDialog(java.lang.String, java.lang.String...), then performs whichever the user picked. This means every existing call site gets clipboard support automatically — no new menu item, toolbar button, or icon is needed anywhere.
  • The file-save path writes PNG data. The chosen output filename is therefore forced to end with .png (case-insensitive) to avoid mismatches such as saving PNG data to picture.jpg or an extensionless file. Writing is performed via the configured PNG writer exposed by Environment.getPngWriter(). Access is synchronized and the writer's output is cleared after each capture. If the target file exists, the user is prompted to confirm overwrite.
  • The clipboard path places the captured image on the system clipboard using the standard AWT DataFlavor.imageFlavor transfer, which is platform-independent: it works the same way on Windows, macOS, and Linux (with a running display server) because it goes through Toolkit's clipboard, not any OS-specific API. It is also exposed directly as copyToClipboard(Component), for callers that want clipboard-only behavior without the Save/Copy/Cancel prompt.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Captures the provided component as an image and places it on the system clipboard, ready to paste into any application that accepts a pasted image (an email, a document, an image editor, a chat window, ...) — with no Save/Copy/Cancel prompt.
    static void
    Captures the provided component as an image, first asking the user whether to save it as a PNG file or copy it to the system clipboard.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • takePicture

      public static void takePicture(Component canvas)
      Captures the provided component as an image, first asking the user whether to save it as a PNG file or copy it to the system clipboard.

      This is the method every existing caller in the framework already uses (the standard toolbar camera button, plot and histogram panels, and MDI-3D's Image menu), so adding clipboard support here makes it available everywhere at once.

      Save to File: the save dialog is parented to the window containing canvas (if any). The selected file is forced to end with .png (case-insensitive); if it already exists, the user is asked to confirm overwrite. If the user cancels the dialog, this method returns without writing a file.

      Copy to Clipboard: delegates to copyToClipboard(Component).

      Error handling: Exceptions during capture, writing, or clipboard access are recorded through the platform logger.

      Parameters:
      canvas - the component to capture; if null, nothing is done
    • copyToClipboard

      public static void copyToClipboard(Component canvas)
      Captures the provided component as an image and places it on the system clipboard, ready to paste into any application that accepts a pasted image (an email, a document, an image editor, a chat window, ...) — with no Save/Copy/Cancel prompt.

      Most callers should use takePicture(Component) instead, so the user gets a choice; call this directly only when clipboard-only behavior is specifically wanted (for example, a dedicated "Copy Image" keyboard shortcut).

      This uses the standard AWT image transfer (DataFlavor.imageFlavor via Toolkit.getSystemClipboard()), which every major desktop platform's AWT implementation supports identically — there is no per-OS branch here. No file is written and no dialog is shown; the method either succeeds silently or logs a warning.

      Error handling: Exceptions during capture or clipboard access are recorded through the platform logger (for example HeadlessException in an environment with no display, or a clipboard owned by a security-restricted process refusing access).

      Parameters:
      canvas - the component to capture; if null, nothing is done