com.vistech.imageviewer
Class ImageCanvas

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--com.vistech.imageviewer.ImageCanvas
All Implemented Interfaces:
ImageDisplay, java.awt.image.ImageObserver, ImagePrint, java.awt.MenuContainer, java.awt.print.Printable, java.io.Serializable
Direct Known Subclasses:
ImageCanvas2D

public class ImageCanvas
extends javax.swing.JComponent
implements ImageDisplay, ImagePrint

A canvas class for displaying images. It can display the AWT Image as well as the BufferedImage objects.

Version:
1.0 18 Nov 1999
Author:
Lawrence Rodrigues
See Also:
Serialized Form

Inner classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Inner classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent
 
Field Summary
 java.awt.image.BufferedImage bufferedImage
           
protected  java.awt.Shape clipShape
           
protected  java.awt.Graphics2D dispGc
           
 java.awt.image.BufferedImage displayImage
           
protected  int displayMode
           
protected  int flipMode
           
protected  int height
           
protected  int imageHeight
           
protected  int imageType
           
protected  int imageWidth
           
protected  boolean invertOn
           
protected  java.awt.Graphics2D offScrGc
           
 java.awt.image.BufferedImage offScrImage
           
protected  int originalImageType
           
protected  boolean paintFlag
           
protected static int PRINT_SIZE_ACTUAL
           
protected static int PRINT_SIZE_PAGE
           
protected  java.awt.Point printLoc
           
protected  int printMode
           
protected  java.awt.Dimension printSize
           
protected  int width
           
 
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 com.vistech.imageviewer.ImageDisplay
TYPE_AWT_IMAGE, TYPE_BUFFERED_IMAGE
 
Fields inherited from interface java.awt.print.Printable
NO_SUCH_PAGE, PAGE_EXISTS
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ImageCanvas()
           
 
Method Summary
 void addImageSentListener(ImageSentListener p)
           
 void clearImageCanvas()
          Clears the image canvas.
static java.awt.Image convertToAWTImage(java.awt.image.BufferedImage bufImage)
           
static java.awt.image.BufferedImage convertToBufferedImage(java.awt.Image img, int wid, int ht, int type)
           
protected  void createDisplayImage()
          Creates the offScreen image.
protected  boolean doDisplayModeAndFlip(int imWid, int imHt)
          Applies the current display mode and flip mode settings to the displayed image.
