galapagos
Class DrawingCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by galapagos.DrawingCanvas
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class DrawingCanvas
extends java.awt.Canvas

Generic Canvas descendant object for drawing lines. This object does not have any intelligence or memory. This object can have zero or more drawers who will be notified when the component refReshing is needed. This object will pass the Graphics object to the associated DrawingController objects. The notified controllers can then draw whatever they want on the passed Graphics object. Date Modified: Feb 24, 2009 Changed the array of DrawingController to List of DrawingController and removed the restriction of maximum number of DrawingController objects associated to this DrawingCanvas.

Author:
Dr Caffeine
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
private  int canvasHeight
          The current height, in pixels, of the canvas.
private  int canvasWidth
          The current width, in pixels, of the canvas.
private  int centerX
          The x-coordinate of the point in the logical coordinate system that corresponds to the physical center of the canvas.
private  int centerY
          The y-coordinate of the point in the logical coordinate system that corresponds to the physical center of the canvas.
private  double DEFAULT_UNIT
          A class constant for the scaling factor, # of pixels per logical unit.
private  java.util.List<DrawingController> drawers
          An array of objects that implements DrawingController interface.
private  boolean drawGrid
          Set to true to draw grid lines.
private  java.awt.Graphics graphic
          A Graphics object for this canvas.
private  int GRID_INTERVAL_FACTOR
          A class constant for the number of logical units between the grid lines.
private  java.awt.Graphics offScreenGraphics
          A Graphics object associated to the offscreen Image used for double-buffering.
private  java.awt.Image offScreenImage
          An Image used for double-buffering.
private  int originX
          The window's pixel coordinate value that corresponds to the x-coordinate of the origin (0,0).
private  int originY
          The window's pixel coordinate value that corresponds to the y-coordinate of the origin (0,0).
private static long serialVersionUID
           
private  double unit
          The current scaling factor value.
 
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
DrawingCanvas()
          A default constructor that creates an instance of the PlottingCanvas class.
 
Method Summary
 void addOwner(DrawingController newdrawer)
          A mutator method that adds another DrawingController that will draw its trajectory on this canvas.
 void clear()
          Erases the current contents of the PlottingCanvas by painting the whole canvas with the background color.
private  void drawAxis(java.awt.Graphics g)
          Draws the X and Y axis in black if the grid lines are shown and in gray if the no grid lines are shown.
private  void drawContents(java.awt.Graphics g)
          Asks the owner DrawingController objects to draw their contents
private  void drawGrids(java.awt.Graphics g)
          Draws the grid lines in cyan.
 void drawPolygon(java.awt.Graphics g, java.awt.Polygon polygon, java.awt.Color color)
          Draws the polygon in the specified color on Graphics g
 void drawText(java.awt.Graphics g, java.awt.Color color, int pensize, Position position, java.lang.String text)
          Draws the text on the Graphics g in the specified color with pensize thickness at position location.
 void init()
          Initializes the necessary objects for double-buffering drawing.
 void paint(java.awt.Graphics g)
          Painting of the canvas is done by drawing the whole contents using the double-buffering technique.
 void plot(java.awt.Graphics graphic, java.awt.Color color, double size, Position pt1, Position pt2)
          Draws a line between two points pt1 and pt2 on the parameter graphic in the specified color and pen size.
 void setGrid(boolean showGrid)
          A mutator method that sets the flag for drawing the grid lines.
 void setOrigin(int x, int y)
          A mutator method that sets the origin point.
 void setUnit(double pixelsPerUnit)
          A mutator method that sets the scaling factor.
 void update(java.awt.Graphics g)
          Overrides the inherited update so no refreshing is done.
 
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

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

DEFAULT_UNIT

private final double DEFAULT_UNIT
A class constant for the scaling factor, # of pixels per logical unit.

See Also:
Constant Field Values

GRID_INTERVAL_FACTOR

private final int GRID_INTERVAL_FACTOR
A class constant for the number of logical units between the grid lines.

See Also:
Constant Field Values

graphic

private java.awt.Graphics graphic
A Graphics object for this canvas.


originX

private int originX
The window's pixel coordinate value that corresponds to the x-coordinate of the origin (0,0). For example, if the canvas size is 800 by 600 and the origin (0,0) of the logical coordinate unit is at the center of the canvas, then originX is set to 400 and originY is set to 300.


originY

private int originY
The window's pixel coordinate value that corresponds to the y-coordinate of the origin (0,0). For example, if the canvas size is 800 by 600 and the origin (0,0) of the logical coordinate unit is at the center of the canvas, then originX is set to 400 and originY is set to 300.


centerX

private int centerX
The x-coordinate of the point in the logical coordinate system that corresponds to the physical center of the canvas.


centerY

private int centerY
The y-coordinate of the point in the logical coordinate system that corresponds to the physical center of the canvas.


