net.sf.jaxodraw.gui
Class JaxoCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by net.sf.jaxodraw.gui.JaxoCanvas
All Implemented Interfaces:
java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.swing.Scrollable

public final class JaxoCanvas
extends javax.swing.JComponent
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, javax.swing.Scrollable

An instance of the canvas: responsible for all the painting.

Since:
2.0
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
 
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
JaxoCanvas(JaxoMainPanel mainPanel)
          Constructs a new canvas within the specified panel.
 
Method Summary
 void applyPrefs()
          Applies the preferences.
 void clear()
          Clears the canvas.
 void copyMarkedObjects()
          Puts the current clipboard to the system clipboard.
 void deleteMarkedObjects()
          Deletes all 'marked' objects from the current canvas graph.
 java.awt.Color getCanvasBackground()
          Background color in the region that is actually covered by the canvas getCanvasSize(), which is the whole canvas, unless the effective maximum size is smaller than the component size.
 java.awt.Rectangle getCanvasBounds()
          Part of the component that is covered by the actual canvas.
 JaxoGraph getCanvasGraph()
          Returns the current graph.
 java.awt.Point getCanvasOrigin()
          Origin of the actual canvas within the component.
 java.awt.Dimension getCanvasSize()
          Canvas size.
 JaxoGraph getClipboard()
          Gets the current clipboard.
 JaxoDefaultGrid getGrid()
          Grid to be used, may be null.
 JaxoHandle getHandle()
          The current handle.
 java.awt.Dimension getMaximumCanvasSize()
          Maximum size of the canvas (even if there are objects with a larger bounding box).
 java.awt.Dimension getMinimumCanvasSize()
          Minimum size of the canvas (even if there are no objects or they have a smaller bounding box).
 java.awt.Dimension getPreferredScrollableViewportSize()
          Return the preferred size of the viewport.
 int getScrollableBlockIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Return the unit increment for block scrolling.
 boolean getScrollableTracksViewportHeight()
          Return true if a viewport should force the height of the scrollable.
 boolean getScrollableTracksViewportWidth()
          Return true if a viewport should force the width of the scrollable.
 int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Return the unit increment for scrolling.
 JaxoZoom getZoom()
          Return the zoom of this canvas.
 void groupMarkedObjects()
          Groups the currently marked objects.
 boolean isAntialiasEnabled()
          Should objects (but not handles) be painted with antialiasing turned explicitly on?
 boolean isGridPainted()
          Whether the grid is painted.
 boolean isSnappingToGrid()
          Determines if points are snapped to the grid.
 void markImageInvalid()
          Mark the whole image as invalid.
 void markImageInvalid(java.awt.Rectangle boundingBox)
          Mark the specified region as invalid.
 void mouseClicked(java.awt.event.MouseEvent e)
          The action to be taken when the mouse is clicked on the canvas.
 void mouseDragged(java.awt.event.MouseEvent e)
          The action to be taken when the mouse is dragged on the canvas.
 void mouseEntered(java.awt.event.MouseEvent e)
          Process the event when the mouse enters the canvas.
 void mouseExited(java.awt.event.MouseEvent e)
          Process the event when the mouse exits the canvas.
 void mouseMoved(java.awt.event.MouseEvent e)
          Process the event when the mouse is moved on the canvas.
 void mousePressed(java.awt.event.MouseEvent e)
          The action to be taken when the mouse is pressed on the canvas.
 void mouseReleased(java.awt.event.MouseEvent e)
          The action to be taken when the mouse is released on the canvas.
 void moveSelection(boolean backGround)
          Moves the currently selected objects into either fore- or background.
protected  void paintComponent(java.awt.Graphics g)
          Paint the Canvas.
 void pasteFromClipboard()
          Paste the objects currently on the clipboard.
protected  void printComponent(java.awt.Graphics gr)
          Print the Canvas.
 void refresh()
          Refresh the canvas.
 void setAntialiasEnabled(boolean value)
          Sets the antialias property.
 void setCanvasBackground(java.awt.Color value)
          Sets the color of the background.
 void setCanvasGraph(JaxoGraph value)
          Sets the current graph.
 void setCanvasGraphAndGrid(JaxoGraph newGraph, JaxoDefaultGrid newGrid)
          Set both graph and grid.
 void setGrid(JaxoDefaultGrid value)
          Sets the grid to be used by this canvas.
 void setGridPainted(boolean value)
          Switches the grid painting on/off.
 void setHandle(JaxoHandle newHandle)
          Sets the handle.
 void setMaximumCanvasSize(java.awt.Dimension value)
          Sets the maximum canvas size.
 void setMinimumCanvasSize(java.awt.Dimension value)
          Sets the minimum canvas size.
 void setMouseLocation(java.awt.Point value)
          Sets the current mouse location in graph coordinates.
 void setSnappingToGrid(boolean value)
          Switches snapping on/off.
 void setZoom(JaxoZoom newZoom)
          Set the zoom for this canvas.
 void ungroupMarkedObjects()
          Ungroup (one level) all marked objects.
 void ungroupSelectedObject()
          If the selected object is a group, ungroup it.
 void updateLanguage()
          Updates the language on any localized sub-components.
 void updateMode(int mode)
          Resets canvas parameters for the given mode.
 
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, getPreferredSize, 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, 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, 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, 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, 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, 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, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JaxoCanvas

