ucar.jpeg.jj2000.disp
Class ImgScrollPane

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by ucar.jpeg.jj2000.disp.ImgScrollPane
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class ImgScrollPane
extends Container

This class implements an image viewer that can display an image larger than the actual display area, and presents scrollbars to scroll the viewable area. This class also supports zooming in and out the image, with no extra memory requirements.

The zoom factor by default is 1. It can be changed with the 'zoom()' and 'setZoom()' methods. The maximum zoom factor is defined by MAX_ZOOM.

The zoom scaling is done directly by the AWT display system. In general it is performed by dropping or repeating lines. It is just intended to provide a display zoom and not for proper scaling of an image.

The scrolling can be performed by copying the actual displayed data to the new scrolled position and redrawing the damaged parts, or by redrawing the entire displayed image portion at the new scrolled position. Which is more efficient depends on the JVM and working environment. By default it is done by copying since it tends to provide less annoying visual artifacts while scrolling, but that can be changed with 'setCopyScroll()'.

This class is very similar to the AWT ScrollPane one, but it is optimized for display of large images and does not suffer from the problems of ScrollPane when changing zoom. The Adjustable elements that represent the scrollbars are made available as in ScrollPane, but the minimum, maximum, visible amount and block increment should not be set (IllegalArgumentException is thrown if attempted), since they are set internally by this class.

Focus and key event listeners that are registered are in fact registered with the components that implement the three areas (the image display and the two scrollbars) so that if any such event is fired in any of these areas it is handled by the registered listener.

Mouse and mouse movement event listeners that are registered are in fact registered with the image display component only. The mouse and mouse movement events on the scrollbars are handled by the Scrollbar default listeners only.

Although it is implemented as a container, it behaves like a component. Specifically no components can be added or removed from objects of this class. Furthermore, no layout manager can be set. It is internally set and it can not be changed.

The implementation uses a lightweight container with an inner class to display the image itself, and two scrollbars. The layout manager is a BorderLayout.

This class should be really implemented as a Component, but it is implemented as a Container for easyness. It should not be assumed it is a subclass of Container since in the future it might be rewritten as a subclass of Component only.

See Also:
ScrollPane, Adjustable, Serialized Form

Nested Class Summary
 
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
static float MAX_ZOOM
          The maximum possible zoom factor: 32.
static int SCROLLBARS_ALWAYS
          The ID for always visible scrollbars
static int SCROLLBARS_AS_NEEDED
          The ID for as needed visible scrollbars
static int SCROLLBARS_NEVER
          The ID for never visible scrollbars
 
Fields inherited from class java.awt.Component
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
ImgScrollPane()
          Creates a new ImgScrollPane with SCROLLBARS_AS_NEEDED scrollbars.
ImgScrollPane(int svt)
          Creates a new ImgScrollPane with the specified type of scrollbar visibility.
 
