diva.canvas
Class JCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by diva.canvas.JCanvas
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.awt.print.Printable, java.io.Serializable
Direct Known Subclasses:
JCanvasPalette, JGraph

public class JCanvas
extends javax.swing.JComponent
implements java.awt.print.Printable

The JCanvas class is the center-piece of this package. The canvas is composed of a canvas pane, which in turn is composed of one or more layers. Each layer may contain arbitrary graphics, although commonly at least one layer is an instance of a "figure layer" class that contains figure objects. The main role of the JCanvas class is to provide the physical screen space on which layers draw themselves, and to interface to the Swing component hierarchy.

This architecture allows a graphics developer to write code for a pane and a set of layers, without being concerned about whether the pane and its layers will be directly contained by a JCanvas or within some other layer. For example, it will be possible for a visualization component to be "embedded" in a larger component.

Version:
$Id: JCanvas.java 38798 2005-07-08 20:00:01Z cxh $
Author:
John Reekie
See Also:
Serialized Form
Accepted Rating:
Yellow

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.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  CanvasPane _canvasPane
          The contained canvas pane.
private  javax.swing.DefaultBoundedRangeModel _horizontalRangeModel
          range model to control the horizontal movement of the model
private  java.awt.image.BufferedImage _offscreen
          The off-screen image.
private  javax.swing.DefaultBoundedRangeModel _verticalRangeModel
          range model to control the vertical movement of the model
private  boolean _workaroundClearRectBug
          A flag to tell us whether to work around the clearRect bug in beta4
 
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.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
JCanvas()
          Create a new canvas that contains a single GraphicsPane.
JCanvas(CanvasPane pane)
          Create a new canvas that contains the given CanvasPane.
 
Method Summary
private  boolean _checkForClearRectBug()
          Check for the clearRect bug by looking at the JDK version
 CanvasPane getCanvasPane()
          Get the canvas pane contained by this component.
 javax.swing.BoundedRangeModel getHorizontalRangeModel()
          return the horizontal range model for this canvas
 java.lang.String getToolTipText(java.awt.event.MouseEvent e)
          Get the toolTipText for the point in the given MouseEvent.
 javax.swing.BoundedRangeModel getVerticalRangeModel()
          return the vertical range model for this canvas
 java.awt.geom.Rectangle2D getViewSize()
          Return the total size of everything in the canvas, in canvas coordinates.
 java.awt.geom.Rectangle2D getVisibleSize()
          Return the size of the visible part of the canvas, in canvas coordinates.
private  void internalProcessMouseEvent(java.awt.event.MouseEvent e)
          Process a mouse event.
 boolean isFocusTraversable()
          Return whether or not focus should be traversable across this object.
 void paint(java.awt.Graphics g)
          Paint the canvas.
 int print(java.awt.Graphics graphics, java.awt.print.PageFormat format, int index)
          Print the canvas to a printer, represented by the specified graphics object.
 int print(java.awt.Graphics graphics, java.awt.print.PageFormat format, int index, java.awt.geom.Rectangle2D printRegion)
          Print the canvas to a printer, represented by the specified graphics object.
protected  void processMouseEvent(java.awt.event.MouseEvent e)
          Process a mouse event.
protected  void processMouseMotionEvent(java.awt.event.MouseEvent e)
          Process a mouse motion event.
 void repaint(DamageRegion d)
          Accept notification that a repaint has occurred on in this canvas.
 void setCanvasPane(CanvasPane pane)
          Set the canvas pane contained by this JCanvas.
 void setDoubleBuffered(boolean flag)
          Turn double-buffering on this canvas on or off.
 void setPreferredSize(java.awt.Dimension d)
          Set the preferred size of this JCanvas.
 
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, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, 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, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, 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, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, 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, 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

_offscreen

private java.awt.image.BufferedImage _offscreen
The off-screen image.


_canvasPane

private CanvasPane _canvasPane
The contained canvas pane.


_horizontalRangeModel

private javax.swing.DefaultBoundedRangeModel _horizontalRangeModel
range model to control the horizontal movement of the model


_verticalRangeModel

private javax.swing.DefaultBoundedRangeModel _verticalRangeModel
range model to control the vertical movement of the model


_workaroundClearRectBug

private transient boolean _workaroundClearRectBug
A flag to tell us whether to work around the clearRect bug in beta4

Constructor Detail

JCanvas

public JCanvas()
Create a new canvas that contains a single GraphicsPane. This is the simplest way of using the JCanvas. Mouse events on the canvas are enabled by default.


JCanvas

public JCanvas(CanvasPane pane)
Create a new canvas that contains the given CanvasPane. Mouse events on the canvas are enabled by default.

Method Detail

getCanvasPane

public final CanvasPane getCanvasPane()
Get the canvas pane contained by this component.


getToolTipText