public JaxoCanvas(JaxoMainPanel mainPanel)
Constructs a new canvas within the specified panel.

Parameters:
mainPanel - An instance of the main panel.
Method Detail

getCanvasOrigin

public java.awt.Point getCanvasOrigin()
Origin of the actual canvas within the component.

Returns:
Point

getCanvasBounds

public java.awt.Rectangle getCanvasBounds()
Part of the component that is covered by the actual canvas. This includes canvas background and grid, -- i.e. a rectangle with size 'canvasSize' and origin 'canvasOrigin'.

Returns:
Rectangle
See Also:
getCanvasSize(), getCanvasOrigin()

markImageInvalid

public void markImageInvalid()
Mark the whole image as invalid. Currently this repaints everything at once, could be changed to defer until later. This is public because now the main panel does some things for the canvas.


markImageInvalid

public void markImageInvalid(java.awt.Rectangle boundingBox)
Mark the specified region as invalid. Currently this is also synchronous.

Parameters:
boundingBox - The region to update.

refresh

public void refresh()
Refresh the canvas.


getCanvasBackground

public java.awt.Color getCanvasBackground()
Background color in the region that is actually covered by the canvas getCanvasSize(), which is the whole canvas, unless the effective maximum size is smaller than the component size. Note that this color may be overwritten by the grid.

Returns:
The canvas background color.

setCanvasBackground

public void setCanvasBackground(java.awt.Color value)
Sets the color of the background.

Parameters:
value - The background color.

isSnappingToGrid

public boolean isSnappingToGrid()
Determines if points are snapped to the grid. This property is independent from whether the grid is painted ('isGridPainted').

Returns:
True if snapping is currently enabled.

setSnappingToGrid

public void setSnappingToGrid(boolean value)
Switches snapping on/off.

Parameters:
value - Whether to snap or not.

isAntialiasEnabled

public boolean isAntialiasEnabled()
Should objects (but not handles) be painted with antialiasing turned explicitly on?

Returns:
True if antialising is enebled.

setAntialiasEnabled

public void setAntialiasEnabled(boolean value)
Sets the antialias property.

Parameters:
value - True if antialising should be enabled.

isGridPainted

public boolean isGridPainted()
Whether the grid is painted.

Returns:
True if the grid is visible.

setGridPainted

public void setGridPainted(boolean value)
Switches the grid painting on/off.

Parameters:
value - True if the grid should be painted.

getGrid

public JaxoDefaultGrid getGrid()
Grid to be used, may be null. A non-null grid is painted based on 'isGridPainted' and potentially used for snapping points. If grid properties change, the canvas automatically updates and repaints itself.

Returns:
The grid used by this canvas.

setGrid

public void setGrid(JaxoDefaultGrid value)
Sets the grid to be used by this canvas.

Parameters:
value - The new grid.

updateLanguage

public void updateLanguage()
Updates the language on any localized sub-components.


setCanvasGraphAndGrid

public void setCanvasGraphAndGrid(JaxoGraph newGraph,
                                  JaxoDefaultGrid newGrid)
Set both graph and grid. This is an optimization to avoid painting to the background image twice (because that is done synchronously).

Parameters:
newGraph - The new graph.
newGrid - The new grid.

setCanvasGraph

public void setCanvasGraph(JaxoGraph value)
Sets the current graph. Also clears the hovering object.

Parameters:
value - The graph to be set.

getCanvasGraph

public JaxoGraph getCanvasGraph()
Returns the current graph.

Returns:
The current graph.

paintComponent

protected void paintComponent(java.awt.Graphics g)
Paint the Canvas.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - The graphics context to paint to.

printComponent

protected void printComponent(java.awt.Graphics gr)
Print the Canvas.

Overrides:
printComponent in class javax.swing.JComponent
Parameters:
gr - The graphics context to print to.

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
The action to be taken when the mouse is clicked on the canvas.

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
e - The corresponding mouse event.

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
The action to be taken when the mouse is pressed on the canvas.

Specified by:
mousePressed in interface java.awt.event.MouseListener
Parameters:
e - The corresponding mouse event.

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
The action to be taken when the mouse is dragged on the canvas.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Parameters:
e - The corresponding mouse event.

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
The action to be taken when the mouse is released on the canvas.

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Parameters:
e - The corresponding mouse event.

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Process the event when the mouse exits the canvas. Does nothing.

Specified by:
mouseExited in interface java.awt.event.MouseListener
Parameters:
e - The mouse event to process.

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Process the event when the mouse enters the canvas. Does nothing.

