public class NavigableImagePanel
extends javax.swing.JPanel
NavigableImagePanel
is a lightweight container displaying
an image that can be zoomed in and out and panned with ease and simplicity,
using an adaptive rendering for high quality display and satisfactory performance.
An image is loaded either via a constructor:
NavigableImagePanel panel = new NavigableImagePanel(image);or using a setter:
NavigableImagePanel panel = new NavigableImagePanel(); panel.setImage(image);When an image is set, it is initially painted centered in the component, at the largest possible size, fully visible, with its aspect ratio is preserved. This is defined as 100% of the image size and its corresponding zoom level is 1.0.
panel.setZoomDevice(ZoomDevice.MOUSE_BUTTON);The left mouse button works as a toggle switch between zooming in and zooming out modes, and the right button zooms an image by one increment (default is 20%). You can change the zoom increment value by:
panel.setZoomIncrement(newZoomIncrement);If you intend to provide programmatic zoom control, set the zoom device to none to disable both the mouse wheel and buttons for zooming purposes:
panel.setZoomDevice(ZoomDevice.NONE);and use
setZoom()
to change the zoom level.
Zooming is always around the point the mouse pointer is currently at, so that
this point (called a zooming center) remains stationary ensuring that the area
of an image we are zooming into does not disappear off the screen. The zooming center
stays at the same location on the screen and all other points move radially away from
it (when zooming in), or towards it (when zooming out). For programmatically
controlled zooming the zooming center is either specified when setZoom()
is called:
panel.setZoom(newZoomLevel, newZoomingCenter);or assumed to be the point of an image which is the closest to the center of the panel, if no zooming center is specified:
panel.setZoom(newZoomLevel);There are no lower or upper zoom level limits.
NavigableImagePanel
does not use scroll bars for navigation,
but relies on a navigation image located in the upper left corner of the panel.
The navigation image is a small replica of the image displayed in the panel.
When you click on any point of the navigation image that part of the image
is displayed in the panel, centered. The navigation image can also be
zoomed in the same way as the main image.
In order to adjust the position of an image in the panel, it can be dragged with the mouse, using the left button. For programmatic image navigation, disable the navigation image:
panel.setNavigationImageEnabled(false)and use
getImageOrigin()
and
setImageOrigin()
to move the image around the panel.
NavigableImagePanel
uses the Nearest Neighbor interpolation
for image rendering (default in Java).
When the scaled image becomes larger than the original image,
the Bilinear interpolation is applied, but only to the part
of the image which is displayed in the panel. This interpolation change threshold
can be controlled by adjusting the value of
HIGH_QUALITY_RENDERING_SCALE_THRESHOLD
.
Author: Slav Boleslawski http://today.java.net/pub/a/today/2007/03/27/navigable-image-panel.html
Modifier and Type | Class and Description |
---|---|
static class |
NavigableImagePanel.Coordinates
This class is required for high precision image coordinates translation.
|
static class |
NavigableImagePanel.ZoomDevice
Defines zoom devices.
|
javax.swing.JPanel.AccessibleJPanel
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
IMAGE_CHANGED_PROPERTY
Identifies that the image in the panel has changed.
|
static java.lang.String |
ZOOM_INCREMENT_CHANGED_PROPERTY
Identifies a change to the zoom increment.
|
static java.lang.String |
ZOOM_LEVEL_CHANGED_PROPERTY
Identifies a change to the zoom level.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
NavigableImagePanel()
Creates a new navigable image panel with no default image and
the mouse scroll wheel as the zooming device.
|
NavigableImagePanel(java.awt.image.BufferedImage image)
Creates a new navigable image panel with the specified image
and the mouse scroll wheel as the zooming device.
|
Modifier and Type | Method and Description |
---|---|
void |
centerImage(NavigableImagePanel.Coordinates imageCoordinates)
Centers the image on the given image coordinates
|
void |
centerImage(java.awt.Point point)
If the given point on this panel is within the image the image
is centered on that point.
|
java.awt.image.BufferedImage |
getImage() |
int |
getImageHeight() |
java.awt.Point |
getImageOrigin()
Gets the image origin.
|
int |
getImageWidth() |
double |
getScale() |
double |
getZoom()
Gets the current zoom level.
|
NavigableImagePanel.ZoomDevice |
getZoomDevice()
Gets the current zoom device.
|
double |
getZoomIncrement()
Gets the current zoom increment.
|
NavigableImagePanel.Coordinates |
imageToPanelCoordinates(NavigableImagePanel.Coordinates coordinates)
Converts the original image coordinates into this panel's coordinates
|
boolean |
isHighQualityRenderingEnabled()
Indicates whether the high quality rendering feature is enabled.
|
boolean |
isNavigationImageEnabled()
Indicates whether navigation image is enabled.
|
static boolean |
isStandardRGBImage(java.awt.image.BufferedImage bImage)
Tests whether an image uses the standard RGB color space.
|
boolean |
isWithinImage(java.awt.Point p)
Tests whether a given point in the panel falls within the image boundaries.
|
protected void |
paintComponent(java.awt.Graphics g)
Paints the panel and its image at the current zoom level, location, and
interpolation method dependent on the image scale.
|
NavigableImagePanel.Coordinates |
panelToImageCoordinates(java.awt.Point point)
Converts this panel's coordinates into the original image coordinates
|
static java.awt.image.BufferedImage |
readImage(java.lang.String imagePath)
Reads an image from the given path
|
void |
resetView()
Resets the view so the image fits the panel
|
void |
setHighQualityRenderingEnabled(boolean enabled)
Enables/disables high quality rendering.
|
void |
setImage(java.awt.image.BufferedImage image)
Sets an image for display in the panel.
|
void |
setImageOrigin(int x,
int y)
Sets the image origin.
|
void |
setImageOrigin(java.awt.Point newOrigin)
Sets the image origin.
|
void |
setNavigationImageEnabled(boolean enabled)
Enables/disables navigation with the navigation image.
|
void |
setZoom(double newZoom)
Sets the zoom level used to display the image.
|
void |
setZoom(double newZoom,
java.awt.Point zoomingCenter)
Sets the zoom level used to display the image, and the zooming center,
around which zooming is done.
|
void |
setZoomDevice(NavigableImagePanel.ZoomDevice newZoomDevice)
Sets a new zoom device.
|
void |
setZoomIncrement(double newZoomIncrement)
Sets a new zoom increment value.
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final java.lang.String ZOOM_LEVEL_CHANGED_PROPERTY
Identifies a change to the zoom level.
public static final java.lang.String ZOOM_INCREMENT_CHANGED_PROPERTY
Identifies a change to the zoom increment.
public static final java.lang.String IMAGE_CHANGED_PROPERTY
Identifies that the image in the panel has changed.
public NavigableImagePanel()
Creates a new navigable image panel with no default image and the mouse scroll wheel as the zooming device.
public NavigableImagePanel(java.awt.image.BufferedImage image)
Creates a new navigable image panel with the specified image and the mouse scroll wheel as the zooming device.
image
- the default imagepublic static java.awt.image.BufferedImage readImage(java.lang.String imagePath) throws java.io.IOException
imagePath
- the path, either file or httpjava.io.IOException
- in case of an exceptionpublic final int getImageWidth()
public final int getImageHeight()
public final void setZoomDevice(NavigableImagePanel.ZoomDevice newZoomDevice)
Sets a new zoom device.
newZoomDevice
- specifies the type of a new zoom device.public final NavigableImagePanel.ZoomDevice getZoomDevice()
Gets the current zoom device.
public final void centerImage(NavigableImagePanel.Coordinates imageCoordinates)
imageCoordinates
- the image coordinates on which to center the imagepublic final void centerImage(java.awt.Point point)
point
- the point on which to center the imagepublic final boolean isWithinImage(java.awt.Point p)
p
- the pointpublic final void setImage(java.awt.image.BufferedImage image)
Sets an image for display in the panel.
image
- an image to be set in the panelpublic final java.awt.image.BufferedImage getImage()
public final double getScale()
public final void resetView()
public static boolean isStandardRGBImage(java.awt.image.BufferedImage bImage)
Tests whether an image uses the standard RGB color space.
bImage
- the image to testpublic final NavigableImagePanel.Coordinates panelToImageCoordinates(java.awt.Point point)
point
- the panel coordinatespublic final NavigableImagePanel.Coordinates imageToPanelCoordinates(NavigableImagePanel.Coordinates coordinates)
coordinates
- the image coordinatespublic final boolean isHighQualityRenderingEnabled()
Indicates whether the high quality rendering feature is enabled.
public final void setHighQualityRenderingEnabled(boolean enabled)
Enables/disables high quality rendering.
enabled
- enables/disables high quality renderingpublic final boolean isNavigationImageEnabled()
Indicates whether navigation image is enabled.
public final void setNavigationImageEnabled(boolean enabled)
Enables/disables navigation with the navigation image.
Navigation image should be disabled when custom, programmatic navigation is implemented.
enabled
- true when navigation image is enabled, false otherwise.public final double getZoom()
Gets the current zoom level.
public final void setZoom(double newZoom)
Sets the zoom level used to display the image.
This method is used in programmatic zooming. The zooming center is the point of the image closest to the center of the panel. After a new zoom level is set the image is repainted.
newZoom
- the zoom level used to display this panel's image.public final void setZoom(double newZoom, java.awt.Point zoomingCenter)
Sets the zoom level used to display the image, and the zooming center, around which zooming is done.
This method is used in programmatic zooming. After a new zoom level is set the image is repainted.
newZoom
- the zoom level used to display this panel's image.zoomingCenter
- the zooming centerpublic final double getZoomIncrement()
Gets the current zoom increment.
public final void setZoomIncrement(double newZoomIncrement)
Sets a new zoom increment value.
newZoomIncrement
- new zoom increment valuepublic final java.awt.Point getImageOrigin()
Gets the image origin.
Image origin is defined as the upper, left corner of the image in the panel's coordinate system.
public final void setImageOrigin(int x, int y)
Sets the image origin.
Image origin is defined as the upper, left corner of the image in the panel's coordinate system. After a new origin is set, the image is repainted. This method is used for programmatic image navigation.
x
- the x coordinate of the new image originy
- the y coordinate of the new image originpublic final void setImageOrigin(java.awt.Point newOrigin)
Sets the image origin.
Image origin is defined as the upper, left corner of the image in the panel's coordinate system. After a new origin is set, the image is repainted. This method is used for programmatic image navigation.
newOrigin
- the value of a new image originprotected void paintComponent(java.awt.Graphics g)
paintComponent
in class javax.swing.JComponent
g
- the Graphics
context for painting