Method Summary
 Component add(Component comp)
          Throws an IllegalArgumentException since no components can be added to this container.
 Component add(Component comp, int index)
          Throws an IllegalArgumentException since no components can be added to this container.
 void add(Component comp, Object constraints)
          Throws an IllegalArgumentException since no components can be added to this container.
 void add(Component comp, Object constraints, int index)
          Throws an IllegalArgumentException since no components can be added to this container.
 Component add(String name, Component comp)
          Throws an IllegalArgumentException since no components can be added to this container.
 void addFocusListener(FocusListener l)
          Adds the specified focus listener to receive focus events from this component.
 void addKeyListener(KeyListener l)
          Adds the specified key listener to receive key events from this component.
 void addMouseListener(MouseListener l)
          Adds the specified mouse listener to receive mouse events from this component.
 void addMouseMotionListener(MouseMotionListener l)
          Adds the specified mouse motion listener to receive mouse motion events from this component.
 void doLayout()
          Causes this container to lay out its components.
 boolean getCopyScroll()
          Returns true if the scrolling is done by copying.
 Adjustable getHAdjustable()
          Returns the Adjustable object which represents the state of the horizontal scrollbar.
 Image getImage()
          Returns the image that is displayed in this component.
 int getScrollbarDisplayPolicy()
          Returns the display policy for the scrollbars.
 Point getScrollPosition()
          Returns the current x,y position within the child which is displayed at the 0,0 location of the scrolled panel's view port.
 Adjustable getVAdjustable()
          Returns the Adjustable object which represents the state of the vertical scrollbar.
 Dimension getViewportSize()
          Returns the current size of the image scroll pane's view port.
 float getZoom()
          Returns the current zoom factor.
 void remove(Component comp)
          Throws an IllegalArgumentException since the components should never be removed from this container.
 void remove(int index)
          Throws an IllegalArgumentException since the components should never be removed from this container.
 void removeAll()
          Throws an IllegalArgumentException since the components should never be removed from this container.
 void removeFocusListener(FocusListener l)
          Removes the specified focus listener so that it no longer receives focus events from this component.
 void removeKeyListener(KeyListener l)
          Removes the specified key listener so that it no longer receives key events from this component.
 void removeMouseListener(MouseListener l)
          Removes the specified mouse listener so that it no longer receives mouse events from this component.
 void removeMouseMotionListener(MouseMotionListener l)
          Removes the specified mouse motion listener so that it no longer receives mouse motion events from this component.
 void setBackground(Color c)
          Sets the background color of this component.
 void setCopyScroll(boolean v)
          Sets if the scrolling is to be done by copying and redrawing of damaged parts of the displayed image.
 void setCursor(Cursor cursor)
          Set the cursor image to a predefined cursor.
 void setEnabled(boolean b)
          Enables or disables this component, depending on the value of the parameter b.
 void setForeground(Color c)
          Sets the foreground color of this component.
 void setImage(Image img)
          Sets the image to display in this component.
 void setLayout(LayoutManager mgr)
          Throws an IllegalArgumentException since the layout manager is internally set and can not be changed.
 void setScrollbarDisplayPolicy(int v)
          Sets the display policy for the scrollbars.
 void setScrollPosition(int x, int y)
          Scrolls to the specified position within the image.
 void setScrollPosition(Point p)
          Scrolls to the specified position within the image.
 void setZoom(float zf)
          Sets the zoom factor to display the image.
 void zoom(float zm)
          Modifies the current zoom factor by the given multiplier.
 
Methods inherited from class java.awt.Container
addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, update, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBounds, setBounds, setComponentOrientation, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SCROLLBARS_ALWAYS

public static final int SCROLLBARS_ALWAYS
The ID for always visible scrollbars

See Also:
Constant Field Values

SCROLLBARS_AS_NEEDED

public static final int SCROLLBARS_AS_NEEDED
The ID for as needed visible scrollbars

See Also:
Constant Field Values

SCROLLBARS_NEVER

public static final int SCROLLBARS_NEVER
The ID for never visible scrollbars

See Also:
Constant Field Values

MAX_ZOOM

public static final float MAX_ZOOM
The maximum possible zoom factor: 32.

See Also:
Constant Field Values
Constructor Detail

ImgScrollPane

public ImgScrollPane()
Creates a new ImgScrollPane with SCROLLBARS_AS_NEEDED scrollbars.


ImgScrollPane

public ImgScrollPane(int svt)
Creates a new ImgScrollPane with the specified type of scrollbar visibility.

Parameters:
svt - The scrollbar visibility type
Method Detail

setImage

public void setImage(Image img)
Sets the image to display in this component. If the image is not ready for display it will be prepared in the current thread. The current zoom factor applies.

If the image is not ready for display (i.e. it has not been rendered at its natural size) it will be rendered in the current thread, if not being already rendered in another one. This means that the current thread can block until the image is ready.

If the image is rendered incrementally (it depends on the underlying 'ImageProducer') it will be displayed in that way if the incremental display is set for the Component class. See the 'imageUpdate()' method of the 'Component' class.

