Class 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:
  • 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
    • 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

      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