Package org.loboevolution.img
Class ImageViewer
java.lang.Object
org.loboevolution.img.ImageViewer
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 Summary
ConstructorDescriptionCreates a new image viewer.ImageViewer
(BufferedImage image) Creates a new image viewer displaying the specified image.ImageViewer
(BufferedImage image, boolean defaultPopupMenu) Creates a new image viewer displaying the specified image. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the specified image mouse listener to this viewer.void
Adds the specified image mouse motion listener to this viewer.void
Adds the specified mouse listener to receive mouse events from the image component of this image viewer.void
Adds the specified mouse motion listener to receive mouse events from the image component of this image viewer.void
addOverlay
(Overlay overlay) Adds an overlay to layer 1.void
addOverlay
(Overlay overlay, int layer) Adds an overlay as the specified layer.void
Adds aPropertyChangeListener
to the listener list.void
Adds aPropertyChangeListener
for a specific property.Returns the image viewer component that can be displayed.getImage()
Returns the currently displayed image.Returns the transformation that is applied to the image.Returns the current interpolation type.Returns the current resize strategy.Returns the scroll pane of the image viewer.Returns the status bar currently associated with this viewer.org.loboevolution.img.Synchronizer
Getter for the field synchronizer.double
Returns the zoom factor used when resize strategy is CUSTOM_ZOOM.boolean
Returns the current pixelated zoom setting.boolean
Returns whether the status bar is set to be visible.pointToPixel
(Point p, boolean clipToImage) Returns the image pixel corresponding to the given point.void
Removes the specified image mouse listener so that it no longer receives mouse click events from the image component of this image viewer.void
Removes the specified image mouse motion listener so that it no longer receives mouse motion events from the image component of this image viewer.void
Removes the specified mouse listener so that it no longer receives mouse motion events from the image component of this image viewer.void
Removes the specified mouse motion listener so that it no longer receives mouse motion events from the image component of this image viewer.void
removeOverlay
(Overlay overlay) Removes an overlay from the image viewer.void
Remove aPropertyChangeListener
from the listener list.void
Remove aPropertyChangeListener
from the listener list.void
setImage
(BufferedImage image) Sets the image displayed by the viewer.void
setInterpolationType
(Object type) Sets the interpolation type to use when resizing images.void
setPixelatedZoom
(boolean pixelatedZoom) Sets whether the image should be resized with nearest neighbor interpolation when it is expanded.void
setResizeStrategy
(ResizeStrategy resizeStrategy) Sets the resize strategy this viewer should use.void
setStatusBar
(StatusBar statusBar) Sets the status bar component for this image viewer.void
setStatusBarVisible
(boolean statusBarVisible) Sets whether the status bar is visible.void
setSynchronizer
(org.loboevolution.img.Synchronizer newSync) Setter for the field synchronizer.void
setZoomFactor
(double newZoomFactor) Sets the zoom factor to use when the resize strategy is CUSTOM_ZOOM.
-
Constructor Details
-
ImageViewer
public ImageViewer()Creates a new image viewer. Initially it will be empty, and it will have a default popup menu. -
ImageViewer
Creates a new image viewer displaying the specified image. TThe viewer will have a default popup menu.- Parameters:
image
- the image to display; ifnull
then no image is displayed
-
ImageViewer
Creates a new image viewer displaying the specified image.- Parameters:
image
- the image to display; if null then no image is displayeddefaultPopupMenu
- iftrue
, then a default popup menu will be created and registered for the viewer- See Also:
-
-
Method Details
-
setStatusBar
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
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
Returns the image viewer component that can be displayed.- Returns:
- the image viewer component
-
setImage
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
Returns the currently displayed image.- Returns:
- the current image, or null if no image is displayed
-
setResizeStrategy
Sets the resize strategy this viewer should use. The default isResizeStrategy.SHRINK_TO_FIT
.- Parameters:
resizeStrategy
- the new resize strategy
-
getResizeStrategy
Returns the current resize strategy. The default isResizeStrategy.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 isfalse
.- Parameters:
pixelatedZoom
- the new value of the pixelatedZoom property
-
isPixelatedZoom
public boolean isPixelatedZoom()Returns the current pixelated zoom setting. The default isfalse
.- Returns:
- the current pixelated zoom setting
-
getInterpolationType
Returns the current interpolation type. The default isRenderingHints.VALUE_INTERPOLATION_BICUBIC
.- Returns:
- the interpolation type
- See Also:
-
setInterpolationType
Sets the interpolation type to use when resizing images. SeeRenderingHints.KEY_INTERPOLATION
for details. The default value isRenderingHints.VALUE_INTERPOLATION_BICUBIC
.The allowed values are:
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
RenderingHints.VALUE_INTERPOLATION_BILINEAR
RenderingHints.VALUE_INTERPOLATION_BICUBIC
(default)
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
- ifnewZoomFactor
is not a positive number
-
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
Adds an overlay as the specified layer.- Parameters:
overlay
- the overlay to addlayer
- the layer to add the overlay to; higher layers are on top of lower layers; the image resides in layer 0
-
addOverlay
Adds an overlay to layer 1.- Parameters:
overlay
- the overlay to add
-
removeOverlay
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
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
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
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
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
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 isnull
, no exception is thrown and no action is performed.- Parameters:
l
- the image mouse motion listener
-
removeImageMouseMotionListener
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 isnull
, no exception is thrown and no action is performed.- Parameters:
l
- the mouse motion listener
-
addImageMouseClickListener
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 isnull
, no exception is thrown and no action is performed.- Parameters:
l
- the image mouse motion listener
-
removeImageMouseClickListener
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 isnull
, no exception is thrown and no action is performed.- Parameters:
l
- the mouse motion listener
-
addPropertyChangeListener
Adds aPropertyChangeListener
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 isnull
, no exception is thrown and no action is taken.- Parameters:
l
- the listener to be added
-
removePropertyChangeListener
Remove aPropertyChangeListener
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 isnull
, or was never added, no exception is thrown and no action is taken.- Parameters:
l
- the listener to remove
-
addPropertyChangeListener
Adds aPropertyChangeListener
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 onl
- the listener to add
-
removePropertyChangeListener
Remove aPropertyChangeListener
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 isnull
, or was never added, no exception is thrown and no action is taken.- Parameters:
name
- the name of the property that was listened onl
- the listener to remove
-
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
- aSynchronizer
object.
-
pointToPixel
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 systemclipToImage
- 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
-