- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
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.
Image
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.
Zooming
Zooming can be controlled interactively, using either the mouse scroll wheel (default) or the mouse two buttons, or programmatically, allowing the programmer to implement other custom zooming methods. If the mouse does not have a scroll wheel, set the zooming device to mouse buttons: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.
Navigation
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.
Rendering
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
.
http://today.java.net/pub/a/today/2007/03/27/navigable-image-panel.html Included with permission.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Defines zoom devices.Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Identifies that the image in the panel has changed.static final String
Identifies a change to the zoom increment.static final String
Identifies a change to the zoom level.Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
ConstructorDescriptionCreates a new navigable image panel with no default image and the mouse scroll wheel as the zooming device.NavigableImagePanel
(BufferedImage image) Creates a new navigable image panel with the specified image and the mouse scroll wheel as the zooming device. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
centerImage
(Point2D.Double imagePoint) Centers the image on the given image pointfinal void
centerImage
(Point panelPoint) Centers the image on the given point on the panel, if it is within the image boundaries.final BufferedImage
getImage()
final int
final Point
Gets the image origin.final int
final double
getScale()
final double
getZoom()
Gets the current zoom level.Gets the current zoom device.final double
Gets the current zoom increment.final Point2D.Double
imageToPanelPoint
(Point2D.Double imagePoint) Converts the original image point into this panel's pointfinal boolean
Indicates whether the high quality rendering feature is enabled.final boolean
final boolean
Indicates whether navigation image is enabled.final boolean
isWithinImage
(Point panelPoint) Tests whether a given point in the panel falls within the image boundaries.protected void
Paints the panel and its image at the current zoom level, location, and interpolation method dependent on the image scale.final Point2D.Double
panelToImagePoint
(Point panelPoint) Converts this panel's point into the original image pointstatic BufferedImage
Reads an image from the given pathfinal void
Resets the view so the image fits the panelfinal void
setHighQualityRenderingEnabled
(boolean enabled) Enables/disables high quality rendering.final void
setImage
(BufferedImage image) Sets an image for display in the panel.final void
setImageOrigin
(int x, int y) Sets the image origin.final void
setImageOrigin
(Point newOrigin) Sets the image origin.final void
setMoveImageEnabled
(boolean moveImageEnabled) final void
setNavigationImageEnabled
(boolean enabled) Enables/disables navigation with the navigation image.final void
setZoom
(double newZoom) Sets the zoom level used to display the image.final void
Sets the zoom level used to display the image, and the zooming center, around which zooming is done.final void
setZoomDevice
(NavigableImagePanel.ZoomDevice newZoomDevice) Sets a new zoom device.final void
setZoomIncrement
(double newZoomIncrement) Sets a new zoom increment value.Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
Methods inherited from class javax.swing.JComponent
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
Methods inherited from class java.awt.Container
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
Methods inherited from class java.awt.Component
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, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Field Details
-
ZOOM_LEVEL_CHANGED_PROPERTY
Identifies a change to the zoom level.
- See Also:
-
ZOOM_INCREMENT_CHANGED_PROPERTY
Identifies a change to the zoom increment.
- See Also:
-
IMAGE_CHANGED_PROPERTY
Identifies that the image in the panel has changed.
- See Also:
-
-
Constructor Details
-
NavigableImagePanel
public NavigableImagePanel()Creates a new navigable image panel with no default image and the mouse scroll wheel as the zooming device.
-
NavigableImagePanel
Creates a new navigable image panel with the specified image and the mouse scroll wheel as the zooming device.
- Parameters:
image
- the default image- Throws:
IllegalArgumentException
- in case the image height and/or width is zero
-
-
Method Details
-
readImage
Reads an image from the given path- Parameters:
imagePath
- the path, either file or http- Returns:
- the loaded image
- Throws:
IOException
- in case of an exception
-
getImageWidth
public final int getImageWidth()- Returns:
- the image width
-
getImageHeight
public final int getImageHeight()- Returns:
- the image height
-
getZoomDevice
Gets the current zoom device.
- Returns:
- the ZoomDevice
-
centerImage
Centers the image on the given image point- Parameters:
imagePoint
- the image point on which to center the image
-
centerImage
Centers the image on the given point on the panel, if it is within the image boundaries.- Parameters:
panelPoint
- the point on which to center the image
-
isWithinImage
Tests whether a given point in the panel falls within the image boundaries.- Parameters:
panelPoint
- the point on the panel- Returns:
- true if an image is available and the given point is within the image
-
setImage
Sets an image for display in the panel.
- Parameters:
image
- an image to be set in the panel- Throws:
IllegalArgumentException
- in case the image height and/or width is zero
-
getImage
- Returns:
- the image being displayed, null if none
-
getScale
public final double getScale()- Returns:
- the zoom scale
-
resetView
public final void resetView()Resets the view so the image fits the panel -
panelToImagePoint
Converts this panel's point into the original image point- Parameters:
panelPoint
- the panel point- Returns:
- the image point
-
imageToPanelPoint
Converts the original image point into this panel's point- Parameters:
imagePoint
- the image point- Returns:
- the panel point
-
isHighQualityRenderingEnabled
public final boolean isHighQualityRenderingEnabled()Indicates whether the high quality rendering feature is enabled.
- Returns:
- true if high quality rendering is enabled, false otherwise.
-
setHighQualityRenderingEnabled
public final void setHighQualityRenderingEnabled(boolean enabled) Enables/disables high quality rendering.
- Parameters:
enabled
- enables/disables high quality rendering
-
isMoveImageEnabled
public final boolean isMoveImageEnabled() -
setMoveImageEnabled
public final void setMoveImageEnabled(boolean moveImageEnabled) -
getZoom
public final double getZoom()Gets the current zoom level.
- Returns:
- the current zoom level
-
setZoom
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.
- Parameters:
newZoom
- the zoom level used to display this panel's image.
-
setZoom
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.
- Parameters:
newZoom
- the zoom level used to display this panel's image.zoomingCenter
- the zooming center
-
getZoomIncrement
public final double getZoomIncrement()Gets the current zoom increment.
- Returns:
- the current zoom increment
-
setZoomIncrement
public final void setZoomIncrement(double newZoomIncrement) Sets a new zoom increment value.
- Parameters:
newZoomIncrement
- new zoom increment value
-
getImageOrigin
Gets the image origin.
Image origin is defined as the upper, left corner of the image in the panel's coordinate system.
- Returns:
- the point of the upper, left corner of the image in the panel's coordinates system.
-
setImageOrigin
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.
- Parameters:
x
- the x coordinate of the new image originy
- the y coordinate of the new image origin
-
setImageOrigin
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.
- Parameters:
newOrigin
- the value of a new image origin
-
paintComponent
Paints the panel and its image at the current zoom level, location, and interpolation method dependent on the image scale.- Overrides:
paintComponent
in classJComponent
- Parameters:
g
- theGraphics
context for painting
-