QJCC homepage

biz.chitec.quarterback.swing
Class ScalingImagePane

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended bybiz.chitec.quarterback.swing.ScalingImagePane
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.swing.SwingConstants

public class ScalingImagePane
extends javax.swing.JComponent
implements javax.swing.SwingConstants

A JComponent-derived panel to show an image scaled to fit with respect to aspect ratio. Image is drawn at the given place in the component. A maximum scale can be given. On resizing the component, the image is rescaled if new scale differs above a threshold from old scale.

ScalingImagePane is aware of its insets and scales and places the image accordingly. So, it can be given a border.

As an additional capability, the painted image can be given a rectangular selection which is automatically transformed into the image's logic coordinate space. To enable this feature, just use setRectangleSelectable(true). The pane fires property events if a selection is done with the mouse.

This component makes extensive use of the AWT image drawing capabilities.

Version:
$Id: 4d4b0e5771591a7ca4f7a6f974e5fab8ad15afac $
Author:
chitec/Dirk Hillbrecht 2002
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
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
private  java.awt.Image baseimage
          The image to be shown
private  double currentscale
          The current scale in which the image is shown
private  java.awt.Rectangle drawrect
           
private  int horizalign
          horizontal alignment of the image
private  int imageheight
          Height of the image
private  int imagewidth
          Width of the image.
private  boolean isrectangleselectable
           
private  boolean isrectangleselected
           
private  java.awt.Rectangle logrect
           
private  double maximumscale
          Maximum scale up to which the image should be enlarged.
private  double maxthreshold
          Maximum threshold above which the scaling change must grow before rescaling is done.
private  double minthreshold
          Minimum threshold below which the scaling change must shrink before rescaling is done.
private  java.awt.Rectangle phyrect
           
private  int scaledheight
          height of the scaled version of the image
private  int scaledwidth
          width of the scaled version of the image
private  int scaledx
          x point where to draw the scaled version of the image
private  int scaledy
          y point where to draw the scaled version of the image
private  int vertalign
          vertical alignment of the image
 
Fields inherited from class javax.swing.JComponent
accessibleContext, 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.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ScalingImagePane()
          Creates a centering ScalingImagePane, maximum scale set to 1, threshold interval (1,1).
ScalingImagePane(int ha, int va)
          Create a scaling image pane with maximum scale 1 (no enlargement) and threshold interval (1,1).
ScalingImagePane(int ha, int va, double mxs)
          Create a scaling image pane with threshold interval set to (1,1) and no image.
ScalingImagePane(int ha, int va, double mxs, double minth, double maxth)
          Create a scaling image pane without image.
ScalingImagePane(int ha, int va, double mxs, double minth, double maxth, java.awt.Image img)
          Create a ScalingImagePane.
 
Method Summary
private  void endRectangle()
           
private  void followRectangle(java.awt.event.MouseEvent e)
           
 int getHorizontalAlignment()
           
 java.awt.Image getImage()
           
 double getMaximumScale()
           
 double getMaximumThreshold()
           
 double getMinimumThreshold()
           
 java.awt.Rectangle getSelectedRectangle()
           
 int getVerticalAlignment()
           
 boolean isRectangleSelectable()
           
 boolean isRectangleSelected()
           
 void paint(java.awt.Graphics g)
          Paint the component.
private  void refreshLogRect()
           
private  void refreshPhyRect()
           
 void rescale()
          Rescales the image.
 void setHorizontalAlignment(int ha)
           
 void setImage(java.awt.Image image)
          Sets the image.
 void setMaximumScale(double newmaxscale)
           
 void setMaximumThreshold(double nt)
           
 void setMinimumThreshold(double nt)
           
 void setRectangleSelectable(boolean b)
           
 void setVerticalAlignment(int ha)
           
private  void startRectangle(java.awt.event.MouseEvent e)
           
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, 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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, 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

baseimage

private java.awt.Image baseimage
The image to be shown


currentscale

private double currentscale
The current scale in which the image is shown


imagewidth

private int imagewidth
Width of the image.


imageheight

private int imageheight
Height of the image


scaledx

