Class SwingImageViewer

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, MediaViewerComponent

public class SwingImageViewer extends JPanel implements MediaViewerComponent
High-fidelity pure Swing image viewer implementing MediaViewerComponent.

Supports smooth interactive zoom (mouse wheel), mouse dragging to pan when zoomed, double-click fit/1:1 toggle, integrated MediaToolbar, and right-click context actions.

Author:
anahata
See Also:
  • Field Details

    • toolbar

      private final MediaToolbar toolbar
      The action and metadata toolbar.
    • scrollPane

      private final JScrollPane scrollPane
      The scroll pane enclosing the canvas.
    • canvas

      private final SwingImageViewer.ImageCanvas canvas
      The interactive image drawing canvas.
    • image

      private BufferedImage image
      Decoded full-resolution image.
    • scale

      private double scale
      Current zoom scale factor (1.0 = 100% 1:1).
    • fitToView

      private boolean fitToView
      Whether the image is currently fitted to the viewport dimensions.
  • Constructor Details

    • SwingImageViewer

      public SwingImageViewer(@NonNull @NonNull AgiPanel agiPanel)
      Constructs a new SwingImageViewer.
      Parameters:
      agiPanel - The parent AgiPanel providing session and configuration context.
  • Method Details

    • setupInteractions

      private void setupInteractions()
      Configures mouse wheel zoom, pan dragging, and context menu.
    • toggleFitOrActualSize

      public void toggleFitOrActualSize()
      Toggles between fit-to-view and actual 1:1 pixel size.
    • zoom

      public void zoom(double factor, Point anchor)
      Zooms the image by a multiplier centered on the given anchor point.
      Parameters:
      factor - Zoom factor.
      anchor - Mouse anchor point.
    • calculateFitScale

      private void calculateFitScale()
      Computes the scale needed to fit the image inside the viewport.
    • updateCanvasSize

      private void updateCanvasSize()
      Updates the canvas dimensions to match the scaled image size.
    • showContextMenu

      private void showContextMenu(MouseEvent e)
      Displays a popup context menu with common image actions.
      Parameters:
      e - Mouse event triggering the popup.
    • getComponent

      public JComponent getComponent()
      Returns the Swing JComponent to be embedded in the UI hierarchy.

      Returns this JPanel as the primary visual component.

      Specified by:
      getComponent in interface MediaViewerComponent
      Returns:
      the Swing component.
    • load

      public void load(byte[] data, String mimeType, String displayName, URI sourceUri)
      Loads and initializes the media data for display and playback.

      Decodes image bytes and updates metadata and toolbar.

      Specified by:
      load in interface MediaViewerComponent
      Parameters:
      data - The raw binary data.
      mimeType - The detected MIME type (e.g. "video/mp4", "image/png", "audio/wav").
      displayName - An optional human-readable name or file name for the media item.
      sourceUri - An optional source URI on disk or remote network.
    • stop

      public void stop()
      Stops active audio or video playback, pausing decoding threads.

      No active audio or video playback in image viewer.

      Specified by:
      stop in interface MediaViewerComponent
    • dispose

      public void dispose()
      Permanently disposes of the component, releasing native decoders, textures, and timers.

      Releases image buffer.

      Specified by:
      dispose in interface MediaViewerComponent