If the image is the same as the current one nothing is done.

Parameters:
img - The image to display.
See Also:
Component.imageUpdate(java.awt.Image, int, int, int, int, int)

getImage

public Image getImage()
Returns the image that is displayed in this component.

Returns:
The image displayed in this component, or null if none.

setZoom

public void setZoom(float zf)
Sets the zoom factor to display the image. A zoom factor larger than 1 corresponds to a zoom in. A factor of 1 corresponds to no scaling. After setting the zoom factor the component is invalidated and 'repaint()' is automatically called so that the image is redrawn at the new zoom factor. In order to revalidate the layout 'validate()' should be called on one of the parent containers. If the new zoom factor is larger than MAX_ZOOM, then MAX_ZOOM will be used.

Parameters:
zf - The zoom factor

zoom

public void zoom(float zm)
Modifies the current zoom factor by the given multiplier. After setting the zoom factor the component is invalidated and 'repaint()' is automatically called so that the image is redrawn at the new zoom factor. In order to revalidate the layout 'validate()' should be called on one of the parent containers. If the resulting zoom factor is larger than MAX_ZOOM, then MAX_ZOOM will be used.

Parameters:
zm - The zoom multiplier to apply.

getZoom

public float getZoom()
Returns the current zoom factor.

Returns:
The current zoom factor

getHAdjustable

public Adjustable getHAdjustable()
Returns the Adjustable object which represents the state of the horizontal scrollbar.


getVAdjustable

public Adjustable getVAdjustable()
Returns the Adjustable object which represents the state of the vertical scrollbar.


getScrollbarDisplayPolicy

public int getScrollbarDisplayPolicy()
Returns the display policy for the scrollbars.

Returns:
the display policy for the scrollbars

setScrollbarDisplayPolicy

public void setScrollbarDisplayPolicy(int v)
Sets the display policy for the scrollbars.

Parameters:
v - the display policy for the scrollbars

setScrollPosition

public void setScrollPosition(int x,
                              int y)
Scrolls to the specified position within the image. Specifying a position outside of the legal scrolling bounds of the image will scroll to the closest legal position. This is a convenience method which interfaces with the Adjustable objects which represent the state of the scrollbars.

Parameters:
x - the x position to scroll to
y - the y position to scroll to

setScrollPosition

public void setScrollPosition(Point p)
Scrolls to the specified position within the image. Specifying a position outside of the legal scrolling bounds of the image will scroll to the closest legal position. This is a convenience method which interfaces with the Adjustable objects which represent the state of the scrollbars.

Parameters:
p - the position to scroll to

getScrollPosition

public Point getScrollPosition()
Returns the current x,y position within the child which is displayed at the 0,0 location of the scrolled panel's view port. This is a convenience method which interfaces with the adjustable objects which represent the state of the scrollbars.

Returns:
the coordinate position for the current scroll position

getViewportSize

public Dimension getViewportSize()
Returns the current size of the image scroll pane's view port. This is the size of the image display area. If this component has not been layed out yet the value is not defined.

Returns:
The size of the image display area

setCopyScroll

public void setCopyScroll(boolean v)
Sets if the scrolling is to be done by copying and redrawing of damaged parts of the displayed image. Otherwise it is done by redrawing the entire displayed image. In general copy scrolling is faster and produces less annoying effects. See the class description.

Parameters:
v - If true scrolling will be done by copying.

getCopyScroll

public boolean getCopyScroll()
Returns true if the scrolling is done by copying.

Returns:
If the copy is done by scrolling

doLayout

public void doLayout()
Causes this container to lay out its components. Most programs should not call this method directly, but should invoke the validate method instead.

Overrides:
doLayout in class Container

addFocusListener

public void addFocusListener(FocusListener l)
Adds the specified focus listener to receive focus events from this component. It is added to the image and scrollbar areas.

Overrides:
addFocusListener in class Component
Parameters:
l - the focus listener

