001: /*************************************************************************
002: * *
003: * 1) This source code file, in unmodified form, and compiled classes *
004: * derived from it can be used and distributed without restriction, *
005: * including for commercial use. (Attribution is not required *
006: * but is appreciated.) *
007: * *
008: * 2) Modified versions of this file can be made and distributed *
009: * provided: the modified versions are put into a Java package *
010: * different from the original package, edu.hws; modified *
011: * versions are distributed under the same terms as the original; *
012: * and the modifications are documented in comments. (Modification *
013: * here does not include simply making subclasses that belong to *
014: * a package other than edu.hws, which can be done without any *
015: * restriction.) *
016: * *
017: * David J. Eck *
018: * Department of Mathematics and Computer Science *
019: * Hobart and William Smith Colleges *
020: * Geneva, New York 14456, USA *
021: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
022: * *
023: *************************************************************************/package edu.hws.jcm.draw;
024:
025: import java.awt.*;
026:
027: /**
028: * A Grid object draws a graph paper-like grid on a Canvas. The pixel width
029: * and height between adjacent grid lines is specified as a parameter to the
030: * constructer, or through the access methods "setXSP(double)" and
031: * "setYSP(double)". Note that the spacing will be scaled to between PIX_MIN
032: * and PIX_MAX (20 and 80, respectively, by default). The color of the grid
033: * lines can be set, and defaults to (220, 220, 220).
034: *
035: * <p>This class was written by Gabriel Weinstock (with some modifications by David Eck).
036: */
037: public class Grid extends Drawable {
038:
039: private Color gcol = new Color(220, 220, 220);
040: private double xsp, ysp;
041: private final int PIX_MAX = 50, PIX_MIN = 20;
042:
043: /**
044: * Create a Grid object with x and y spacing 1.0. This does not mean that
045: * the actual spacing between grid lines will be 1. It will be some reasonable
046: * fraction or multiply of 1, with the value chosen to give a reasonable
047: * spacing between the grid lines.
048: */
049: public Grid() {
050: this (1.0, 1.0);
051: }
052:
053: /**
054: * Create a Grid object with spacing specified.
055: */
056: public Grid(double xspace, double yspace) {
057: xsp = xspace;
058: ysp = yspace;
059: }
060:
061: /**
062: * Access method which returns the Color of the grid lines.
063: */
064: public Color getColor() {
065: return gcol;
066: }
067:
068: /**
069: * Method to set the Color used to draw grid lines.
070: */
071:
072: public void setColor(Color c) {
073: if (c != null && !c.equals(gcol)) {
074: gcol = c;
075: needsRedraw();
076: }
077: }
078:
079: /**
080: * Access method to return the x spacing used between grid lines.
081: */
082: public double getXSP() {
083: return xsp;
084: }
085:
086: /**
087: * Access method to return the y spacing used between grid lines
088: */
089: public double getYSP() {
090: return ysp;
091: }
092:
093: /**
094: * Method to set the x spacing between grid lines. This does not mean that
095: * the actual spacing between grid lines will be x. It will be some reasonable
096: * fraction or multiply of s, with the value chosen to give a reasonable
097: * spacing between the grid lines.
098: */
099: public void setXSP(double x) {
100: xsp = x;
101: needsRedraw();
102: }
103:
104: /**
105: * Method to set the y spacing between grid lines. This does not mean that
106: * the actual spacing between grid lines will be y. It will be some reasonable
107: * fraction or multiply of s, with the value chosen to give a reasonable
108: * spacing between the grid lines.
109: */
110: public void setYSP(double y) {
111: ysp = y;
112: needsRedraw();
113: }
114:
115: /**
116: * Draws the grid if an update is required. This is not usually called directly.
117: *
118: * @param g the Graphics context
119: * @param coordsch boolean describing whether coordinates have changed
120: */
121: public void draw(Graphics g, boolean coordsch) {
122: if (coords == null)
123: return;
124: double pixwidth = coords.getPixelWidth();
125: double pixheight = coords.getPixelHeight();
126: if (Double.isNaN(pixwidth) || Double.isNaN(pixheight)
127: || Double.isInfinite(pixheight)
128: || Double.isInfinite(pixwidth) || pixwidth == 0
129: || pixheight == 0)
130: return;
131: g.setColor(gcol);
132: // start by drawing vertical grid lines (starting with center):
133: if (xsp > 0) {
134: double x = xsp;
135: while (x > (pixwidth * PIX_MAX))
136: x /= 10;
137: if (x < (pixwidth * PIX_MIN))
138: x *= 5;
139: if (x > (pixwidth * PIX_MAX))
140: x /= 2;
141: int j = (int) (Math.ceil(coords.getXmin() / x));
142: double i = j * x;
143: while (coords.xToPixel(i) < (coords.getWidth() + coords
144: .getLeft())) {
145: g.drawLine(coords.xToPixel(i), coords.getTop(), coords
146: .xToPixel(i), coords.getTop()
147: + coords.getHeight());
148: i += x;
149: }
150: }
151: // next draw horizontal grid lines
152: if (ysp > 0) {
153: double y = ysp;
154: while (y > (pixheight * PIX_MAX))
155: y /= 10;
156: if (y < (pixheight * PIX_MIN))
157: y *= 5;
158: if (y > (pixheight * PIX_MAX))
159: y /= 2;
160: int j = (int) (Math.ceil(coords.getYmin() / y));
161: double i = j * y;
162: while (coords.yToPixel(i) > coords.getTop()) {
163: g.drawLine(coords.getLeft(), coords.yToPixel(i), coords
164: .getLeft()
165: + coords.getWidth(), coords.yToPixel(i));
166: i += y;
167: }
168: }
169: }
170:
171: } // end class Grid
|