com.singularsys.jepexamples.applets
Class AbstractCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by com.singularsys.jepexamples.applets.AbstractCanvas
All Implemented Interfaces:
java.awt.event.ComponentListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible
Direct Known Subclasses:
FunctionCanvas, ParametrisedCanvas

public abstract class AbstractCanvas
extends java.awt.Canvas
implements java.awt.event.MouseListener, java.awt.event.ComponentListener, java.awt.event.MouseWheelListener, java.awt.event.MouseMotionListener, java.awt.event.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 java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
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
protected  java.awt.Color axisColor
          Color for axis
protected  java.awt.Color bgColor
          Color for background
protected  double cenX
           
protected  double cenY
           
protected  int defaultOffX
           
protected  int defaultOffY
           
protected  double defaultScaleX
          Default scale values, set in constructor used in reset
protected  double defaultScaleY
          Default scale values, set in constructor used in reset
protected  java.awt.Dimension dimensions
          Dimensions of the canvas
protected  java.awt.Color gridColor
          Color for grid
protected  java.awt.Color labelColor
          Color for labels
protected  java.awt.Font labelFont
           
protected  java.awt.Point lastpos
           
protected  int offX
          Offset of the graph in absolute coords
protected  int offY
          Offset of the graph in absolute coords
protected  java.awt.PopupMenu popup
          A popup menu
protected  java.awt.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  java.awt.image.BufferStrategy strategy
          BufferStratergy to use
 
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
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
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(java.awt.event.ComponentEvent e)
           
 void componentMoved(java.awt.event.ComponentEvent e)
           
 void componentResized(java.awt.event.ComponentEvent e)
           
 void componentShown(java.awt.event.ComponentEvent e)
           
abstract  void dump()
          Dump textual representation to System.out
 int getOffX()
           
 int getOffY()
           
 java.awt.PopupMenu getPopup()
          A pop-up menu which subclasses can add items to.
 double getScaleX()
           
 double getScaleY()
           
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
           
 void keyTyped(java.awt.event.KeyEvent e)
           
 void mouseClicked(java.awt.event.MouseEvent e)
           
 void mouseDragged(java.awt.event.MouseEvent e)
           
 void mouseEntered(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
           
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
           
 void mouseReleased(java.awt.event.MouseEvent e)
           
 void mouseWheelMoved(java.awt.event.MouseWheelEvent e)
           
 void paint(java.awt.Graphics g)
          Provides double buffering support and calls paintCanvas.
protected  void paintAxes(java.awt.Graphics g)
          Paints the axes for the graph.
protected  void paintCanvas(java.awt.Graphics g)
          Paint the canvas.
protected abstract  void paintCurve(java.awt.Graphics g)
          Paint the curve
protected  void paintGrid(java.awt.Graphics g)
           
protected  void paintScale(java.awt.Graphics g)
           
protected  void paintTime(java.awt.Graphics g, long t1, long t2)
          Paints the elapse time
protected  void paintWhite(java.awt.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(java.awt.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 java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

bgColor

protected java.awt.Color bgColor
Color for background


labelColor

protected java.awt.Color labelColor
Color for labels


axisColor

protected java.awt.Color axisColor
Color for axis


gridColor

protected java.awt.Color gridColor
Color for grid


scaleFont

protected java.awt.Font scaleFont

labelFont

protected java.awt.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 double defaultScaleX
Default scale values, set in constructor used in reset


defaultScaleY

protected double defaultScaleY
Default scale values, set in constructor used in reset


defaultOffX

protected int defaultOffX

defaultOffY

protected int defaultOffY

dimensions

protected java.awt.Dimension dimensions
Dimensions of the canvas


strategy

protected java.awt.image.BufferStrategy strategy
BufferStratergy to use


popup

protected java.awt.PopupMenu 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 java.awt.Point lastpos
Constructor Detail

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.

Parameters:
sx -
sy -
offx -
offy -
Method Detail

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/10.

Parameters:
amount -

shiftCanvas

public void shiftCanvas(int xdiff,
                        int ydiff)

getPopup

public java.awt.PopupMenu getPopup()
A pop-up menu which subclasses can add items to.

Returns:
the menu

update

public void update(java.awt.Graphics g)
Just calls paint. Eliminates background flicker problems with Canvas.update.

Overrides:
update in class java.awt.Canvas

paint

public void paint(java.awt.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 java.awt.Canvas
See Also:
paintCanvas(Graphics)

paintCanvas

protected void paintCanvas(java.awt.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(java.awt.Graphics g)
Paint the curve

Parameters:
g -

paintTime

protected void paintTime(java.awt.Graphics g,
                         long t1,
                         long t2)
Paints the elapse time

Parameters:
g -
t1 - start time in nano-secs.
t2 - end time in nano-secs.

paintWhite

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


paintAxes

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


paintScale

protected void paintScale(java.awt.Graphics g)

paintGrid

protected void paintGrid(java.awt.Graphics g)

componentHidden

public void componentHidden(java.awt.event.ComponentEvent e)
Specified by:
componentHidden in interface java.awt.event.ComponentListener

componentMoved

public void componentMoved(java.awt.event.ComponentEvent e)
Specified by:
componentMoved in interface java.awt.event.ComponentListener

componentShown

public void componentShown(java.awt.event.ComponentEvent e)
Specified by:
componentShown in interface java.awt.event.ComponentListener

componentResized

public void componentResized(java.awt.event.ComponentEvent e)
Specified by:
componentResized in interface java.awt.event.ComponentListener

mouseWheelMoved

public void mouseWheelMoved(java.awt.event.MouseWheelEvent e)
Specified by:
mouseWheelMoved in interface java.awt.event.MouseWheelListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Specified by:
keyTyped in interface java.awt.event.KeyListener


Copyright © 2010 Singular Systems http://www.singularsys.com/jep