Copyright 2011-01-06 Werner Randelshofer

ch.randelshofer.gui
Class ImagePanel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by ch.randelshofer.gui.ImagePanel
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class ImagePanel
extends javax.swing.JComponent

Displays images on a panel.

Version:
2009-12-25 Display multi-line messages.
2009-12-24 Request focus on mouse click.
2006-10-01 Fixed message displaying.
2006-07-23 Support for 16:9 anamorph pixel aspect added.
1.3.1 2005-01-22 Changing the pixel aspect triggers validation of the parent component.
1.3 2004-12-25 Access methods for rendering hints added. Method setTexture added. Underline suffixes from instance variables removed.
1.2 2002-04-02 Workaround for MRJ 1.3.1 Update 1 on Mac OS X.
1.1 2000-09-28 Does not update scale factor property during paints anymore.
1.0 1999-10-19
Author:
Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland.
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
static int ANAMORPH_PIXEL_ASPECT
          Pixel aspect policy: Use Anamoprh 16:9 pixel aspect (=16/9*4/5=1.4222).
static int EXACT_PIXEL_ASPECT
          Pixel aspect policy: Preserve the exact pixel aspect.
static int IGNORE_PIXEL_ASPECT
          Pixel aspect policy: Ignore pixel aspect.
static int ROUNDED_PIXEL_ASPECT
          Pixel aspect policy: Preserves only extreme pixel aspects, where one dimension is a multiple of the other dimension.
static int SCALE_TO_IMAGE_ASPECT
          Image scaling policy: Scale to panel but keep the image aspect.
static int SCALE_TO_IMAGE_SIZE
          Image scaling policy: Scale as defined by #setAspectRatio and the image aspect.
static int SCALE_TO_VIEW_SIZE
          Image scaling policy: Scale to panel size.
 
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.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
ImagePanel()
           
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Adds a listener who is interested in changes of this object.
 double getAspectRatioX()
          Gets the horizontal scale factor that is used to scale images shown in this image panel.
 double getAspectRatioY()
          Gets the vertical scale factor that is used to scale images shown in this image panel.
 java.awt.Image getImage()
          Gets the image that is displayed in this image panel.
 int getImageScalePolicy()
          Returns the image scale policy.
 int getPixelAspectPolicy()
          Returns the pixel aspect policy.
 double getPixelAspectX()
          Gets the horizontal pixel aspect of the image according to the pixel aspect policy that is in affect.
 double getPixelAspectY()
          Gets the vertical pixel aspect of the image according to the pixel aspect policy that is in effect.
 java.awt.Dimension getPreferredImageSize()
          Gets the preferred image size.
 java.awt.Dimension getPreferredSize()
          Gets the preferred size of this image panel.
 java.awt.RenderingHints getRenderingHints()
          Gets the rendering hints.
 java.awt.Dimension getScaledImageSize()
          Gets the scaled and pixel aspect corrected image size.
 double getScaleFactor()
          Gets the scale factor.
 java.awt.image.BufferedImage getTexture()
          Gets the backdrop paint.
 boolean imageUpdate(java.awt.Image img, int flags, int x, int y, int w, int h)
          XXX Netscape gets very slow when painting all SOMEBITS of an image.
 void paintComponent(java.awt.Graphics gr)
           
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Removes a previously registered listener.
 void setAspectRatio(double ratioX, double ratioY)
          Sets the aspect ratio.
protected  void setAspectRatio0(double ratioX, double ratioY)
           
 void setImage(java.awt.Image image)
          Sets the image and displays it in this image panel.
 void setImageScalePolicy(int policy)
          Sets the image scale policy.
 void setMessage(java.lang.String message)
           
 void setPixelAspectPolicy(int policy)
          Sets the pixel aspect policy.
 void setRenderingHints(java.awt.RenderingHints newValue)
          Sets the rendering hints.
 void setScaleFactor(double scaleFactor)
          Sets the scale factor.
 void setTexture(java.awt.image.BufferedImage newValue)
          Sets the texture for the backdrop paint.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, 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, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, 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, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, 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, 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, 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, hide, 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, 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

IGNORE_PIXEL_ASPECT

public static final int IGNORE_PIXEL_ASPECT
Pixel aspect policy: Ignore pixel aspect.

See Also:
Constant Field Values

ROUNDED_PIXEL_ASPECT

public static final int ROUNDED_PIXEL_ASPECT
Pixel aspect policy: Preserves only extreme pixel aspects, where one dimension is a multiple of the other dimension.

See Also:
Constant Field Values

EXACT_PIXEL_ASPECT

public static final int EXACT_PIXEL_ASPECT
Pixel aspect policy: Preserve the exact pixel aspect.

See Also:
Constant Field Values

ANAMORPH_PIXEL_ASPECT

public static final int ANAMORPH_PIXEL_ASPECT
Pixel aspect policy: Use Anamoprh 16:9 pixel aspect (=16/9*4/5=1.4222).