removeFocusListener

public void removeFocusListener(FocusListener l)
Removes the specified focus listener so that it no longer receives focus events from this component.

Overrides:
removeFocusListener in class Component
Parameters:
l - the focus listener

addKeyListener

public void addKeyListener(KeyListener l)
Adds the specified key listener to receive key events from this component. It is added to the image and scrollbar areas.

Overrides:
addKeyListener in class Component
Parameters:
l - the key listener

removeKeyListener

public void removeKeyListener(KeyListener l)
Removes the specified key listener so that it no longer receives key events from this component.

Overrides:
removeKeyListener in class Component
Parameters:
l - the key listener

addMouseListener

public void addMouseListener(MouseListener l)
Adds the specified mouse listener to receive mouse events from this component. It is actually added to the image area only and not to the scrollbar areas.

Overrides:
addMouseListener in class Component
Parameters:
l - the mouse listener

removeMouseListener

public void removeMouseListener(MouseListener l)
Removes the specified mouse listener so that it no longer receives mouse events from this component.

Overrides:
removeMouseListener in class Component
Parameters:
l - the mouse listener

addMouseMotionListener

public void addMouseMotionListener(MouseMotionListener l)
Adds the specified mouse motion listener to receive mouse motion events from this component. It is actually added to the image area only and not to the scrollbar areas.

Overrides:
addMouseMotionListener in class Component
Parameters:
l - the mouse motion listener

removeMouseMotionListener

public void removeMouseMotionListener(MouseMotionListener l)
Removes the specified mouse motion listener so that it no longer receives mouse motion events from this component.

Overrides:
removeMouseMotionListener in class Component
Parameters:
l - the mouse motion listener

setBackground

public void setBackground(Color c)
Sets the background color of this component. It sets the background of the 3 areas (image and scrollbars) plus the container itself.

Overrides:
setBackground in class Component
Parameters:
c - The color to become background color for this component

setCursor

public void setCursor(Cursor cursor)
Set the cursor image to a predefined cursor. It sets the cursor of the image area and this container to the specified one. It does not set the cursor of the scrollbars.

Overrides:
setCursor in class Component
Parameters:
cursor - One of the constants defined by the Cursor class.

setEnabled

public void setEnabled(boolean b)
Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially by default.

Overrides:
setEnabled in class Component
Parameters:
b - If true, this component is enabled; otherwise this component is disabled.

setForeground

public void setForeground(Color c)
Sets the foreground color of this component. It sets the foreground of the 3 areas (image display and scrollbars) plus this contaioner's foreground.

Overrides:
setForeground in class Component
Parameters:
c - The color to become this component's foreground color.

add

public Component add(Component comp)
Throws an IllegalArgumentException since no components can be added to this container.

Overrides:
add in class Container

add

public Component add(String name,
                     Component comp)
Throws an IllegalArgumentException since no components can be added to this container.

Overrides:
add in class Container

add

public Component add(Component comp,
                     int index)
Throws an IllegalArgumentException since no components can be added to this container.

Overrides:
add in class Container

add

public void add(Component comp,
                Object constraints)
Throws an IllegalArgumentException since no components can be added to this container.

Overrides:
add in class Container

add

public void add(Component comp,
                Object constraints,
                int index)
Throws an IllegalArgumentException since no components can be added to this container.

Overrides:
add in class Container

remove

public void remove(int index)
Throws an IllegalArgumentException since the components should never be removed from this container.

Overrides:
remove in class Container

remove

public void remove(Component comp)
Throws an IllegalArgumentException since the components should never be removed from this container.

Overrides:
remove in class Container

removeAll

public void removeAll()
Throws an IllegalArgumentException since the components should never be removed from this container.

Overrides:
removeAll in class Container

setLayout

public void setLayout(LayoutManager mgr)
Throws an IllegalArgumentException since the layout manager is internally set and can not be changed.

Overrides:
setLayout in class Container


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.