001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.snap;
034:
035: import java.awt.BasicStroke;
036: import java.awt.Color;
037: import java.awt.Graphics2D;
038: import java.awt.Stroke;
039: import java.awt.geom.NoninvertibleTransformException;
040: import java.awt.geom.Point2D;
041:
042: import com.vividsolutions.jts.geom.Coordinate;
043: import com.vividsolutions.jump.util.Blackboard;
044: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
045: import com.vividsolutions.jump.workbench.ui.renderer.SimpleRenderer;
046:
047: public class GridRenderer extends SimpleRenderer {
048: public final static String CONTENT_ID = "GRID";
049: public final static String ENABLED_KEY = GridRenderer.class
050: + " - ENABLED";
051: public final static String DOTS_ENABLED_KEY = GridRenderer.class
052: + " - DOTS ENABLED";
053: public final static String LINES_ENABLED_KEY = GridRenderer.class
054: + " - LINES ENABLED";
055: private Blackboard blackboard;
056:
057: public GridRenderer(Blackboard blackboard, LayerViewPanel panel) {
058: super (CONTENT_ID, panel);
059: this .blackboard = blackboard;
060: }
061:
062: protected void paint(Graphics2D g)
063: throws NoninvertibleTransformException {
064:
065: if (!blackboard.get(ENABLED_KEY, false)) {
066: return;
067: }
068:
069: double gridSize = blackboard.get(
070: SnapToGridPolicy.GRID_SIZE_KEY, 20d);
071: double viewGridSize = gridSize * panel.getViewport().getScale();
072:
073: if (viewGridSize < 5) {
074: return;
075: }
076:
077: g.setColor(Color.lightGray);
078:
079: double minModelX = Math.floor(panel.getViewport()
080: .getEnvelopeInModelCoordinates().getMinX()
081: / gridSize)
082: * gridSize;
083: double maxModelX = Math.ceil(panel.getViewport()
084: .getEnvelopeInModelCoordinates().getMaxX()
085: / gridSize)
086: * gridSize;
087: double minModelY = Math.floor(panel.getViewport()
088: .getEnvelopeInModelCoordinates().getMinY()
089: / gridSize)
090: * gridSize;
091: double maxModelY = Math.ceil(panel.getViewport()
092: .getEnvelopeInModelCoordinates().getMaxY()
093: / gridSize)
094: * gridSize;
095:
096: if (blackboard.get(DOTS_ENABLED_KEY, false)) {
097: paintDots(g, gridSize, minModelX, maxModelX, minModelY,
098: maxModelY);
099: }
100:
101: if (blackboard.get(LINES_ENABLED_KEY, false)) {
102: paintLines(g, gridSize, minModelX, maxModelX, minModelY,
103: maxModelY);
104: }
105: }
106:
107: private void paintDots(Graphics2D g, double gridSize,
108: double minModelX, double maxModelX, double minModelY,
109: double maxModelY) throws NoninvertibleTransformException {
110: for (double x = minModelX; x < maxModelX; x += gridSize) {
111: for (double y = minModelY; y < maxModelY; y += gridSize) {
112: Point2D p = panel.getViewport().toViewPoint(
113: new Coordinate(x, y));
114: g.drawLine((int) p.getX(), (int) p.getY(), (int) p
115: .getX(), (int) p.getY());
116: }
117: }
118: }
119:
120: private Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
121: BasicStroke.JOIN_BEVEL, 0, new float[] { 1, 2 }, 0);
122:
123: private void paintLines(Graphics2D g, double gridSize,
124: double minModelX, double maxModelX, double minModelY,
125: double maxModelY) throws NoninvertibleTransformException {
126: g.setStroke(stroke);
127: for (double x = minModelX; x < maxModelX; x += gridSize) {
128: Point2D min = panel.getViewport().toViewPoint(
129: new Coordinate(x, minModelY));
130: Point2D max = panel.getViewport().toViewPoint(
131: new Coordinate(x, maxModelY));
132: g.drawLine((int) min.getX(), (int) min.getY(), (int) max
133: .getX(), (int) max.getY());
134: }
135:
136: for (double y = minModelY; y < maxModelY; y += gridSize) {
137: Point2D min = panel.getViewport().toViewPoint(
138: new Coordinate(minModelX, y));
139: Point2D max = panel.getViewport().toViewPoint(
140: new Coordinate(maxModelX, y));
141: g.drawLine((int) min.getX(), (int) min.getY(), (int) max
142: .getX(), (int) max.getY());
143: }
144: }
145: }
|