unit

private double unit
The current scaling factor value.


drawGrid

private boolean drawGrid
Set to true to draw grid lines.


offScreenImage

private java.awt.Image offScreenImage
An Image used for double-buffering.


offScreenGraphics

private java.awt.Graphics offScreenGraphics
A Graphics object associated to the offscreen Image used for double-buffering.


canvasWidth

private int canvasWidth
The current width, in pixels, of the canvas.


canvasHeight

private int canvasHeight
The current height, in pixels, of the canvas.


drawers

private java.util.List<DrawingController> drawers
An array of objects that implements DrawingController interface.

Constructor Detail

DrawingCanvas

public DrawingCanvas()
A default constructor that creates an instance of the PlottingCanvas class. Grid lines are drawn, the center position of the canvas is set to (0,0) of the logical (user) coordinate, and the scaling unit is set to the default size defined by the constant DEFAULT_UNIT.

Method Detail

addOwner

public void addOwner(DrawingController newdrawer)
A mutator method that adds another DrawingController that will draw its trajectory on this canvas.

Parameters:
newdrawer - A DrawingController that will draw its trajectory on this canvas.

clear

public void clear()
Erases the current contents of the PlottingCanvas by painting the whole canvas with the background color.


drawPolygon

public void drawPolygon(java.awt.Graphics g,
                        java.awt.Polygon polygon,
                        java.awt.Color color)
Draws the polygon in the specified color on Graphics g

Parameters:
g -
polygon -
color -

drawText

public void drawText(java.awt.Graphics g,
                     java.awt.Color color,
                     int pensize,
                     Position position,
                     java.lang.String text)
Draws the text on the Graphics g in the specified color with pensize thickness at position location.

Parameters:
g - the Graphics to draw text on
color - the color of the pen
pensize - the thickness of the pen
position - the position to draw the text
text - the content to display

init

public void init()
Initializes the necessary objects for double-buffering drawing. Gets the size information of the canvas, which is necessary later in translating logical coordinates into the window's pixel coordinates for drawing. For the drawing to occur correctly, this method MUST be called after the container object (e.g. Frame) that contains this canvas is shown and before a drawing method of this canvas is called.


paint

public void paint(java.awt.Graphics g)
Painting of the canvas is done by drawing the whole contents using the double-buffering technique.

Overrides:
paint in class java.awt.Canvas
Parameters:
g - A Graphics object the painting takes place.

plot

public void plot(java.awt.Graphics graphic,
                 java.awt.Color color,
                 double size,
                 Position pt1,
                 Position pt2)
Draws a line between two points pt1 and pt2 on the parameter graphic in the specified color and pen size.

Parameters:
graphic - A Graphics object where the line is drawn.
color - A color used to draw the line.
size - The pen size.
pt1 - The starting point of the line.
pt2 - The ending point of the line.

setGrid

public void setGrid(boolean showGrid)
A mutator method that sets the flag for drawing the grid lines. Passing true will make the grid lines to appear.

Parameters:
showGrid - Pass 'true' to show the grid lines.

setOrigin

public void setOrigin(int x,
                      int y)
A mutator method that sets the origin point. The parameter (x,y) specifies the point in the logical coordinate system that corresponds to the physical center position of the canvas. By default, the center of the canvas represents the point (0,0) of the logical coordinate system. For example, if you pass 100 and 200 for the x and y parameters, the logical coordinate center point is shifted 100 units to the left and 200 units down.

Parameters:
x - The x-coordinate of a logical point that corresponds to the center of the canvas.
y - The y-coordinate of a logical point that corresponds to the center of the canvas.

setUnit

public void setUnit(double pixelsPerUnit)
A mutator method that sets the scaling factor. The default scaling is 2 pixels per single logical unit. Increase the number for a more zoomed view and decrease the number for a more zoomed out view.

Parameters:
pixelsPerUnit - The scaling unit.

update

public void update(java.awt.Graphics g)
Overrides the inherited update so no refreshing is done. Since double-buffering is used, new image is drawn over the old one without erasing the old one. This technique avoids flickering. NOTE: do not call this method directly.

Overrides:
update in class java.awt.Canvas
Parameters:
g - A Graphics object where the drawing takes place.

drawAxis

private void drawAxis(java.awt.Graphics g)
Draws the X and Y axis in black if the grid lines are shown and in gray if the no grid lines are shown.

Parameters:
g - The Graphics object where the X and Y axis are drawn.

drawContents

private void drawContents(java.awt.Graphics g)
Asks the owner DrawingController objects to draw their contents

Parameters:
g - The Graphics object where the contents are drawn.

drawGrids

private void drawGrids(java.awt.Graphics g)
Draws the grid lines in cyan. The spacing between the grid lines is set to (default spacing * scale factor) logical units.

Parameters:
g - The Graphics object where the grid lines are drawn.