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.renderer.java2D;
034:
035: import java.awt.Rectangle;
036: import java.awt.Shape;
037: import java.awt.geom.AffineTransform;
038: import java.awt.geom.GeneralPath;
039: import java.awt.geom.NoninvertibleTransformException;
040: import java.awt.geom.PathIterator;
041: import java.awt.geom.Point2D;
042: import java.awt.geom.Rectangle2D;
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046:
047: import com.vividsolutions.jts.geom.Coordinate;
048: import com.vividsolutions.jts.geom.GeometryFactory;
049: import com.vividsolutions.jts.geom.LineString;
050: import com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter.LineStringPath;
051:
052: // Converted PolygonShape from java.awt.Polygon to GeneralPath
053: // for more accurate (float instead of int) rendering.
054: // From larry becker's SkyJUMP code to OpenJUMP [mmichaud]
055: public class PolygonShape implements Shape {
056: private GeneralPath shell;
057: private ArrayList holes = new ArrayList();
058:
059: public PolygonShape() {
060: shell = null;
061: holes = null;
062: }
063:
064: /**
065: * @param shellVertices in view coordinates
066: * @param holeVerticesCollection a Coordinate[] for each hole, in view coordinates
067: */
068: public PolygonShape(Coordinate[] shellVertices,
069: Collection holeVerticesCollection) {
070: shell = toPolygon(shellVertices);
071:
072: for (Iterator i = holeVerticesCollection.iterator(); i
073: .hasNext();) {
074: Coordinate[] holeVertices = (Coordinate[]) i.next();
075: holes.add(toPolygon(holeVertices));
076: }
077: }
078:
079: class PolygonPath implements PathIterator {
080: private int iterate;
081: private int numPoints;
082: private Coordinate[] points;
083:
084: public PolygonPath(Coordinate[] coordinates) {
085: points = coordinates;
086: this .numPoints = points.length;
087: iterate = 0;
088: }
089:
090: private int getSegType() {
091: // Tip from Larry Becker to have nice JOIN_BEVEL 2007-07-13 [mmichaud]
092: if (iterate == numPoints - 1)
093: return PathIterator.SEG_CLOSE;
094: return (iterate == 0) ? PathIterator.SEG_MOVETO
095: : PathIterator.SEG_LINETO;
096: }
097:
098: public int currentSegment(double[] coords) {
099: coords[0] = points[iterate].x;
100: coords[1] = points[iterate].y;
101: return getSegType();
102: }
103:
104: public int currentSegment(float[] coords) {
105: coords[0] = (float) points[iterate].x;
106: coords[1] = (float) points[iterate].y;
107: return getSegType();
108: }
109:
110: public int getWindingRule() {
111: return GeneralPath.WIND_EVEN_ODD;
112: }
113:
114: public boolean isDone() {
115: return !(iterate < numPoints);
116: }
117:
118: public void next() {
119: iterate++;
120: }
121: }
122:
123: public final GeneralPath toPolygon(Coordinate[] coordinates) {
124: int numPoints = coordinates.length;
125: GeneralPath shape = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
126: numPoints);
127: PathIterator pi = new PolygonPath(coordinates);
128: shape.append(pi, false);
129: return shape;
130: // GeneralPath shape = new GeneralPath(GeneralPath.WIND_EVEN_ODD, numPoints);
131: // shape.moveTo((float) coordinates[0].x, (float) coordinates[0].y);
132: // for (int i = 1; i < numPoints; i++) {
133: // shape.lineTo((float) coordinates[i].x, (float) coordinates[i].y);
134: // }
135: // return shape;
136:
137: // java.awt.Polygon polygon = new java.awt.Polygon();
138: //
139: // for (int i = 0; i < coordinates.length; i++) {
140: // polygon.addPoint((int) coordinates[i].x, (int) coordinates[i].y);
141: // }
142: //
143: // return polygon;
144: }
145:
146: /*
147: private java.awt.Polygon toPolygon(Coordinate[] coordinates) {
148: java.awt.Polygon polygon = new java.awt.Polygon();
149:
150: for (int i = 0; i < coordinates.length; i++) {
151: polygon.addPoint((int) coordinates[i].x, (int) coordinates[i].y);
152: }
153:
154: return polygon;
155: }
156: */
157:
158: public Rectangle getBounds() {
159: /**@todo Implement this java.awt.Shape method*/
160: throw new java.lang.UnsupportedOperationException(
161: "Method getBounds() not yet implemented.");
162: }
163:
164: public Rectangle2D getBounds2D() {
165: return shell.getBounds2D();
166: }
167:
168: public boolean contains(double x, double y) {
169: /**@todo Implement this java.awt.Shape method*/
170: throw new java.lang.UnsupportedOperationException(
171: "Method contains() not yet implemented.");
172: }
173:
174: public boolean contains(Point2D p) {
175: /**@todo Implement this java.awt.Shape method*/
176: throw new java.lang.UnsupportedOperationException(
177: "Method contains() not yet implemented.");
178: }
179:
180: public boolean intersects(double x, double y, double w, double h) {
181: /**@todo Implement this java.awt.Shape method*/
182: throw new java.lang.UnsupportedOperationException(
183: "Method intersects() not yet implemented.");
184: }
185:
186: public boolean intersects(Rectangle2D r) {
187: /**@todo Implement this java.awt.Shape method*/
188: throw new java.lang.UnsupportedOperationException(
189: "Method intersects() not yet implemented.");
190: }
191:
192: public boolean contains(double x, double y, double w, double h) {
193: /**@todo Implement this java.awt.Shape method*/
194: throw new java.lang.UnsupportedOperationException(
195: "Method contains() not yet implemented.");
196: }
197:
198: public boolean contains(Rectangle2D r) {
199: /**@todo Implement this java.awt.Shape method*/
200: throw new java.lang.UnsupportedOperationException(
201: "Method contains() not yet implemented.");
202: }
203:
204: public PathIterator getPathIterator(AffineTransform at) {
205: ArrayList rings = new ArrayList();
206: rings.add(shell);
207: rings.addAll(holes);
208:
209: return new ShapeCollectionPathIterator(rings, at);
210: }
211:
212: public PathIterator getPathIterator(AffineTransform at,
213: double flatness) {
214: // since we don't support curved geometries, can simply delegate to the simple method
215: return getPathIterator(at);
216: }
217: }
|