net.sourceforge.webcompmath.draw
Class Grid

java.lang.Object
  extended by java.awt.Component
      extended by net.sourceforge.webcompmath.draw.Drawable
          extended by net.sourceforge.webcompmath.draw.Grid
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable
Direct Known Subclasses:
WCMGridBean

public class Grid
extends Drawable

A Grid object draws a graph paper-like grid on a Canvas. The pixel width and height between adjacent grid lines is specified as a parameter to the constructer, or through the access methods "setXSP(double)" and "setYSP(double)". Note that in PIXEL_BASED mode the spacing will be scaled to between PIX_MIN and PIX_MAX (20 and 80, respectively, by default). The color of the grid lines can be set, and defaults to (220, 220, 220).

In COORD_BASED mode, you directly specify the horizontal and vertical spacing of the grid. As you zoom out, the grid lines get closer together. Grid will avoid overlapping grid lines, always keeping at least the width of the grid lines as spacing between the lines.

Grid also supports a DOTS style, where the lines of the grid are replaced by dots drawn at the points where the grid lines would have intersected.

This class was originally written by Gabriel Weinstock (with some modifications by David Eck, and later enhancement by Tom Downey)

See Also:
Serialized Form

Nested Class Summary
 
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
static int COORD_BASED
          Indicates that the mode of the grid is coordinate based.
static int CROSS_HATCH
          The style of the grid is a cross hatch of vertical and horizontal lines
static int DOTS
          The style of the grid is an array of dots
static int PIXEL_BASED
          Indicates that the mode of the grid is pixel based.
 
Fields inherited from class net.sourceforge.webcompmath.draw.Drawable
canvas, coords
 
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
Grid()
          Create a Grid object with x and y spacing 1.0.
Grid(double xspace, double yspace)
          Create a Grid object with spacing specified.
 
Method Summary
 void draw(java.awt.Graphics g, boolean coordsch)
          Draws the grid if an update is required.
 java.awt.Color getColor()
          Access method which returns the Color of the grid lines.
 int getGridStyle()
          Get the style of the grid, either CROSS_HATCH or DOTS.
 float getLineWidth()
          Get the width of the grid lines (or dots), in pixels.
 int getMode()
          Get the mode of the grid, either PIXEL_BASED or COORD_BASED.
 double getXSP()
          Access method to return the x spacing used between grid lines.
 double getYSP()
          Access method to return the y spacing used between grid lines
 void setColor(java.awt.Color c)
          Method to set the Color used to draw grid lines.
 void setGridStyle(int gridStyle)
          Set the style of the grid, either CROSS_HATCH or DOTS.
 void setLineWidth(float lineWidth)
          Set the width of the grid lines (or dots), in pixels, from 0 to 5.
 void setMode(int mode)
          Set the mode of the grid, either PIXEL_BASED or COORD_BASED.
 void setXSP(double x)
          Method to set the x spacing between grid lines.
 void setYSP(double y)
          Method to set the y spacing between grid lines.
 
Methods inherited from class net.sourceforge.webcompmath.draw.Drawable
getVisible, needsRedraw, setOwnerData, setVisible
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, 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, getAccessibleContext, 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, paint, 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, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PIXEL_BASED

public static final int PIXEL_BASED
Indicates that the mode of the grid is pixel based. The grid will appear to have roughly the same spacing no matter how the graph is zoomed. This is the default.

See Also:
Constant Field Values

COORD_BASED

public static final int COORD_BASED
Indicates that the mode of the grid is coordinate based. The grid spacing will change as the graph is zoomed.

See Also:
Constant Field Values

CROSS_HATCH

public static final int CROSS_HATCH
The style of the grid is a cross hatch of vertical and horizontal lines

See Also:
Constant Field Values

DOTS

public static final int DOTS
The style of the grid is an array of dots

See Also:
Constant Field Values
Constructor Detail

Grid

public Grid()
Create a Grid object with x and y spacing 1.0. This does not mean that the actual spacing between grid lines will be 1. It will be some reasonable fraction or multiply of 1, with the value chosen to give a reasonable spacing between the grid lines.


Grid

public Grid(double xspace,
            double yspace)
Create a Grid object with spacing specified.

Parameters:
xspace - x spacing
yspace - y spacing
Method Detail

getColor

public java.awt.Color getColor()
Access method which returns the Color of the grid lines.

Returns:
grid color

setColor

public void setColor(java.awt.Color c)
Method to set the Color used to draw grid lines.

Parameters:
c - grid color

getXSP

public double getXSP()
Access method to return the x spacing used between grid lines.

Returns:
x spacing

getYSP

public double getYSP()
Access method to return the y spacing used between grid lines

Returns:
y spacing

setXSP

public void setXSP(double x)
Method to set the x spacing between grid lines. This does not mean that the actual spacing between grid lines will be x. It will be some reasonable fraction or multiply of s, with the value chosen to give a reasonable spacing between the grid lines.

Parameters:
x - x spacing

setYSP

public void setYSP(double y)
Method to set the y spacing between grid lines. This does not mean that the actual spacing between grid lines will be y. It will be some reasonable fraction or multiply of s, with the value chosen to give a reasonable spacing between the grid lines.

Parameters:
y - y spacing

getGridStyle

public int getGridStyle()
Get the style of the grid, either CROSS_HATCH or DOTS.

Returns:
Returns the gridStyle.

setGridStyle

public void setGridStyle(int gridStyle)
Set the style of the grid, either CROSS_HATCH or DOTS.

Parameters:
gridStyle - The gridStyle to set.

getLineWidth

public float getLineWidth()
Get the width of the grid lines (or dots), in pixels.

Returns:
Returns the lineWidth.

setLineWidth

public void setLineWidth(float lineWidth)
Set the width of the grid lines (or dots), in pixels, from 0 to 5. If set to 0, the thinnest possible line is drawn.

Parameters:
lineWidth - The lineWidth to set.

getMode

public int getMode()
Get the mode of the grid, either PIXEL_BASED or COORD_BASED.

Returns:
Returns the mode.

setMode

public void setMode(int mode)
Set the mode of the grid, either PIXEL_BASED or COORD_BASED.

Parameters:
mode - The mode to set.

draw

public void draw(java.awt.Graphics g,
                 boolean coordsch)
Draws the grid if an update is required. This is not usually called directly.

Specified by:
draw in class Drawable
Parameters:
g - the Graphics context
coordsch - boolean describing whether coordinates have changed