See Also:
Constant Field Values

SCALE_TO_IMAGE_SIZE

public static final int SCALE_TO_IMAGE_SIZE
Image scaling policy: Scale as defined by #setAspectRatio and the image aspect.

See Also:
Constant Field Values

SCALE_TO_VIEW_SIZE

public static final int SCALE_TO_VIEW_SIZE
Image scaling policy: Scale to panel size.

See Also:
Constant Field Values

SCALE_TO_IMAGE_ASPECT

public static final int SCALE_TO_IMAGE_ASPECT
Image scaling policy: Scale to panel but keep the image aspect.

See Also:
Constant Field Values
Constructor Detail

ImagePanel

public ImagePanel()
Method Detail

paintComponent

public void paintComponent(java.awt.Graphics gr)
Overrides:
paintComponent in class javax.swing.JComponent

setPixelAspectPolicy

public void setPixelAspectPolicy(int policy)
Sets the pixel aspect policy.

Parameters:
policy - PixelAspectPolicy must be one of IGNORE_PIXEL_ASPECT, ROUNDED_PIXEL_ASPECT, EXACT_PIXEL_ASPECT.
Throws:
java.lang.IllegalArgumentException - When passing invalid policy.

getPixelAspectPolicy

public int getPixelAspectPolicy()
Returns the pixel aspect policy.


setImageScalePolicy

public void setImageScalePolicy(int policy)
Sets the image scale policy.


getImageScalePolicy

public int getImageScalePolicy()
Returns the image scale policy.


getPixelAspectX

public double getPixelAspectX()
Gets the horizontal pixel aspect of the image according to the pixel aspect policy that is in affect.

Returns:
Horizontal pixel aspect.

getPixelAspectY

public double getPixelAspectY()
Gets the vertical pixel aspect of the image according to the pixel aspect policy that is in effect.

Returns:
Vertical pixel aspect.

getPreferredImageSize

public java.awt.Dimension getPreferredImageSize()
Gets the preferred image size.

Returns:
Image dimension after applying the pixel aspect policy.

getScaledImageSize

public java.awt.Dimension getScaledImageSize()
Gets the scaled and pixel aspect corrected image size.

Returns:
Image dimension after scaling and applying the pixel aspect policy.

setImage

public void setImage(java.awt.Image image)
Sets the image and displays it in this image panel.


getImage

public java.awt.Image getImage()
Gets the image that is displayed in this image panel.

Returns:
image.

setTexture

public void setTexture(java.awt.image.BufferedImage newValue)
Sets the texture for the backdrop paint.


getTexture

public java.awt.image.BufferedImage getTexture()
Gets the backdrop paint.


setScaleFactor

public void setScaleFactor(double scaleFactor)
Sets the scale factor. The scale factor scales images shown in this image panel.

Parameters:
scaleFactor - The scale factor.

getScaleFactor

public double getScaleFactor()
Gets the scale factor.

Returns:
Scale factor.

setRenderingHints

public void setRenderingHints(java.awt.RenderingHints newValue)
Sets the rendering hints.

Parameters:
newValue - The new rendering hints.

getRenderingHints

public java.awt.RenderingHints getRenderingHints()
Gets the rendering hints.

Returns:
Rendering Hints.

setAspectRatio

public void setAspectRatio(double ratioX,
                           double ratioY)
Sets the aspect ratio. The aspect ratio distorts images shown in this image panel.

Parameters:
ratioX - Horizontal scale factor.
ratioY - Vertical scale factor.

setAspectRatio0

protected void setAspectRatio0(double ratioX,
                               double ratioY)

getAspectRatioX

public double getAspectRatioX()
Gets the horizontal scale factor that is used to scale images shown in this image panel.

Returns:
Horizontal scale factor.

getAspectRatioY

public double getAspectRatioY()
Gets the vertical scale factor that is used to scale images shown in this image panel.

Returns:
Vertical scale factor.

getPreferredSize

public java.awt.Dimension getPreferredSize()
Gets the preferred size of this image panel. The preferred size depends on the image size, the scale factor and the pixel aspect policy.

Overrides:
getPreferredSize in class javax.swing.JComponent

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a listener who is interested in changes of this object.

Overrides:
addPropertyChangeListener in class java.awt.Container

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a previously registered listener.

Overrides:
removePropertyChangeListener in class java.awt.Component

setMessage

public void setMessage(java.lang.String message)

imageUpdate

public boolean imageUpdate(java.awt.Image img,
                           int flags,
                           int x,
                           int y,
                           int w,
                           int h)
XXX Netscape gets very slow when painting all SOMEBITS of an image.

Specified by:
imageUpdate in interface java.awt.image.ImageObserver
Overrides:
imageUpdate in class java.awt.Component

Copyright 2011-01-06 Werner Randelshofer