Class ImageViewer

java.lang.Object
org.loboevolution.img.ImageViewer

public final class ImageViewer extends Object
A general purpose image viewer component. It contains a scroll pane and manages the size of the image in accordance with the resize strategy property. It can also provide a popup menu containing generally useful viewing controls and functions; this default popup can be disabled by passing defaultPopupMenu = null to the constructor.

The Swing component that can be added to the GUI is obtained by calling getComponent().

ImageViewer supports status bars: arbitrary components that can be added to the viewer and are displayed below the image.

Overlays can also be added to the viewer; for details, see the the documentation of the Overlay class.

See Also:
  • Constructor Details

    • ImageViewer

      public ImageViewer()
      Creates a new image viewer. Initially it will be empty, and it will have a default popup menu.
    • ImageViewer

      public ImageViewer(BufferedImage image)
      Creates a new image viewer displaying the specified image. TThe viewer will have a default popup menu.
      Parameters:
      image - the image to display; if null then no image is displayed
    • ImageViewer

      public ImageViewer(BufferedImage image, boolean defaultPopupMenu)
      Creates a new image viewer displaying the specified image.
      Parameters:
      image - the image to display; if null then no image is displayed
      defaultPopupMenu - if true, then a default popup menu will be created and registered for the viewer
      See Also:
  • Method Details

    • setStatusBar

      public void setStatusBar(StatusBar statusBar)
      Sets the status bar component for this image viewer. The new status bar is made visible only if the statusBarVisible property is true. If statusBar is null this method removes any existing status bar.
      Parameters:
      statusBar - the new status bar component to set
      Throws:
      IllegalArgumentException - if the status bar has already been added to a different image viewer
    • getStatusBar

      public StatusBar getStatusBar()
      Returns the status bar currently associated with this viewer.
      Returns:
      the current status bar, or null if the viewer has no status bar
    • setStatusBarVisible

      public void setStatusBarVisible(boolean statusBarVisible)
      Sets whether the status bar is visible. The status bar is hidden by default.
      Parameters:
      statusBarVisible - true, if the status bar should be visible; false otherwise
    • isStatusBarVisible

      public boolean isStatusBarVisible()
      Returns whether the status bar is set to be visible. The status bar is hidden by default.
      Returns:
      the statusBarVisible property
    • getComponent

      public JComponent getComponent()
      Returns the image viewer component that can be displayed.
      Returns:
      the image viewer component
    • setImage

      public void setImage(BufferedImage image)
      Sets the image displayed by the viewer. If the argument is the same object as the image currently being displayed, then this method will trigger a refresh. If you modify the image shown by the viewer, use this function to notify the component and cause it to update.
      Parameters:
      image - the new image to display; if null then no image is displayed
    • getImage

      public BufferedImage getImage()
      Returns the currently displayed image.
      Returns:
      the current image, or null if no image is displayed
    • setResizeStrategy

      public void setResizeStrategy(ResizeStrategy resizeStrategy)
      Sets the resize strategy this viewer should use. The default is ResizeStrategy.SHRINK_TO_FIT.
      Parameters:
      resizeStrategy - the new resize strategy
    • getResizeStrategy

      public ResizeStrategy getResizeStrategy()
      Returns the current resize strategy. The default is ResizeStrategy.SHRINK_TO_FIT.
      Returns:
      the current resize strategy
    • setPixelatedZoom

      public void setPixelatedZoom(boolean pixelatedZoom)
      Sets whether the image should be resized with nearest neighbor interpolation when it is expanded. The default is false.
      Parameters:
      pixelatedZoom - the new value of the pixelatedZoom property
    • isPixelatedZoom

      public boolean isPixelatedZoom()
      Returns the current pixelated zoom setting. The default is false.
      Returns:
      the current pixelated zoom setting
    • getInterpolationType

      public Object getInterpolationType()
      Returns the current interpolation type. The default is RenderingHints.VALUE_INTERPOLATION_BICUBIC.
      Returns:
      the interpolation type
      See Also:
    • setInterpolationType

      public void setInterpolationType(Object type)
      Sets the interpolation type to use when resizing images. See RenderingHints.KEY_INTERPOLATION for details. The default value is RenderingHints.VALUE_INTERPOLATION_BICUBIC.

      The allowed values are:

      Changing the interpolation type to bilinear or nearest neighbor improves painting performance when the image needs to be resized.

      Note: when the pixelatedZoom property is set to true and the image is enlarged, then the nearest neighbor method is used regardless of this setting.

      Parameters:
      type - the interpolation type to use when resizing images
      Throws:
      IllegalArgumentException - if the parameter is not one of the allowed values
    • getZoomFactor

      public double getZoomFactor()
      Returns the zoom factor used when resize strategy is CUSTOM_ZOOM. The default value is 1.
      Returns:
      the custom zoom factor
    • setZoomFactor

      public void setZoomFactor(double newZoomFactor)
      Sets the zoom factor to use when the resize strategy is CUSTOM_ZOOM. The default value is 1.

      Note that calling this function does not change the current resize strategy.

      Parameters:
      newZoomFactor - the new zoom factor for the CUSTOM_ZOOM strategy
      Throws:
      IllegalArgumentException - if newZoomFactor is not a positive number
    • getImageTransform

      public AffineTransform getImageTransform()
      Returns the transformation that is applied to the image. Most commonly the transformation is the concatenation of a uniform scale and a translation.

      The AffineTransform instance returned by this method should not be modified.

      Returns:
      the transformation applied to the image before painting
      Throws:
      IllegalStateException - if there is no image set or if the size of the viewer is 0 (for example because it is not in a visible component)
    • addOverlay

      public void addOverlay(Overlay overlay, int layer)
      Adds an overlay as the specified layer.
      Parameters:
      overlay - the overlay to add
      layer - the layer to add the overlay to; higher layers are on top of lower layers; the image resides in layer 0
    • addOverlay

      public void addOverlay(Overlay overlay)
      Adds an overlay to layer 1.
      Parameters:
      overlay - the overlay to add
    • removeOverlay

      public void removeOverlay(Overlay overlay)
      Removes an overlay from the image viewer.
      Parameters:
      overlay - the overlay to remove
      Throws:
      IllegalArgumentException - if the overlay is not in the image viewer
    • addMouseListener

      public void addMouseListener(MouseListener l)
      Adds the specified mouse listener to receive mouse events from the image component of this image viewer. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the mouse listener
    • removeMouseListener

      public void removeMouseListener(MouseListener l)
      Removes the specified mouse listener so that it no longer receives mouse motion events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the mouse motion listener
    • addMouseMotionListener

      public void addMouseMotionListener(MouseMotionListener l)
      Adds the specified mouse motion listener to receive mouse events from the image component of this image viewer. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the mouse listener
    • removeMouseMotionListener

      public void removeMouseMotionListener(MouseMotionListener l)
      Removes the specified mouse motion listener so that it no longer receives mouse motion events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the mouse motion listener
    • addImageMouseMotionListener

      public void addImageMouseMotionListener(ImageMouseMotionListener l)
      Adds the specified image mouse motion listener to this viewer. The listener is notified as the mouse moves over pixels of the image. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the image mouse motion listener
    • removeImageMouseMotionListener

      public void removeImageMouseMotionListener(ImageMouseMotionListener l)
      Removes the specified image mouse motion listener so that it no longer receives mouse motion events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the mouse motion listener
    • addImageMouseClickListener

      public void addImageMouseClickListener(ImageMouseClickListener l)
      Adds the specified image mouse listener to this viewer. The listener is notified as mouse buttons are clicked over pixels of the image. If listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the image mouse motion listener
    • removeImageMouseClickListener

      public void removeImageMouseClickListener(ImageMouseClickListener l)
      Removes the specified image mouse listener so that it no longer receives mouse click events from the image component of this image viewer. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If the listener l is null, no exception is thrown and no action is performed.
      Parameters:
      l - the mouse motion listener
    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener l)
      Adds a PropertyChangeListener to the listener list. The same listener object may be added more than once, and will be called as many times as it is added. If the listener is null, no exception is thrown and no action is taken.
      Parameters:
      l - the listener to be added
    • removePropertyChangeListener

      public void removePropertyChangeListener(PropertyChangeListener l)
      Remove a PropertyChangeListener from the listener list. This removes a listener that was registered for all properties. If the listener was added more than once, it will be notified one less time after being removed. If the listener is null, or was never added, no exception is thrown and no action is taken.
      Parameters:
      l - the listener to remove
    • addPropertyChangeListener

      public void addPropertyChangeListener(String name, PropertyChangeListener l)
      Adds a PropertyChangeListener for a specific property. The listener will be invoked only when a call on firePropertyChange names that specific property. The same listener object may be added more than once. For each property, the listener will be invoked the number of times it was added for that property. If the property name or the listener is null, no exception is thrown and no action is taken.
      Parameters:
      name - the name of the property to listen on
      l - the listener to add
    • removePropertyChangeListener

      public void removePropertyChangeListener(String name, PropertyChangeListener l)
      Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties. If the listener was added more than once, it will be notified one less time after being removed. If the listener is null, or was never added, no exception is thrown and no action is taken.
      Parameters:
      name - the name of the property that was listened on
      l - the listener to remove
    • getScrollPane

      public JScrollPane getScrollPane()
      Returns the scroll pane of the image viewer.
      Returns:
      the scroll pane
    • getSynchronizer

      public org.loboevolution.img.Synchronizer getSynchronizer()

      Getter for the field synchronizer.

      Returns:
      a Synchronizer object.
    • setSynchronizer

      public void setSynchronizer(org.loboevolution.img.Synchronizer newSync)

      Setter for the field synchronizer.

      Parameters:
      newSync - a Synchronizer object.
    • pointToPixel

      public Point pointToPixel(Point p, boolean clipToImage)
      Returns the image pixel corresponding to the given point. If the clipToImage parameter is false, then the function will return an appropriately positioned pixel on an infinite plane, even if the point is outside the image bounds. If clipToImage is true then the function will return null for such positions, and any non-null return value will be a valid image pixel.
      Parameters:
      p - a point in component coordinate system
      clipToImage - whether the function should return null for positions outside the image bounds
      Returns:
      the corresponding image pixel
      Throws:
      IllegalStateException - if there is no image set or if the size of the viewer is 0 (for example because it is not in a visible component)
    • getPopupMenu

      public JPopupMenu getPopupMenu()