Specified by:
mouseEntered in interface java.awt.event.MouseListener
Parameters:
e - The mouse event to process.

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Process the event when the mouse is moved on the canvas.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener
Parameters:
e - The mouse event to process.

setMouseLocation

public void setMouseLocation(java.awt.Point value)
Sets the current mouse location in graph coordinates. Notifies any registered PropertyChangeListeners.

Parameters:
value - The new mouse coordinates.

getCanvasSize

public java.awt.Dimension getCanvasSize()
Canvas size. This is equal to the (inner) component size unless because of viewport size tracking, the component has become larger than the effective maximum canvas size.

Returns:
The canvas size.

getMinimumCanvasSize

public java.awt.Dimension getMinimumCanvasSize()
Minimum size of the canvas (even if there are no objects or they have a smaller bounding box). The default value is (0,0).

Returns:
The minimum canvas size.

setMinimumCanvasSize

public void setMinimumCanvasSize(java.awt.Dimension value)
Sets the minimum canvas size.

Parameters:
value - The minimum canvas size.

getMaximumCanvasSize

public java.awt.Dimension getMaximumCanvasSize()
Maximum size of the canvas (even if there are objects with a larger bounding box). The default value is larger than (0,0). If the maximum canvas size is smaller (in either direction) than the minimum canvas size, the latter wins.

Returns:
The maximum canvas size.

setMaximumCanvasSize

public void setMaximumCanvasSize(java.awt.Dimension value)
Sets the maximum canvas size.

Parameters:
value - The maximum canvas size.

getHandle

public JaxoHandle getHandle()
The current handle.

Returns:
The current handle.

setHandle

public void setHandle(JaxoHandle newHandle)
Sets the handle.

Parameters:
newHandle - The handle to set.

applyPrefs

public void applyPrefs()
Applies the preferences.


copyMarkedObjects

public void copyMarkedObjects()
Puts the current clipboard to the system clipboard.


getClipboard

public JaxoGraph getClipboard()
Gets the current clipboard.

Returns:
A JaxoGraph holding all the objects that are currently on the clipboard, or an empty graph, if the clipboard is empty.

pasteFromClipboard

public void pasteFromClipboard()
Paste the objects currently on the clipboard.


deleteMarkedObjects

public void deleteMarkedObjects()
Deletes all 'marked' objects from the current canvas graph.


ungroupMarkedObjects

public void ungroupMarkedObjects()
Ungroup (one level) all marked objects.


ungroupSelectedObject

public void ungroupSelectedObject()
If the selected object is a group, ungroup it.


groupMarkedObjects

public void groupMarkedObjects()
Groups the currently marked objects.


updateMode

public void updateMode(int mode)
Resets canvas parameters for the given mode. Sets the cursor, and determines whether to draw visualAid and handles.

Parameters:
mode - The mode to adjust to.

clear

public void clear()
Clears the canvas.


moveSelection

public void moveSelection(boolean backGround)
Moves the currently selected objects into either fore- or background.

Parameters:
backGround - True for moving into background, false for foreground.

getPreferredScrollableViewportSize

public java.awt.Dimension getPreferredScrollableViewportSize()
Return the preferred size of the viewport.

Specified by:
getPreferredScrollableViewportSize in interface javax.swing.Scrollable
Returns:
The preferred size of the viewport.

getScrollableUnitIncrement

public int getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Return the unit increment for scrolling.

Specified by:
getScrollableUnitIncrement in interface javax.swing.Scrollable
Parameters:
visibleRect - the visible view area.
orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
direction - less than zero to scroll up/left, greater than zero for down/right.
Returns:
Returns 6.

getScrollableBlockIncrement

public int getScrollableBlockIncrement(java.awt.Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Return the unit increment for block scrolling.

Specified by:
getScrollableBlockIncrement in interface javax.swing.Scrollable
Parameters:
visibleRect - the visible view area.
orientation - Either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
direction - less than zero to scroll up/left, greater than zero for down/right.
Returns:
Returns the width/height of visibleRect depending on orientation.

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Return true if a viewport should force the width of the scrollable.

Specified by:
getScrollableTracksViewportWidth in interface javax.swing.Scrollable
Returns:
true if the parent's width is larger than the width of the preferred size.

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Return true if a viewport should force the height of the scrollable.

Specified by:
getScrollableTracksViewportHeight in interface javax.swing.Scrollable
Returns:
true if the parent's height is larger than the height of the preferred size.

getZoom

public JaxoZoom getZoom()
Return the zoom of this canvas.

Returns:
the zoom of this canvas.

setZoom

public void setZoom(JaxoZoom newZoom)
Set the zoom for this canvas.

Parameters:
newZoom - the zoom to set.


Licensed under GPL. For more information, see http://jaxodraw.sourceforge.net/license.html or the LICENSE file in the jaxodraw distribution.