Package com.singularsys.jepexamples.applets

# Class AbstractCanvas

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel
com.singularsys.jepexamples.applets.AbstractCanvas

All Implemented Interfaces:
  `ComponentListener`, `KeyListener`, `MouseListener`, `MouseMotionListener`, `MouseWheelListener`, `ImageObserver`, `MenuContainer`, `Serializable`, `EventListener`, `Accessible`

Direct Known Subclasses:
  `FunctionCanvas`, `ParametrisedCanvas`

public abstract class AbstractCanvas extends JPanel implements MouseListener, ComponentListener, MouseWheelListener, MouseMotionListener, KeyListener
An abstract base class providing scaling and double buffering support for Canvas components. Scaling is such that the position of the origin on the screen does not change. Has methods for clearing background, painting axis, grid, scales and elapse time.

Author:
  Richard Morris
See Also:
  - Serialized Form

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from class javax.swing.JPanel

  `JPanel.AccessibleJPanel`

  ## Nested classes/interfaces inherited from class javax.swing.JComponent

  `JComponent.AccessibleJComponent`

  ## Nested classes/interfaces inherited from class java.awt.Container

  `Container.AccessibleAWTContainer`

  ## Nested classes/interfaces inherited from class java.awt.Component

  `Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final Color`
  `axisColor`
  Color for axis
  `protected final Color`
  `bgColor`
  Color for background
  `protected double`
  `cenX`
  `protected double`
  `cenY`
  `protected final int`
  `defaultOffX`
  `protected final int`
  `defaultOffY`
  `protected final double`
  `defaultScaleX`
  Default scale values, set in constructor used in reset
  `protected final double`
  `defaultScaleY`
  `protected Dimension`
  `dimensions`
  Dimensions of the canvas
  `protected final Color`
  `gridColor`
  Color for grid
  `protected final Color`
  `labelColor`
  Color for labels
  `protected final Font`
  `labelFont`
  `protected Point`
  `lastpos`
  `protected int`
  `offX`
  Offset of the graph in absolute coords
  `protected int`
  `offY`
  Offset of the graph in absolute coords
  `protected final JPopupMenu`
  `popup`
  A popup menu
  `protected final Font`
  `scaleFont`
  `protected double`
  `scaleX`
  Scaling of the graph in x and y directions
  `protected double`
  `scaleY`
  Scaling of the graph in x and y directions
  `protected boolean`
  `showGrid`
  Whether to show calculation times
  `protected boolean`
  `showScale`
  Whether to show scales
  `protected boolean`
  `showTime`
  Whether to show calculation times
  `protected BufferStrategy`
  `strategy`
  BufferStrategy to use

  ### Fields inherited from class javax.swing.JComponent

  `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

  `accessibleContext, 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

  Constructors
  Constructor
  Description
  `AbstractCanvas()`
  Constructor using 1 for scaling and 0 for offset.
  `AbstractCanvas(double sx, double sy, int offx, int offy)`
  Constructor specifying the default values for scaling and offset.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `center()`
  `protected int`
  `clipX(int xAbsolute)`
  Clip screen X values to lie within screen
  `protected int`
  `clipY(int yAbsolute)`
  Clip screen Y values to lie within screen
  `void`
  `componentHidden(ComponentEvent e)`
  `void`
  `componentMoved(ComponentEvent e)`
  `void`
  `componentResized(ComponentEvent e)`
  `void`
  `componentShown(ComponentEvent e)`
  `abstract void`
  `dump()`
  Dump textual representation to `System.out`
  `protected abstract long`
  `getNumPts()`
  The number of points which are calculated
  `int`
  `getOffX()`
  `int`
  `getOffY()`
  `JPopupMenu`
  `getPopup()`
  A pop-up menu which subclasses can add items to.
  `double`
  `getScaleX()`
  `double`
  `getScaleY()`
  `void`
  `keyPressed(KeyEvent e)`
  `void`
  `keyReleased(KeyEvent e)`
  `void`
  `keyTyped(KeyEvent e)`
  `void`
  `mouseClicked(MouseEvent e)`
  `void`
  `mouseDragged(MouseEvent e)`
  `void`
  `mouseEntered(MouseEvent e)`
  `void`
  `mouseExited(MouseEvent e)`
  `void`
  `mouseMoved(MouseEvent e)`
  `void`
  `mousePressed(MouseEvent e)`
  `void`
  `mouseReleased(MouseEvent e)`
  `void`
  `mouseWheelMoved(MouseWheelEvent e)`
  `void`
  `paint(Graphics g)`
  Provides double buffering support and calls paintCanvas.
  `protected void`
  `paintAxes(Graphics g)`
  Paints the axes for the graph.
  `protected void`
  `paintCanvas(Graphics g)`
  Paint the canvas.
  `protected abstract void`
  `paintCurve(Graphics g)`
  Paint the curve
  `protected void`
  `paintGrid(Graphics g)`
  `protected void`
  `paintScale(Graphics g)`
  `protected void`
  `paintTime(Graphics g, long t1, long t2)`
  Paints the elapsed time
  `protected void`
  `paintWhite(Graphics g)`
  Fills the background with white.
  `protected void`
  `rescaled()`
  Called when scale or offset changed Default action is to call repaint
  `void`
  `reset()`
  `void`
  `resetCanvas()`
  `protected void`
  `resized()`
  Called when canvas sized changes Default action is to call repaint
  `void`
  `setOffset(int offX, int offY)`
  `void`
  `setScale(double scX, double scY)`
  `void`
  `shiftCanvas(int xdiff, int ydiff)`
  `void`
  `update(Graphics g)`
  Just calls paint.
  `protected int`
  `xAbsolute(double xRelative)`
  Translates from scaled coordinates to screen coordinates.
  `protected double`
  `xRelative(int xAbsolute)`
  Translates from screen coordinates to scaled coordinates.
  `protected int`
  `yAbsolute(double yRelative)`
  Translates from scaled coordinates to screen coordinates.
  `protected double`
  `yRelative(int yAbsolute)`
  Translates from screen coordinates to scaled coordinates.
  `void`
  `zoomCanvas(double amount)`
  Zoom the canvas.

  ### Methods inherited from class javax.swing.JPanel

  `getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI`

  ### Methods inherited from class javax.swing.JComponent

  `addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, 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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, 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`

  ### 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, 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`

