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.style;
034:
035: import java.awt.BasicStroke;
036: import java.awt.Graphics2D;
037: import java.awt.geom.AffineTransform;
038: import java.awt.geom.GeneralPath;
039: import java.awt.geom.NoninvertibleTransformException;
040: import java.awt.geom.Point2D;
041:
042: import com.vividsolutions.jump.I18N;
043: import com.vividsolutions.jump.workbench.model.Layer;
044: import com.vividsolutions.jump.workbench.ui.GUIUtil;
045: import com.vividsolutions.jump.workbench.ui.Viewport;
046: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
047:
048: public class ArrowLineStringEndpointStyle extends
049: LineStringEndpointStyle {
050: private final static double SMALL_ANGLE = 10;
051: private final static double MEDIUM_ANGLE = 30;
052:
053: private final static double MEDIUM_LENGTH = 10;
054: private final static double LARGE_LENGTH = 15;
055: private boolean filled;
056: private double finAngle;
057: protected double finLength;
058:
059: /**
060: * @param finAngle degrees
061: * @param finLength pixels
062: */
063: public ArrowLineStringEndpointStyle(String name, boolean start,
064: String iconFile, double finAngle, double finLength,
065: boolean filled) {
066: super (name, IconLoader.icon(iconFile), start);
067: this .finAngle = finAngle;
068: this .finLength = finLength;
069: this .filled = filled;
070: }
071:
072: protected void paint(Point2D terminal, Point2D next,
073: Viewport viewport, Graphics2D graphics)
074: throws NoninvertibleTransformException {
075: if (terminal.equals(next)) {
076: return;
077: }
078:
079: graphics.setColor(lineColorWithAlpha);
080: graphics.setStroke(stroke);
081:
082: GeneralPath arrowhead = arrowhead(terminal, next, finLength,
083: finAngle);
084:
085: if (filled) {
086: arrowhead.closePath();
087: graphics.fill(arrowhead);
088: }
089:
090: //#fill isn't affected by line width, but #draw is. Therefore, draw even
091: //if we've already filled. [Jon Aquino]
092: graphics.draw(arrowhead);
093: }
094:
095: /**
096: * @param tail the tail of the whole arrow; just used to determine angle
097: * @param finLength required distance from the tip to each fin's tip
098: */
099: private GeneralPath arrowhead(Point2D shaftTip, Point2D shaftTail,
100: double finLength, double finAngle) {
101: GeneralPath arrowhead = new GeneralPath();
102: Point2D finTip1 = fin(shaftTip, shaftTail, finLength, finAngle);
103: Point2D finTip2 = fin(shaftTip, shaftTail, finLength, -finAngle);
104: arrowhead
105: .moveTo((float) finTip1.getX(), (float) finTip1.getY());
106: arrowhead.lineTo((float) shaftTip.getX(), (float) shaftTip
107: .getY());
108: arrowhead
109: .lineTo((float) finTip2.getX(), (float) finTip2.getY());
110:
111: return arrowhead;
112: }
113:
114: private Point2D fin(Point2D shaftTip, Point2D shaftTail,
115: double length, double angle) {
116: double shaftLength = shaftTip.distance(shaftTail);
117: Point2D finTail = shaftTip;
118: Point2D finTip = GUIUtil.add(GUIUtil.multiply(GUIUtil.subtract(
119: shaftTail, shaftTip), length / shaftLength), finTail);
120: AffineTransform affineTransform = new AffineTransform();
121: affineTransform.rotate((angle * Math.PI) / 180, finTail.getX(),
122: finTail.getY());
123:
124: return affineTransform.transform(finTip, null);
125: }
126:
127: public abstract static class Feathers extends
128: ArrowLineStringEndpointStyle {
129: private final static int SPACING = 5;
130: private final static int FEATHERS = 2;
131:
132: public Feathers(String name, boolean start, String iconFile) {
133: super (name, start, iconFile, MEDIUM_ANGLE, MEDIUM_LENGTH,
134: false);
135: }
136:
137: protected void paint(Point2D terminal, Point2D next,
138: Viewport viewport, Graphics2D graphics)
139: throws NoninvertibleTransformException {
140: for (int i = 0; i < FEATHERS; i++) {
141: Point2D unit = GUIUtil.multiply(GUIUtil.subtract(next,
142: terminal), 1d / next.distance(terminal));
143: Point2D pseudoTerminal = GUIUtil.add(terminal, GUIUtil
144: .multiply(unit, (finLength + (i * SPACING))));
145: super .paint(pseudoTerminal, terminal, viewport,
146: graphics);
147: }
148: }
149:
150: public void initialize(Layer layer) {
151: super .initialize(layer);
152:
153: //Force line width to 1; anything thicker looks ugly (feathers will run
154: //into each other). [Jon Aquino]
155: stroke = new BasicStroke(1, BasicStroke.CAP_ROUND,
156: BasicStroke.JOIN_ROUND);
157: }
158: }
159:
160: public static class FeathersStart extends Feathers {
161: public FeathersStart() {
162: super (
163: I18N
164: .get("ui.renderer.style.ArrowLineStringEndpointStyle.start-feathers"),
165: true, "FeathersStart.gif");
166: }
167: }
168:
169: public static class FeathersEnd extends Feathers {
170: public FeathersEnd() {
171: super (
172: I18N
173: .get("ui.renderer.style.ArrowLineStringEndpointStyle.end-feathers"),
174: false, "FeathersEnd.gif");
175: }
176: }
177:
178: public static class OpenStart extends ArrowLineStringEndpointStyle {
179: public OpenStart() {
180: super (
181: I18N
182: .get("ui.renderer.style.ArrowLineStringEndpointStyle.start-arrow-open"),
183: true, "ArrowStartOpen.gif", MEDIUM_ANGLE,
184: MEDIUM_LENGTH, false);
185: }
186: }
187:
188: public static class OpenEnd extends ArrowLineStringEndpointStyle {
189: public OpenEnd() {
190: super (
191: I18N
192: .get("ui.renderer.style.ArrowLineStringEndpointStyle.end-arrow-open"),
193: false, "ArrowEndOpen.gif", MEDIUM_ANGLE,
194: MEDIUM_LENGTH, false);
195: }
196: }
197:
198: public static class SolidStart extends ArrowLineStringEndpointStyle {
199: public SolidStart() {
200: super (
201: I18N
202: .get("ui.renderer.style.ArrowLineStringEndpointStyle.start-arrow-solid"),
203: true, "ArrowStartSolid.gif", MEDIUM_ANGLE,
204: MEDIUM_LENGTH, true);
205: }
206: }
207:
208: public static class SolidEnd extends ArrowLineStringEndpointStyle {
209: public SolidEnd() {
210: super (
211: I18N
212: .get("ui.renderer.style.ArrowLineStringEndpointStyle.end-arrow-solid"),
213: false, "ArrowEndSolid.gif", MEDIUM_ANGLE,
214: MEDIUM_LENGTH, true);
215: }
216: }
217:
218: public static class NarrowSolidStart extends
219: ArrowLineStringEndpointStyle {
220: public NarrowSolidStart() {
221: super (
222: I18N
223: .get("ui.renderer.style.ArrowLineStringEndpointStyle.start-arrow-solid-narrow"),
224: true, "ArrowStartSolidNarrow.gif", SMALL_ANGLE,
225: LARGE_LENGTH, true);
226: }
227: }
228:
229: public static class NarrowSolidEnd extends
230: ArrowLineStringEndpointStyle {
231: public NarrowSolidEnd() {
232: super (
233: I18N
234: .get("ui.renderer.style.ArrowLineStringEndpointStyle.end-arrow-solid-narrow"),
235: false, "ArrowEndSolidNarrow.gif", SMALL_ANGLE,
236: LARGE_LENGTH, true);
237: }
238: }
239: }
|