jj2000.disp
Class ImgScrollPane

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--jj2000.disp.ImgScrollPane
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class ImgScrollPane
extends java.awt.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
private  class ImgScrollPane.ImageScrollDisplay
          This class implements the component that displays the currently viewable image portion inside the ImgScrollPane.
(package private)  class ImgScrollPane.ISPScrollbar
          Scrollbars for the ImgScrollPane container.
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
(package private) static float BLOCK_INCREMENT_PROPORTION
          The propertion between the visible scrollbar length and the block increment amount: 0.8
private  boolean copyScroll
          If scrolling is to be done by copying ot not.
(package private)  ImgScrollPane.ISPScrollbar hsbar
          The horizontal scrollbar.
private  ImgScrollPane.ImageScrollDisplay imgDisplay
          The image display
(package private) static int INTERNAL_GAP
          The inetrnal gap between the elements, in pixels: 0
private  java.awt.Dimension lastSize
          The viewable size used in the last scrollbar calculation.
private  float lastZoom
          The zoom used in the last scrollbar calculation.
static float MAX_ZOOM
          The maximum possible zoom factor: 32.
private  int sbType
          The scrollbar type (always, as needed, etc.)
(package private) static int SCROLLBAR_THICKNESS
          The thickness of the scrollbars: 16 pixels
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
(package private)  ImgScrollPane.ISPScrollbar vsbar
          The vertical scrollabr.
private  float zoom
          The zoom to use in displaying the image.
 
Fields inherited from class java.awt.Container
 
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
 java.awt.Component add(java.awt.Component comp)
          Throws an IllegalArgumentException since no components can be added to this container.
 java.awt.Component add(java.awt.Component comp, int index)
          Throws an IllegalArgumentException since no components can be added to this container.
 void add(java.awt.Component comp, java.lang.Object constraints)
          Throws an IllegalArgumentException since no components can be added to this container.
 void add(java.awt.Component comp, java.lang.Object constraints, int index)
          Throws an IllegalArgumentException since no components can be added to this container.
 java.awt.Component add(java.lang.String name, java.awt.Component comp)
          Throws an IllegalArgumentException since no components can be added to this container.
 void addFocusListener(java.awt.event.FocusListener l)
          Adds the specified focus listener to receive focus events from this component.
 void addKeyListener(java.awt.event.KeyListener l)
          Adds the specified key listener to receive key events from this component.
 void addMouseListener(java.awt.event.MouseListener l)
          Adds the specified mouse listener to receive mouse events from this component.
 void addMouseMotionListener(java.awt.event.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.
 java.awt.Adjustable getHAdjustable()
          Returns the Adjustable object which represents the state of the horizontal scrollbar.
 java.awt.Image getImage()
          Returns the image that is displayed in this component.
 int getScrollbarDisplayPolicy()
          Returns the display policy for the scrollbars.
 java.awt.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.
 java.awt.Adjustable getVAdjustable()
          Returns the Adjustable object which represents the state of the vertical scrollbar.
 java.awt.Dimension getViewportSize()
          Returns the current size of the image scroll pane's view port.
 float getZoom()
          Returns the current zoom factor.
 void remove(java.awt.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(java.awt.event.FocusListener l)
          Removes the specified focus listener so that it no longer receives focus events from this component.
 void removeKeyListener(java.awt.event.KeyListener l)
          Removes the specified key listener so that it no longer receives key events from this component.
 void removeMouseListener(java.awt.event.MouseListener l)
          Removes the specified mouse listener so that it no longer receives mouse events from this component.
 void removeMouseMotionListener(java.awt.event.MouseMotionListener l)
          Removes the specified mouse motion listener so that it no longer receives mouse motion events from this component.
 void setBackground(java.awt.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(java.awt.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(java.awt.Color c)
          Sets the foreground color of this component.
 void setImage(java.awt.Image img)
          Sets the image to display in this component.
 void setLayout(java.awt.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.
private  void setScrollbars()
          Sets the scrollbars values, according to the image display area and image size.
 void setScrollPosition(int x, int y)
          Scrolls to the specified position within the image.
 void setScrollPosition(java.awt.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, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, removeContainerListener, removeNotify, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, transferFocusBackward, 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, getAccessibleContext, getBackground, 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, 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, isOpaque, 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, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, 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

SCROLLBAR_THICKNESS

static final int SCROLLBAR_THICKNESS
The thickness of the scrollbars: 16 pixels

See Also:
Constant Field Values

INTERNAL_GAP

static final int INTERNAL_GAP
The inetrnal gap between the elements, in pixels: 0

See Also:
Constant Field Values

BLOCK_INCREMENT_PROPORTION

static final float BLOCK_INCREMENT_PROPORTION
The propertion between the visible scrollbar length and the block increment amount: 0.8

See Also:
Constant Field Values

hsbar

ImgScrollPane.ISPScrollbar hsbar
The horizontal scrollbar.


vsbar

ImgScrollPane.ISPScrollbar vsbar
The vertical scrollabr.


imgDisplay

private ImgScrollPane.ImageScrollDisplay imgDisplay
The image display


sbType

private int sbType
The scrollbar type (always, as needed, etc.)


zoom

private float zoom
The zoom to use in displaying the image. A factor larger than one produces a zoom in effect.


lastZoom

private float lastZoom
The zoom used in the last scrollbar calculation.


lastSize

private java.awt.Dimension lastSize
The viewable size used in the last scrollbar calculation.


copyScroll

private boolean copyScroll
If scrolling is to be done by copying ot not. If not done by copying everything is redrawn.

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(java.awt.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 java.awt.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 java.awt.Adjustable getHAdjustable()
Returns the Adjustable object which represents the state of the horizontal scrollbar.


getVAdjustable

public java.awt.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(java.awt.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 java.awt.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 java.awt.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 java.awt.Container

addFocusListener

public void addFocusListener(java.awt.event.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 java.awt.Component
Parameters:
l - the focus listener

removeFocusListener

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

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

addKeyListener

public void addKeyListener(java.awt.event.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 java.awt.Component
Parameters:
l - the key listener

removeKeyListener

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

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

addMouseListener

public void addMouseListener(java.awt.event.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 java.awt.Component
Parameters:
l - the mouse listener

removeMouseListener

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

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

addMouseMotionListener

public void addMouseMotionListener(java.awt.event.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 java.awt.Component
Parameters:
l - the mouse motion listener

removeMouseMotionListener

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

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

setBackground

public void setBackground(java.awt.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 java.awt.Component
Parameters:
c - The color to become background color for this component

setCursor

public void setCursor(java.awt.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 java.awt.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 java.awt.Component
Parameters:
b - If true, this component is enabled; otherwise this component is disabled.

setForeground

public void setForeground(java.awt.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 java.awt.Component
Parameters:
c - The color to become this component's foreground color.

add

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

Overrides:
add in class java.awt.Container

add

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

Overrides:
add in class java.awt.Container

add

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

Overrides:
add in class java.awt.Container

add

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

Overrides:
add in class java.awt.Container

add

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

Overrides:
add in class java.awt.Container

remove

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

Overrides:
remove in class java.awt.Container

remove

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

Overrides:
remove in class java.awt.Container

removeAll

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

Overrides:
removeAll in class java.awt.Container

setLayout

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

Overrides:
setLayout in class java.awt.Container

setScrollbars

private void setScrollbars()
Sets the scrollbars values, according to the image display area and image size. The current scroll position is kept.