- ## Field Details

  - ### bgColor

    protected final Color bgColor
    Color for background

  - ### labelColor

    protected final Color labelColor
    Color for labels

  - ### axisColor

    protected final Color axisColor
    Color for axis

  - ### gridColor

    protected final Color gridColor
    Color for grid

  - ### scaleFont

    protected final Font scaleFont

  - ### labelFont

    protected final Font labelFont

  - ### cenX

    protected double cenX

  - ### cenY

    protected double cenY

  - ### scaleX

    protected double scaleX
    Scaling of the graph in x and y directions

  - ### scaleY

    protected double scaleY
    Scaling of the graph in x and y directions

  - ### offX

    protected int offX
    Offset of the graph in absolute coords

  - ### offY

    protected int offY
    Offset of the graph in absolute coords

  - ### defaultScaleX

    protected final double defaultScaleX
    Default scale values, set in constructor used in reset

  - ### defaultScaleY

    protected final double defaultScaleY

  - ### defaultOffX

    protected final int defaultOffX

  - ### defaultOffY

    protected final int defaultOffY

  - ### dimensions

    protected Dimension dimensions
    Dimensions of the canvas

  - ### strategy

    protected BufferStrategy strategy
    BufferStrategy to use

  - ### popup

    protected final JPopupMenu popup
    A popup menu

  - ### showScale

    protected boolean showScale
    Whether to show scales

  - ### showGrid

    protected boolean showGrid
    Whether to show calculation times

  - ### showTime

    protected boolean showTime
    Whether to show calculation times

  - ### lastpos

    protected Point lastpos

- ## Constructor Details

  - ### AbstractCanvas

    public AbstractCanvas()
    Constructor using 1 for scaling and 0 for offset.

  - ### AbstractCanvas

    public AbstractCanvas(double sx, double sy, int offx, int offy)
    Constructor specifying the default values for scaling and offset.