public java.lang.String getToolTipText(java.awt.event.MouseEvent e)
Get the toolTipText for the point in the given MouseEvent. Ask the canvasPane for a toolTip for the location of the mouse event. Note that you should not in general call setToolTipText on this canvas, since the value is ignored and the value returned by this method used instead. Even worse, if you call setToolTipText(null), then tooltips will probably get disabled entirely!

Overrides:
getToolTipText in class javax.swing.JComponent

isFocusTraversable

public boolean isFocusTraversable()
Return whether or not focus should be traversable across this object. This must return true to allow keyboard events to be grabbed. Return true in this class.

Overrides:
isFocusTraversable in class java.awt.Component

paint

public void paint(java.awt.Graphics g)
Paint the canvas. Every layer in this canvas will be requested to paint itself.

This method allocates an offscreen buffer if necessary, and then paints the canvas into the right buffer and blits it to the on-screen buffer.

Acknowledgement: some of this code was adapted from code posted by Jonathon Knudsen to the Java2D mailing list, May 1998.

Overrides:
paint in class javax.swing.JComponent

print

public int print(java.awt.Graphics graphics,
                 java.awt.print.PageFormat format,
                 int index)
          throws java.awt.print.PrinterException
Print the canvas to a printer, represented by the specified graphics object. Scale the size of the canvas to fit onto the printed page, while preserving the shape of the objects on the page.

Specified by:
print in interface java.awt.print.Printable
Parameters:
graphics - The context into which the page is drawn.
format - The size and orientation of the page being drawn.
index - The zero based index of the page to be drawn.
Returns:
PAGE_EXISTS if the page is rendered successfully, or NO_SUCH_PAGE if pageIndex specifies a non-existent page.
Throws:
java.awt.print.PrinterException - If the print job is terminated.

print

public int print(java.awt.Graphics graphics,
                 java.awt.print.PageFormat format,
                 int index,
                 java.awt.geom.Rectangle2D printRegion)
          throws java.awt.print.PrinterException
Print the canvas to a printer, represented by the specified graphics object. Scale the given printRegion to fit onto the printed page, while preserving the shape of the objects on the page.

Parameters:
graphics - The context into which the page is drawn.
format - The size and orientation of the page being drawn.
index - The zero based index of the page to be drawn.
printRegion - The rectangular region of the canvas, in screen coordinates, that will be printed to the screen.
Returns:
PAGE_EXISTS if the page is rendered successfully, or NO_SUCH_PAGE if pageIndex specifies a non-existent page.
Throws:
java.awt.print.PrinterException - If the print job is terminated.

repaint

public void repaint(DamageRegion d)
Accept notification that a repaint has occurred on in this canvas. Call the given damage region to generate the appropriate calls to the Swing repaint manager.


setCanvasPane

public final void setCanvasPane(CanvasPane pane)
Set the canvas pane contained by this JCanvas. If there is already a pane in this JCanvas, replace it. If the pane already is in a canvas, remove it from that other canvas.


setDoubleBuffered

public void setDoubleBuffered(boolean flag)
Turn double-buffering on this canvas on or off. This method overrides the inherited method to delete the off-screen buffer.

Overrides:
setDoubleBuffered in class javax.swing.JComponent

setPreferredSize

public void setPreferredSize(java.awt.Dimension d)
Set the preferred size of this JCanvas. In addition to calling the superclass method, this calls setSize() on the contained pane.

Overrides:
setPreferredSize in class javax.swing.JComponent

getHorizontalRangeModel

public javax.swing.BoundedRangeModel getHorizontalRangeModel()
return the horizontal range model for this canvas


getVerticalRangeModel

public javax.swing.BoundedRangeModel getVerticalRangeModel()
return the vertical range model for this canvas


getViewSize

public java.awt.geom.Rectangle2D getViewSize()
Return the total size of everything in the canvas, in canvas coordinates.


getVisibleSize

public java.awt.geom.Rectangle2D getVisibleSize()
Return the size of the visible part of the canvas, in canvas coordinates.


processMouseEvent

protected void processMouseEvent(java.awt.event.MouseEvent e)
Process a mouse event. This method overrides the inherited method to create a LayerEvent or LayerMotionEvent and pass the event on to its pane (if it is not null). The mouse event is passed to the superclass' method for handling.

Overrides:
processMouseEvent in class javax.swing.JComponent

processMouseMotionEvent

protected void processMouseMotionEvent(java.awt.event.MouseEvent e)
Process a mouse motion event. This method overrides the inherited method to create a LayerEvent or LayerMotionEvent and pass the event on to its pane (if it is not null). The mouse event is passed to the superclass' method for handling.

Overrides:
processMouseMotionEvent in class javax.swing.JComponent

internalProcessMouseEvent

private void internalProcessMouseEvent(java.awt.event.MouseEvent e)
Process a mouse event. This internal method is called by both processMouseEvent() and processMouseMotionEvent().


_checkForClearRectBug

private boolean _checkForClearRectBug()
Check for the clearRect bug by looking at the JDK version