private int scaledx
x point where to draw the scaled version of the image


scaledy

private int scaledy
y point where to draw the scaled version of the image


scaledwidth

private int scaledwidth
width of the scaled version of the image


scaledheight

private int scaledheight
height of the scaled version of the image


horizalign

private int horizalign
horizontal alignment of the image


vertalign

private int vertalign
vertical alignment of the image


maximumscale

private double maximumscale
Maximum scale up to which the image should be enlarged. If there is more space, keep the size as it is.


minthreshold

private double minthreshold
Minimum threshold below which the scaling change must shrink before rescaling is done.


maxthreshold

private double maxthreshold
Maximum threshold above which the scaling change must grow before rescaling is done.


isrectangleselectable

private boolean isrectangleselectable

phyrect

private java.awt.Rectangle phyrect

drawrect

private java.awt.Rectangle drawrect

logrect

private java.awt.Rectangle logrect

isrectangleselected

private boolean isrectangleselected
Constructor Detail

ScalingImagePane

public ScalingImagePane(int ha,
                        int va,
                        double mxs,
                        double minth,
                        double maxth,
                        java.awt.Image img)
Create a ScalingImagePane. The component shows the given Image object as large as possible with respect to its own size and the aspect ratio of the image. The image is drawn at the appropriate relative place according to the alignment hints. It is not scaled above the given maximum scale.

If the ScalingImagePane is resized, the scale of the Image is recomputed. New and old scale are compared. If their relation is between the two threshold values, no rescaling is taking place.

Parameters:
ha - Horizontal alignment: LEFT, CENTER, RIGHT
va - Vertical alignment: TOP, CENTER, BOTTOM
mxs - Maximum scaling factor up to which the image is enlarged
minth - Minimum threshold of scaling factor
maxth - Maximum threshold of scaling factor
img - The image to be shown

ScalingImagePane

public ScalingImagePane(int ha,
                        int va,
                        double mxs,
                        double minth,
                        double maxth)
Create a scaling image pane without image.


ScalingImagePane

public ScalingImagePane(int ha,
                        int va,
                        double mxs)
Create a scaling image pane with threshold interval set to (1,1) and no image.


ScalingImagePane

public ScalingImagePane(int ha,
                        int va)
Create a scaling image pane with maximum scale 1 (no enlargement) and threshold interval (1,1).


ScalingImagePane

public ScalingImagePane()
Creates a centering ScalingImagePane, maximum scale set to 1, threshold interval (1,1).

Method Detail

setHorizontalAlignment

public void setHorizontalAlignment(int ha)

getHorizontalAlignment

public int getHorizontalAlignment()

setVerticalAlignment

public void setVerticalAlignment(int ha)

getVerticalAlignment

public int getVerticalAlignment()

setMaximumScale

public void setMaximumScale(double newmaxscale)

getMaximumScale

public double getMaximumScale()

setMinimumThreshold

public void setMinimumThreshold(double nt)

getMinimumThreshold

public double getMinimumThreshold()

setMaximumThreshold

public void setMaximumThreshold(double nt)

getMaximumThreshold

public double getMaximumThreshold()

setRectangleSelectable

public void setRectangleSelectable(boolean b)

isRectangleSelectable

public boolean isRectangleSelectable()

isRectangleSelected

public boolean isRectangleSelected()

getSelectedRectangle

public java.awt.Rectangle getSelectedRectangle()

setImage

public void setImage(java.awt.Image image)
Sets the image. Does not do anything if same image is already used. Initiates rescaling and redrawing.


getImage

public java.awt.Image getImage()

rescale

public void rescale()
Rescales the image. Call this method if you change alignments, maximum scale or thresholds (or several of them). Otherwise, changes will not take effect.


paint

public void paint(java.awt.Graphics g)
Paint the component. All the work is done by Graphics.drawImage.


startRectangle

private void startRectangle(java.awt.event.MouseEvent e)

followRectangle

private void followRectangle(java.awt.event.MouseEvent e)

endRectangle

private void endRectangle()

refreshLogRect

private void refreshLogRect()

refreshPhyRect

private void refreshPhyRect()

QJCC homepage