- ## Method Details

  - ### center

    protected void center()

  - ### dump

    public abstract void dump()
    Dump textual representation to `System.out`

  - ### rescaled

    protected void rescaled()
    Called when scale or offset changed Default action is to call repaint

  - ### resized

    protected void resized()
    Called when canvas sized changes Default action is to call repaint

  - ### clipX

    protected int clipX(int xAbsolute)
    Clip screen X values to lie within screen

  - ### clipY

    protected int clipY(int yAbsolute)
    Clip screen Y values to lie within screen

  - ### xAbsolute

    protected int xAbsolute(double xRelative)
    Translates from scaled coordinates to screen coordinates.

  - ### yAbsolute

    protected int yAbsolute(double yRelative)
    Translates from scaled coordinates to screen coordinates.

  - ### xRelative

    protected double xRelative(int xAbsolute)
    Translates from screen coordinates to scaled coordinates.

  - ### yRelative

    protected double yRelative(int yAbsolute)
    Translates from screen coordinates to scaled coordinates.

  - ### getScaleX

    public double getScaleX()

  - ### getScaleY

    public double getScaleY()

  - ### setScale

    public void setScale(double scX, double scY)

  - ### getOffX

    public int getOffX()

  - ### getOffY

    public int getOffY()

  - ### setOffset

    public void setOffset(int offX, int offY)

  - ### reset

    public void reset()

  - ### resetCanvas

    public void resetCanvas()

  - ### zoomCanvas

    public void zoomCanvas(double amount)
    Zoom the canvas. The x, y scales are multiplied by 1+amount/50.
    Parameters:
      `amount` - zoom factor

  - ### shiftCanvas

    public void shiftCanvas(int xdiff, int ydiff)

  - ### getPopup

    public JPopupMenu getPopup()
    A pop-up menu which subclasses can add items to.
    Returns:
      the menu

  - ### update

    public void update(Graphics g)
    Just calls paint. Eliminates background flicker problems with Canvas.update.
    Overrides:
      `update` in class `JComponent`

  - ### paint

    public void paint(Graphics g)
    Provides double buffering support and calls paintCanvas. Subclasses should not override this method unless they want a different buffering strategy. Instead, they should implement paintCanvas.
    Overrides:
      `paint` in class `JComponent`
    See Also:
      - `paintCanvas(Graphics)`

  - ### paintCanvas

    protected void paintCanvas(Graphics g)
    Paint the canvas. Calls paintWhite(g); paintAxes(g); paintScale(g); paintGrid(g); paintCurve(g); and paintTime(g,t1,t2);
    Parameters:
      `g` - the graphics object

  - ### paintCurve

    protected abstract void paintCurve(Graphics g)
    Paint the curve

  - ### paintTime

    protected void paintTime(Graphics g, long t1, long t2)
    Paints the elapsed time
    Parameters:
      `g` -
      `t1` - start time in nano-secs.
      `t2` - end time in nano-secs.

  - ### getNumPts

    protected abstract long getNumPts()
    The number of points which are calculated
    Returns:
      the number of points

  - ### paintWhite

    protected void paintWhite(Graphics g)
    Fills the background with white.

  - ### paintAxes

    protected void paintAxes(Graphics g)
    Paints the axes for the graph.

  - ### paintScale

    protected void paintScale(Graphics g)

  - ### paintGrid

    protected void paintGrid(Graphics g)

  - ### componentHidden

    public void componentHidden(ComponentEvent e)
    Specified by:
      `componentHidden` in interface `ComponentListener`

  - ### componentMoved

    public void componentMoved(ComponentEvent e)
    Specified by:
      `componentMoved` in interface `ComponentListener`

  - ### componentShown

    public void componentShown(ComponentEvent e)
    Specified by:
      `componentShown` in interface `ComponentListener`

  - ### componentResized

    public void componentResized(ComponentEvent e)
    Specified by:
      `componentResized` in interface `ComponentListener`

  - ### mouseWheelMoved

    public void mouseWheelMoved(MouseWheelEvent e)
    Specified by:
      `mouseWheelMoved` in interface `MouseWheelListener`

  - ### mousePressed

    public void mousePressed(MouseEvent e)
    Specified by:
      `mousePressed` in interface `MouseListener`

  - ### mouseReleased

    public void mouseReleased(MouseEvent e)
    Specified by:
      `mouseReleased` in interface `MouseListener`

  - ### mouseDragged

    public void mouseDragged(MouseEvent e)
    Specified by:
      `mouseDragged` in interface `MouseMotionListener`

  - ### mouseMoved

    public void mouseMoved(MouseEvent e)
    Specified by:
      `mouseMoved` in interface `MouseMotionListener`

  - ### mouseClicked

    public void mouseClicked(MouseEvent e)
    Specified by:
      `mouseClicked` in interface `MouseListener`

  - ### mouseEntered

    public void mouseEntered(MouseEvent e)
    Specified by:
      `mouseEntered` in interface `MouseListener`

  - ### mouseExited

    public void mouseExited(MouseEvent e)
    Specified by:
      `mouseExited` in interface `MouseListener`

  - ### keyPressed

    public void keyPressed(KeyEvent e)
    Specified by:
      `keyPressed` in interface `KeyListener`

  - ### keyReleased

    public void keyReleased(KeyEvent e)
    Specified by:
      `keyReleased` in interface `KeyListener`

  - ### keyTyped

    public void keyTyped(KeyEvent e)
    Specified by:
      `keyTyped` in interface `KeyListener`