static boolean doFlip(java.awt.image.BufferedImage img, java.awt.Graphics gc, int flipMode, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
          Flips a buffered image.
static boolean doFlip(java.awt.Image img, java.awt.Graphics gc, int flipMode, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2)
          Flips an AWT image.
 void draw(java.awt.Shape shape)
          Draws the specified shape on the destination image
 java.awt.Dimension drawingDim()
           
protected  void fireImageSentEvents(java.awt.image.BufferedImage img)
           
 java.awt.Image getAWTImage()
          Returns the image property.
 java.awt.image.BufferedImage getBufferedImage()
          Returns the BufferedImage property.
 java.awt.image.BufferedImage getDisplayedImage()
          Returns the image displayed on the canvas.
 java.awt.Graphics2D getDisplayedImageGC()
           
 int getDisplayMode()
          Returns the display mode.
 int getFlipMode()
          Returns the flip mode.
 java.awt.Dimension getImageSize()
          Returns the image size.
 int getImageType()
          Returns the image type property.
 boolean getInvert()
          Returns the invert mode.
 java.awt.image.BufferedImage getOffScreenImage()
          Returns the off screen image.
 java.awt.Dimension getPreferredSize()
           
 java.awt.Point getPrintLocation()
          Returns the ULHC of the print location.
 java.awt.Dimension getPrintSize()
          Returns the print size.
 boolean imageUpdate(java.awt.Image img, int infoflags, int x, int y, int wid, int ht)
           
protected  void init()
           
 void invert(boolean on)
          Inverts and image.
static void main(java.lang.String[] args)
           
 void paintComponent(java.awt.Graphics gc)
           
 boolean paintImage()
          Paints the currently set image.
 int print(java.awt.Graphics g, java.awt.print.PageFormat f, int pageIndex)
           
 void printImage(java.awt.Graphics2D g2d)
          Prints the image(s)drawn on the input graphics context.
protected  void redraw()
          creates a new off screen image and then redraws the current image over it.
 void removeImageSentListener(ImageSentListener p)
           
 void reset()
          Paints the currently displayed image again with currently set display and flip modes
 void setAWTImage(java.awt.Image image)
          Sets the image property.
 void setBufferedImage(java.awt.image.BufferedImage bimage)
          Sets the BufferedImage property.
 void setClip(java.awt.Shape clip)
          Sets the clip shape on the destination image
 void setDisplayImage(java.awt.image.BufferedImage image)
          Sets the image to be displayed on the canvas.
 void setDisplayMode(int mode)
          Sets the display mode.
 void setFlipMode(int mode)
          Sets the flip mode.
 void setImageType(int imageType)
          Sets the image type property.
 void setInvert(boolean onOrOff)
          Sets the invert mode property.
 void setOffScreenImage(java.awt.image.BufferedImage image)
          Sets the off screen image.
protected  void setOffScrGc()
          Sets off screen Graphics.
 void setPrintLocation(java.awt.Point d)
          Sets the ULHC location at which the printing starts.
 void setPrintSize(java.awt.Dimension d)
          Sets the size of the print.
 
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, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getUIClassID, getVerifyInputWhenFocusTarget, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, hide, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processFocusEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.vistech.imageviewer.ImageDisplay
addMouseListener, addMouseMotionListener, addPropertyChangeListener, addVetoableChangeListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removeVetoableChangeListener, setCursor
 

Field Detail

PRINT_SIZE_PAGE

protected static final int PRINT_SIZE_PAGE

PRINT_SIZE_ACTUAL

protected static final int PRINT_SIZE_ACTUAL

printMode

protected int printMode

originalImageType

protected int originalImageType

bufferedImage

public transient java.awt.image.BufferedImage bufferedImage

offScrImage

public transient java.awt.image.BufferedImage offScrImage

displayImage

public transient java.awt.image.BufferedImage displayImage

offScrGc

protected transient java.awt.Graphics2D offScrGc

dispGc

protected transient java.awt.Graphics2D dispGc

printLoc

protected java.awt.Point printLoc

printSize

protected java.awt.Dimension printSize

width

protected int width

height

protected int height

imageWidth

protected int imageWidth

imageHeight

protected int imageHeight

displayMode

protected int displayMode

flipMode

protected int flipMode

imageType

protected int imageType

clipShape

protected java.awt.Shape clipShape

invertOn

protected boolean invertOn

paintFlag

protected boolean paintFlag
Constructor Detail

ImageCanvas

public ImageCanvas()
Method Detail

init

protected void init()

getPreferredSize

public java.awt.Dimension getPreferredSize()
Overrides:
getPreferredSize in class javax.swing.JComponent

setAWTImage

public void setAWTImage(java.awt.Image image)
Description copied from interface: ImageDisplay
Sets the image property. If the input parameter is not null, the it becomes the current image. This is a bound property.
Specified by:
setAWTImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
image - the image object

getAWTImage

public java.awt.Image getAWTImage()
Description copied from interface: ImageDisplay
Returns the image property.
Specified by:
getAWTImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the image object

setBufferedImage

public void setBufferedImage(java.awt.image.BufferedImage bimage)
Description copied from interface: ImageDisplay
Sets the BufferedImage property. If the input parameter is not null, the it becomes the current image. This is a bound property.
Specified by:
setBufferedImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
BufferedImage - the image object

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage()
Description copied from interface: ImageDisplay
Returns the BufferedImage property. If the image loaded is of Image type, this method creates a BufferedImage from the original image object.
Specified by:
getBufferedImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the BufferedImage object

getImageSize

public java.awt.Dimension getImageSize()
Description copied from interface: ImageDisplay
Returns the image size.
Specified by:
getImageSize in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the image size.

convertToBufferedImage

public static java.awt.image.BufferedImage convertToBufferedImage(java.awt.Image img,
                                                                  int wid,
                                                                  int ht,
                                                                  int type)

convertToAWTImage

public static java.awt.Image convertToAWTImage(java.awt.image.BufferedImage bufImage)

setOffScreenImage

public void setOffScreenImage(java.awt.image.BufferedImage image)
Description copied from interface: ImageDisplay
Sets the off screen image.
Specified by:
setOffScreenImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
image - the off screen image.

getOffScreenImage

public java.awt.image.BufferedImage getOffScreenImage()
Description copied from interface: ImageDisplay
Returns the off screen image.
Specified by:
getOffScreenImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the off screen imge.

setDisplayImage

public void setDisplayImage(java.awt.image.BufferedImage image)
Description copied from interface: ImageDisplay
Sets the image to be displayed on the canvas. When an image is set this way, it is not saved. This method needs to be used for temporory display only.The correct way to set the image is to call the setAWTImage() or setBufferedImage() methods.
Specified by:
setDisplayImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
image - the image to be displayed.

getDisplayedImage

public java.awt.image.BufferedImage getDisplayedImage()
Description copied from interface: ImageDisplay
Returns the image displayed on the canvas.
Specified by:
getDisplayedImage in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the displayed imge.

getDisplayedImageGC

public java.awt.Graphics2D getDisplayedImageGC()

setImageType

public void setImageType(int imageType)
Description copied from interface: ImageDisplay
Sets the image type property.
Specified by:
setImageType in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
imageType - the image type.

getImageType

public int getImageType()
Description copied from interface: ImageDisplay
Returns the image type property.
Specified by:
getImageType in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the image type.

setDisplayMode

public void setDisplayMode(int mode)
Description copied from interface: ImageDisplay
Sets the display mode. This is a bound property . The input parameter can be one of NORMAL, ORIG_SIZE, TO_FIT, SCALED. These constants are defined in the DisplayMode class. Call to this method would reset the viewport and the image is redarwn with new display mode.
Specified by:
setDisplayMode in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
dispMode - the display mode

getDisplayMode

public int getDisplayMode()
Description copied from interface: ImageDisplay
Returns the display mode.
Specified by:
getDisplayMode in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the current display mode.

setFlipMode

public void setFlipMode(int mode)
Description copied from interface: ImageDisplay
Sets the flip mode. This is a bound property . The input parameter can be one of NORMAL, LEFT_RIGHT, TOP_BOTTOM, LEFT_RIGHT_TOP_BOTTON. These constants are defined in the FlipMode class. Call to this method would reset the viewport and the image is redraw with new flip mode.
Specified by:
setFlipMode in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
flipMode - the flip mode

getFlipMode

public int getFlipMode()
Description copied from interface: ImageDisplay
Returns the flip mode.
Specified by:
getFlipMode in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the current flip mode.

setInvert

public void setInvert(boolean onOrOff)
Description copied from interface: ImageDisplay
Sets the invert mode property. It fires the propertyChange events because it is a bound property. If set true, it inverts the image.
Specified by:
setInvert in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
onOrOff - the invert mode.

getInvert

public boolean getInvert()
Description copied from interface: ImageDisplay
Returns the invert mode.
Specified by:
getInvert in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Returns:
the invert mode

createDisplayImage

protected void createDisplayImage()
Creates the offScreen image. The ImageCanvas has to be visible for the offscreen image to be visible.

setOffScrGc

protected void setOffScrGc()
Sets off screen Graphics. The ImageCanvas has to be visible for the off screen screen to be available

drawingDim

public java.awt.Dimension drawingDim()

paintImage

public boolean paintImage()
Paints the currently set image. Applies the current display mode and filp modes before painting.
Specified by:
paintImage in interface ImageDisplay

doDisplayModeAndFlip

protected boolean doDisplayModeAndFlip(int imWid,
                                       int imHt)
Applies the current display mode and flip mode settings to the displayed image. First applies the display mode and then calls the doFlip() method to apply the flip mode to the resulting image. The display mode and flip mode settings are applied by using the appropriate drawImage() method (of Graphics class).
Parameters:
imWid - the image width.
imHt - the image height.

doFlip

public static boolean doFlip(java.awt.Image img,
                             java.awt.Graphics gc,
                             int flipMode,
                             int dx1,
                             int dy1,
                             int dx2,
                             int dy2,
                             int sx1,
                             int sy1,
                             int sx2,
                             int sy2)
Flips an AWT image. Draws the resulting image on the specified graphics context.
Parameters:
img - the image to be flipped.@see FlipMode
gc - the graphics context.
flipMode - the orientation of the flip.
dx1 - the x coordinate of the first corner of the destination image.
dy1 - the y coordinate of the first corner of the destination image.
dx2 - the x coordinate of the second corner of the destination image.
dy2 - the y coordinate of the second corner of the destination image.
dx1 - the x coordinate of the first corner of the source image.
dy1 - the y coordinate of the first corner of the source image.
dx2 - the x coordinate of the second corner of the source image.
dy2 - the y coordinate of the second corner of the source image.
Returns:
true if successful.

doFlip

public static boolean doFlip(java.awt.image.BufferedImage img,
                             java.awt.Graphics gc,
                             int flipMode,
                             int dx1,
                             int dy1,
                             int dx2,
                             int dy2,
                             int sx1,
                             int sy1,
                             int sx2,
                             int sy2)
Flips a buffered image. Draws the resulting image on the specified graphics context.
Parameters:
img - the image to be flipped.@see FlipMode
gc - the graphics context.
flipMode - the orientation of the flip.
dx1 - the x coordinate of the first corner of the destination image.
dy1 - the y coordinate of the first corner of the destination image.
dx2 - the x coordinate of the second corner of the destination image.
dy2 - the y coordinate of the second corner of the destination image.
dx1 - the x coordinate of the first corner of the source image.
dy1 - the y coordinate of the first corner of the source image.
dx2 - the x coordinate of the second corner of the source image.
dy2 - the y coordinate of the second corner of the source image.

invert

public void invert(boolean on)
Inverts and image.
Parameters:
on - If true, inverts the currently displayed image, else redraws the current image. Set the input to true to invert the image. Set the input to false to revert to the normal display mode.

setClip

public void setClip(java.awt.Shape clip)
Description copied from interface: ImageDisplay
Sets the clip shape on the destination image
Specified by:
setClip in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
clipshape - the shape of the clipping area

draw

public void draw(java.awt.Shape shape)
Description copied from interface: ImageDisplay
Draws the specified shape on the destination image
Specified by:
draw in interface ImageDisplay
Following copied from interface: com.vistech.imageviewer.ImageDisplay
Parameters:
shape - the shape to be drawn

imageUpdate

public boolean imageUpdate(java.awt.Image img,
                           int infoflags,
                           int x,
                           int y,
                           int wid,
                           int ht)
Overrides:
imageUpdate in class java.awt.Component

setPrintLocation

public void setPrintLocation(java.awt.Point d)
Description copied from interface: ImagePrint
Sets the ULHC location at which the printing starts.
Specified by:
setPrintLocation in interface ImagePrint
Following copied from interface: com.vistech.imageviewer.ImagePrint
Parameters:
d - the ULHC location.

getPrintLocation

public java.awt.Point getPrintLocation()
Description copied from interface: ImagePrint
Returns the ULHC of the print location.
Specified by:
getPrintLocation in interface ImagePrint
Following copied from interface: com.vistech.imageviewer.ImagePrint
Returns:
the print location

setPrintSize

public void setPrintSize(java.awt.Dimension d)
Description copied from interface: ImagePrint
Sets the size of the print.
Specified by:
setPrintSize in interface ImagePrint
Following copied from interface: com.vistech.imageviewer.ImagePrint
Parameters:
d - the print size.

getPrintSize

public java.awt.Dimension getPrintSize()
Description copied from interface: ImagePrint
Returns the print size.
Specified by:
getPrintSize in interface ImagePrint
Following copied from interface: com.vistech.imageviewer.ImagePrint
Returns:
the print size.

print

public int print(java.awt.Graphics g,
                 java.awt.print.PageFormat f,
                 int pageIndex)
Specified by:
print in interface java.awt.print.Printable

printImage

public void printImage(java.awt.Graphics2D g2d)
Description copied from interface: ImagePrint
Prints the image(s)drawn on the input graphics context.
Specified by:
printImage in interface ImagePrint
Following copied from interface: com.vistech.imageviewer.ImagePrint
Parameters:
g - the graphics context over which images are drawn.

paintComponent

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

redraw

protected void redraw()
creates a new off screen image and then redraws the current image over it.

reset

public void reset()
Paints the currently displayed image again with currently set display and flip modes

clearImageCanvas

public void clearImageCanvas()
Clears the image canvas. The currently set image object becomes null. To display a new image, set the image by calling setAWTImage() or setBufferedImage() with the new image.

addImageSentListener

public void addImageSentListener(ImageSentListener p)

removeImageSentListener

public void removeImageSentListener(ImageSentListener p)

fireImageSentEvents

protected void fireImageSentEvents(java.awt.image.BufferedImage img)

main

public static void main(java.lang.String[] args)