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:
ComplexCanvas,FunctionCanvas,ImplicitCanvas,ParametrisedCanvas,PixelCanvas
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JPanel
JPanel.AccessibleJPanelNested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ColorColor for axisprotected final ColorColor for backgroundprotected doubleprotected doubleprotected final intprotected final intprotected final doubleDefault scale values, set in constructor used in resetprotected final doubleprotected DimensionDimensions of the canvasprotected final ColorColor for gridprotected final ColorColor for labelsprotected final Fontprotected Pointprotected intOffset of the graph in absolute coordsprotected intOffset of the graph in absolute coordsprotected final JPopupMenuA popup menuprotected final Fontprotected doubleScaling of the graph in x and y directionsprotected doubleScaling of the graph in x and y directionsprotected booleanWhether to show calculation timesprotected booleanWhether to show scalesprotected booleanWhether to show calculation timesprotected BufferStrategyBufferStrategy to useFields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
Constructor Summary
ConstructorsConstructorDescriptionConstructor 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 TypeMethodDescriptionprotected voidcenter()protected intclipX(int xAbsolute) Clip screen X values to lie within screenprotected intclipY(int yAbsolute) Clip screen Y values to lie within screenvoidvoidvoidvoidabstract voiddump()Dump textual representation toSystem.outprotected abstract longThe number of points which are calculatedintgetOffX()intgetOffY()getPopup()A pop-up menu which subclasses can add items to.doubledoublevoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidvoidProvides double buffering support and calls paintCanvas.protected voidPaints the axes for the graph.protected voidPaint the canvas.protected abstract voidPaint the curveprotected voidprotected voidprotected voidPaints the elapsed timeprotected voidFills the background with white.protected voidrescaled()Called when scale or offset changed Default action is to call repaintvoidreset()voidprotected voidresized()Called when canvas sized changes Default action is to call repaintvoidsetOffset(int offX, int offY) voidsetScale(double scX, double scY) voidshiftCanvas(int xdiff, int ydiff) voidJust calls paint.protected intxAbsolute(double xRelative) Translates from scaled coordinates to screen coordinates.protected doublexRelative(int xAbsolute) Translates from screen coordinates to scaled coordinates.protected intyAbsolute(double yRelative) Translates from scaled coordinates to screen coordinates.protected doubleyRelative(int yAbsolute) Translates from screen coordinates to scaled coordinates.voidzoomCanvas(double amount) Zoom the canvas.Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUIMethods 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, unregisterKeyboardActionMethods 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, validateTreeMethods 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
-
Field Details
-
bgColor
Color for background -
labelColor
Color for labels -
axisColor
Color for axis -
gridColor
Color for grid -
scaleFont
-
labelFont
-
cenX
protected double cenX -
cenY
protected double cenY -
scaleX
protected double scaleXScaling of the graph in x and y directions -
scaleY
protected double scaleYScaling of the graph in x and y directions -
offX
protected int offXOffset of the graph in absolute coords -
offY
protected int offYOffset of the graph in absolute coords -
defaultScaleX
protected final double defaultScaleXDefault scale values, set in constructor used in reset -
defaultScaleY
protected final double defaultScaleY -
defaultOffX
protected final int defaultOffX -
defaultOffY
protected final int defaultOffY -
dimensions
Dimensions of the canvas -
strategy
BufferStrategy to use -
popup
A popup menu -
showScale
protected boolean showScaleWhether to show scales -
showGrid
protected boolean showGridWhether to show calculation times -
showTime
protected boolean showTimeWhether to show calculation times -
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 toSystem.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
A pop-up menu which subclasses can add items to.- Returns:
- the menu
-
update
Just calls paint. Eliminates background flicker problems with Canvas.update.- Overrides:
updatein classJComponent
-
paint
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:
paintin classJComponent- See Also:
-
paintCanvas
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
Paint the curve -
paintTime
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
Fills the background with white. -
paintAxes
Paints the axes for the graph. -
paintScale
-
paintGrid
-
componentHidden
- Specified by:
componentHiddenin interfaceComponentListener
-
componentMoved
- Specified by:
componentMovedin interfaceComponentListener
-
componentShown
- Specified by:
componentShownin interfaceComponentListener
-
componentResized
- Specified by:
componentResizedin interfaceComponentListener
-
mouseWheelMoved
- Specified by:
mouseWheelMovedin interfaceMouseWheelListener
-
mousePressed
- Specified by:
mousePressedin interfaceMouseListener
-
mouseReleased
- Specified by:
mouseReleasedin interfaceMouseListener
-
mouseDragged
- Specified by:
mouseDraggedin interfaceMouseMotionListener
-
mouseMoved
- Specified by:
mouseMovedin interfaceMouseMotionListener
-
mouseClicked
- Specified by:
mouseClickedin interfaceMouseListener
-
mouseEntered
- Specified by:
mouseEnteredin interfaceMouseListener
-
mouseExited
- Specified by:
mouseExitedin interfaceMouseListener
-
keyPressed
- Specified by:
keyPressedin interfaceKeyListener
-
keyReleased
- Specified by:
keyReleasedin interfaceKeyListener
-
keyTyped
- Specified by:
keyTypedin interfaceKeyListener
-