root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / GeneralPathX.java @ 2859
History | View | Annotate | Download (24.3 KB)
1 | 214 | fernando | /*
|
---|---|---|---|
2 | * Created on 10-jun-2004
|
||
3 | *
|
||
4 | * TODO To change the template for this generated file go to
|
||
5 | * Window - Preferences - Java - Code Generation - Code and Comments
|
||
6 | */
|
||
7 | 1100 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 | *
|
||
9 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
10 | *
|
||
11 | * This program is free software; you can redistribute it and/or
|
||
12 | * modify it under the terms of the GNU General Public License
|
||
13 | * as published by the Free Software Foundation; either version 2
|
||
14 | * of the License, or (at your option) any later version.
|
||
15 | *
|
||
16 | * This program is distributed in the hope that it will be useful,
|
||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
19 | * GNU General Public License for more details.
|
||
20 | *
|
||
21 | * You should have received a copy of the GNU General Public License
|
||
22 | * along with this program; if not, write to the Free Software
|
||
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
24 | *
|
||
25 | * For more information, contact:
|
||
26 | *
|
||
27 | * Generalitat Valenciana
|
||
28 | * Conselleria d'Infraestructures i Transport
|
||
29 | * Av. Blasco Ib??ez, 50
|
||
30 | * 46010 VALENCIA
|
||
31 | * SPAIN
|
||
32 | *
|
||
33 | * +34 963862235
|
||
34 | * gvsig@gva.es
|
||
35 | * www.gvsig.gva.es
|
||
36 | *
|
||
37 | * or
|
||
38 | *
|
||
39 | * IVER T.I. S.A
|
||
40 | * Salamanca 50
|
||
41 | * 46005 Valencia
|
||
42 | * Spain
|
||
43 | *
|
||
44 | * +34 963163400
|
||
45 | * dac@iver.es
|
||
46 | */
|
||
47 | 214 | fernando | package com.iver.cit.gvsig.fmap.core; |
48 | |||
49 | /**
|
||
50 | * @author FJP
|
||
51 | *
|
||
52 | */
|
||
53 | /*
|
||
54 | * @(#)GeneralPathX.java 1.58 03/01/23
|
||
55 | *
|
||
56 | * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
|
||
57 | * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||
58 | */
|
||
59 | |||
60 | import java.awt.Shape; |
||
61 | import java.awt.geom.AffineTransform; |
||
62 | import java.awt.geom.FlatteningPathIterator; |
||
63 | import java.awt.geom.IllegalPathStateException; |
||
64 | import java.awt.geom.PathIterator; |
||
65 | import java.awt.geom.Point2D; |
||
66 | import java.awt.geom.Rectangle2D; |
||
67 | 2183 | fernando | import java.io.Serializable; |
68 | 214 | fernando | |
69 | 885 | fjp | import org.cresques.cts.ICoordTrans; |
70 | |||
71 | 214 | fernando | import sun.awt.geom.Crossings; |
72 | import sun.awt.geom.Curve; |
||
73 | |||
74 | /**
|
||
75 | * The <code>GeneralPathX</code> class represents a geometric path
|
||
76 | * constructed from straight lines, and quadratic and cubic
|
||
77 | * (Bézier) curves. It can contain multiple subpaths.
|
||
78 | * <p>
|
||
79 | * The winding rule specifies how the interior of a path is
|
||
80 | * determined. There are two types of winding rules:
|
||
81 | * EVEN_ODD and NON_ZERO.
|
||
82 | * <p>
|
||
83 | * An EVEN_ODD winding rule means that enclosed regions
|
||
84 | * of the path alternate between interior and exterior areas as
|
||
85 | * traversed from the outside of the path towards a point inside
|
||
86 | * the region.
|
||
87 | * <p>
|
||
88 | * A NON_ZERO winding rule means that if a ray is
|
||
89 | * drawn in any direction from a given point to infinity
|
||
90 | * and the places where the path intersects
|
||
91 | * the ray are examined, the point is inside of the path if and only if
|
||
92 | * the number of times that the path crosses the ray from
|
||
93 | * left to right does not equal the number of times that the path crosses
|
||
94 | * the ray from right to left.
|
||
95 | * @version 1.58, 01/23/03
|
||
96 | * @author Jim Graham
|
||
97 | */
|
||
98 | 2183 | fernando | public class GeneralPathX implements Shape, Cloneable, Serializable { |
99 | 214 | fernando | /**
|
100 | * An even-odd winding rule for determining the interior of
|
||
101 | * a path.
|
||
102 | */
|
||
103 | public static final int WIND_EVEN_ODD = PathIterator.WIND_EVEN_ODD; |
||
104 | |||
105 | /**
|
||
106 | * A non-zero winding rule for determining the interior of a
|
||
107 | * path.
|
||
108 | */
|
||
109 | public static final int WIND_NON_ZERO = PathIterator.WIND_NON_ZERO; |
||
110 | |||
111 | // For code simplicity, copy these constants to our namespace
|
||
112 | // and cast them to byte constants for easy storage.
|
||
113 | private static final byte SEG_MOVETO = (byte) PathIterator.SEG_MOVETO; |
||
114 | private static final byte SEG_LINETO = (byte) PathIterator.SEG_LINETO; |
||
115 | private static final byte SEG_QUADTO = (byte) PathIterator.SEG_QUADTO; |
||
116 | private static final byte SEG_CUBICTO = (byte) PathIterator.SEG_CUBICTO; |
||
117 | private static final byte SEG_CLOSE = (byte) PathIterator.SEG_CLOSE; |
||
118 | |||
119 | byte[] pointTypes; |
||
120 | double[] pointCoords; |
||
121 | int numTypes;
|
||
122 | int numCoords;
|
||
123 | int windingRule;
|
||
124 | |||
125 | static final int INIT_SIZE = 20; |
||
126 | static final int EXPAND_MAX = 500; |
||
127 | |||
128 | /**
|
||
129 | * Constructs a new <code>GeneralPathX</code> object.
|
||
130 | * If an operation performed on this path requires the
|
||
131 | * interior of the path to be defined then the default NON_ZERO
|
||
132 | * winding rule is used.
|
||
133 | * @see #WIND_NON_ZERO
|
||
134 | */
|
||
135 | public GeneralPathX() {
|
||
136 | this(WIND_NON_ZERO, INIT_SIZE, INIT_SIZE);
|
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * Constructs a new <code>GeneralPathX</code> object with the specified
|
||
141 | * winding rule to control operations that require the interior of the
|
||
142 | * path to be defined.
|
||
143 | * @param rule the winding rule
|
||
144 | * @see #WIND_EVEN_ODD
|
||
145 | * @see #WIND_NON_ZERO
|
||
146 | */
|
||
147 | public GeneralPathX(int rule) { |
||
148 | this(rule, INIT_SIZE, INIT_SIZE);
|
||
149 | } |
||
150 | |||
151 | /**
|
||
152 | * Constructs a new <code>GeneralPathX</code> object with the specified
|
||
153 | * winding rule and the specified initial capacity to store path
|
||
154 | * coordinates. This number is an initial guess as to how many path
|
||
155 | * segments are in the path, but the storage is expanded
|
||
156 | * as needed to store whatever path segments are added to this path.
|
||
157 | * @param rule the winding rule
|
||
158 | * @param initialCapacity the estimate for the number of path segments
|
||
159 | * in the path
|
||
160 | * @see #WIND_EVEN_ODD
|
||
161 | * @see #WIND_NON_ZERO
|
||
162 | */
|
||
163 | public GeneralPathX(int rule, int initialCapacity) { |
||
164 | this(rule, initialCapacity, initialCapacity);
|
||
165 | } |
||
166 | |||
167 | /**
|
||
168 | * Constructs a new <code>GeneralPathX</code> object with the specified
|
||
169 | * winding rule and the specified initial capacities to store point types
|
||
170 | * and coordinates.
|
||
171 | * These numbers are an initial guess as to how many path segments
|
||
172 | * and how many points are to be in the path, but the
|
||
173 | * storage is expanded as needed to store whatever path segments are
|
||
174 | * added to this path.
|
||
175 | * @param rule the winding rule
|
||
176 | * @param initialTypes the estimate for the number of path segments
|
||
177 | * in the path
|
||
178 | * @param initialCapacity the estimate for the number of points
|
||
179 | * @see #WIND_EVEN_ODD
|
||
180 | * @see #WIND_NON_ZERO
|
||
181 | */
|
||
182 | GeneralPathX(int rule, int initialTypes, int initialCoords) { |
||
183 | setWindingRule(rule); |
||
184 | pointTypes = new byte[initialTypes]; |
||
185 | pointCoords = new double[initialCoords * 2]; |
||
186 | } |
||
187 | |||
188 | /**
|
||
189 | * Constructs a new <code>GeneralPathX</code> object from an arbitrary
|
||
190 | * {@link Shape} object.
|
||
191 | * All of the initial geometry and the winding rule for this path are
|
||
192 | * taken from the specified <code>Shape</code> object.
|
||
193 | * @param s the specified <code>Shape</code> object
|
||
194 | */
|
||
195 | public GeneralPathX(Shape s) { |
||
196 | this(WIND_NON_ZERO, INIT_SIZE, INIT_SIZE);
|
||
197 | PathIterator pi = s.getPathIterator(null); |
||
198 | setWindingRule(pi.getWindingRule()); |
||
199 | append(pi, false);
|
||
200 | } |
||
201 | |||
202 | private void needRoom(int newTypes, int newCoords, boolean needMove) { |
||
203 | if (needMove && numTypes == 0) { |
||
204 | throw new IllegalPathStateException("missing initial moveto "+ |
||
205 | "in path definition");
|
||
206 | } |
||
207 | int size = pointCoords.length;
|
||
208 | if (numCoords + newCoords > size) {
|
||
209 | int grow = size;
|
||
210 | if (grow > EXPAND_MAX * 2) { |
||
211 | grow = EXPAND_MAX * 2;
|
||
212 | } |
||
213 | if (grow < newCoords) {
|
||
214 | grow = newCoords; |
||
215 | } |
||
216 | double[] arr = new double[size + grow]; |
||
217 | System.arraycopy(pointCoords, 0, arr, 0, numCoords); |
||
218 | pointCoords = arr; |
||
219 | } |
||
220 | size = pointTypes.length; |
||
221 | if (numTypes + newTypes > size) {
|
||
222 | int grow = size;
|
||
223 | if (grow > EXPAND_MAX) {
|
||
224 | grow = EXPAND_MAX; |
||
225 | } |
||
226 | if (grow < newTypes) {
|
||
227 | grow = newTypes; |
||
228 | } |
||
229 | byte[] arr = new byte[size + grow]; |
||
230 | System.arraycopy(pointTypes, 0, arr, 0, numTypes); |
||
231 | pointTypes = arr; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /**
|
||
236 | * Adds a point to the path by moving to the specified
|
||
237 | * coordinates.
|
||
238 | * @param x, y the specified coordinates
|
||
239 | */
|
||
240 | public synchronized void moveTo(double x, double y) { |
||
241 | if (numTypes > 0 && pointTypes[numTypes - 1] == SEG_MOVETO) { |
||
242 | pointCoords[numCoords - 2] = x;
|
||
243 | pointCoords[numCoords - 1] = y;
|
||
244 | } else {
|
||
245 | needRoom(1, 2, false); |
||
246 | pointTypes[numTypes++] = SEG_MOVETO; |
||
247 | pointCoords[numCoords++] = x; |
||
248 | pointCoords[numCoords++] = y; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | /**
|
||
253 | * Adds a point to the path by drawing a straight line from the
|
||
254 | * current coordinates to the new specified coordinates.
|
||
255 | * @param x, y the specified coordinates
|
||
256 | */
|
||
257 | public synchronized void lineTo(double x, double y) { |
||
258 | needRoom(1, 2, true); |
||
259 | pointTypes[numTypes++] = SEG_LINETO; |
||
260 | pointCoords[numCoords++] = x; |
||
261 | pointCoords[numCoords++] = y; |
||
262 | } |
||
263 | |||
264 | /**
|
||
265 | * Adds a curved segment, defined by two new points, to the path by
|
||
266 | * drawing a Quadratic curve that intersects both the current
|
||
267 | * coordinates and the coordinates (x2, y2), using the
|
||
268 | * specified point (x1, y1) as a quadratic parametric control
|
||
269 | * point.
|
||
270 | * @param x1, y1 the coordinates of the first quadratic control
|
||
271 | * point
|
||
272 | * @param x2, y2 the coordinates of the final endpoint
|
||
273 | */
|
||
274 | public synchronized void quadTo(double x1, double y1, double x2, double y2) { |
||
275 | needRoom(1, 4, true); |
||
276 | pointTypes[numTypes++] = SEG_QUADTO; |
||
277 | pointCoords[numCoords++] = x1; |
||
278 | pointCoords[numCoords++] = y1; |
||
279 | pointCoords[numCoords++] = x2; |
||
280 | pointCoords[numCoords++] = y2; |
||
281 | } |
||
282 | |||
283 | /**
|
||
284 | * Adds a curved segment, defined by three new points, to the path by
|
||
285 | * drawing a Bézier curve that intersects both the current
|
||
286 | * coordinates and the coordinates (x3, y3), using the
|
||
287 | * specified points (x1, y1) and (x2, y2) as
|
||
288 | * Bézier control points.
|
||
289 | * @param x1, y1 the coordinates of the first Béezier
|
||
290 | * control point
|
||
291 | * @param x2, y2 the coordinates of the second Bézier
|
||
292 | * control point
|
||
293 | * @param x3, y3 the coordinates of the final endpoint
|
||
294 | */
|
||
295 | public synchronized void curveTo(double x1, double y1, |
||
296 | double x2, double y2, |
||
297 | double x3, double y3) { |
||
298 | needRoom(1, 6, true); |
||
299 | pointTypes[numTypes++] = SEG_CUBICTO; |
||
300 | pointCoords[numCoords++] = x1; |
||
301 | pointCoords[numCoords++] = y1; |
||
302 | pointCoords[numCoords++] = x2; |
||
303 | pointCoords[numCoords++] = y2; |
||
304 | pointCoords[numCoords++] = x3; |
||
305 | pointCoords[numCoords++] = y3; |
||
306 | } |
||
307 | |||
308 | /**
|
||
309 | * Closes the current subpath by drawing a straight line back to
|
||
310 | * the coordinates of the last <code>moveTo</code>. If the path is already
|
||
311 | * closed then this method has no effect.
|
||
312 | */
|
||
313 | public synchronized void closePath() { |
||
314 | if (numTypes == 0 || pointTypes[numTypes - 1] != SEG_CLOSE) { |
||
315 | needRoom(1, 0, true); |
||
316 | pointTypes[numTypes++] = SEG_CLOSE; |
||
317 | } |
||
318 | } |
||
319 | |||
320 | /**
|
||
321 | * Appends the geometry of the specified <code>Shape</code> object to the
|
||
322 | * path, possibly connecting the new geometry to the existing path
|
||
323 | * segments with a line segment.
|
||
324 | * If the <code>connect</code> parameter is <code>true</code> and the
|
||
325 | * path is not empty then any initial <code>moveTo</code> in the
|
||
326 | * geometry of the appended <code>Shape</code>
|
||
327 | * is turned into a <code>lineTo</code> segment.
|
||
328 | * If the destination coordinates of such a connecting <code>lineTo</code>
|
||
329 | * segment match the ending coordinates of a currently open
|
||
330 | * subpath then the segment is omitted as superfluous.
|
||
331 | * The winding rule of the specified <code>Shape</code> is ignored
|
||
332 | * and the appended geometry is governed by the winding
|
||
333 | * rule specified for this path.
|
||
334 | * @param s the <code>Shape</code> whose geometry is appended
|
||
335 | * to this path
|
||
336 | * @param connect a boolean to control whether or not to turn an
|
||
337 | * initial <code>moveTo</code> segment into a <code>lineTo</code>
|
||
338 | * segment to connect the new geometry to the existing path
|
||
339 | */
|
||
340 | public void append(Shape s, boolean connect) { |
||
341 | PathIterator pi = s.getPathIterator(null); |
||
342 | append(pi,connect); |
||
343 | } |
||
344 | |||
345 | /**
|
||
346 | * Appends the geometry of the specified
|
||
347 | * {@link PathIterator} object
|
||
348 | * to the path, possibly connecting the new geometry to the existing
|
||
349 | * path segments with a line segment.
|
||
350 | * If the <code>connect</code> parameter is <code>true</code> and the
|
||
351 | * path is not empty then any initial <code>moveTo</code> in the
|
||
352 | * geometry of the appended <code>Shape</code> is turned into a
|
||
353 | * <code>lineTo</code> segment.
|
||
354 | * If the destination coordinates of such a connecting <code>lineTo</code>
|
||
355 | * segment match the ending coordinates of a currently open
|
||
356 | * subpath then the segment is omitted as superfluous.
|
||
357 | * The winding rule of the specified <code>Shape</code> is ignored
|
||
358 | * and the appended geometry is governed by the winding
|
||
359 | * rule specified for this path.
|
||
360 | * @param pi the <code>PathIterator</code> whose geometry is appended to
|
||
361 | * this path
|
||
362 | * @param connect a boolean to control whether or not to turn an
|
||
363 | * initial <code>moveTo</code> segment into a <code>lineTo</code> segment
|
||
364 | * to connect the new geometry to the existing path
|
||
365 | */
|
||
366 | public void append(PathIterator pi, boolean connect) { |
||
367 | double coords[] = new double[6]; |
||
368 | while (!pi.isDone()) {
|
||
369 | switch (pi.currentSegment(coords)) {
|
||
370 | case SEG_MOVETO:
|
||
371 | if (!connect || numTypes < 1 || numCoords < 2) { |
||
372 | moveTo(coords[0], coords[1]); |
||
373 | break;
|
||
374 | } |
||
375 | if (pointTypes[numTypes - 1] != SEG_CLOSE && |
||
376 | pointCoords[numCoords - 2] == coords[0] && |
||
377 | pointCoords[numCoords - 1] == coords[1]) |
||
378 | { |
||
379 | // Collapse out initial moveto/lineto
|
||
380 | break;
|
||
381 | } |
||
382 | // NO BREAK;
|
||
383 | case SEG_LINETO:
|
||
384 | lineTo(coords[0], coords[1]); |
||
385 | break;
|
||
386 | case SEG_QUADTO:
|
||
387 | quadTo(coords[0], coords[1], |
||
388 | coords[2], coords[3]); |
||
389 | break;
|
||
390 | case SEG_CUBICTO:
|
||
391 | curveTo(coords[0], coords[1], |
||
392 | coords[2], coords[3], |
||
393 | coords[4], coords[5]); |
||
394 | break;
|
||
395 | case SEG_CLOSE:
|
||
396 | closePath(); |
||
397 | break;
|
||
398 | } |
||
399 | pi.next(); |
||
400 | connect = false;
|
||
401 | } |
||
402 | } |
||
403 | |||
404 | /**
|
||
405 | * Returns the fill style winding rule.
|
||
406 | * @return an integer representing the current winding rule.
|
||
407 | * @see #WIND_EVEN_ODD
|
||
408 | * @see #WIND_NON_ZERO
|
||
409 | * @see #setWindingRule
|
||
410 | */
|
||
411 | public synchronized int getWindingRule() { |
||
412 | return windingRule;
|
||
413 | } |
||
414 | |||
415 | /**
|
||
416 | * Sets the winding rule for this path to the specified value.
|
||
417 | * @param rule an integer representing the specified
|
||
418 | * winding rule
|
||
419 | * @exception <code>IllegalArgumentException</code> if
|
||
420 | * <code>rule</code> is not either
|
||
421 | * <code>WIND_EVEN_ODD</code> or
|
||
422 | * <code>WIND_NON_ZERO</code>
|
||
423 | * @see #WIND_EVEN_ODD
|
||
424 | * @see #WIND_NON_ZERO
|
||
425 | * @see #getWindingRule
|
||
426 | */
|
||
427 | public void setWindingRule(int rule) { |
||
428 | if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO) {
|
||
429 | throw new IllegalArgumentException("winding rule must be "+ |
||
430 | "WIND_EVEN_ODD or "+
|
||
431 | "WIND_NON_ZERO");
|
||
432 | } |
||
433 | windingRule = rule; |
||
434 | } |
||
435 | |||
436 | /**
|
||
437 | * Returns the coordinates most recently added to the end of the path
|
||
438 | * as a {@link Point2D} object.
|
||
439 | * @return a <code>Point2D</code> object containing the ending
|
||
440 | * coordinates of the path or <code>null</code> if there are no points
|
||
441 | * in the path.
|
||
442 | */
|
||
443 | public synchronized Point2D getCurrentPoint() { |
||
444 | if (numTypes < 1 || numCoords < 2) { |
||
445 | return null; |
||
446 | } |
||
447 | int index = numCoords;
|
||
448 | if (pointTypes[numTypes - 1] == SEG_CLOSE) { |
||
449 | loop: |
||
450 | for (int i = numTypes - 2; i > 0; i--) { |
||
451 | switch (pointTypes[i]) {
|
||
452 | case SEG_MOVETO:
|
||
453 | break loop;
|
||
454 | case SEG_LINETO:
|
||
455 | index -= 2;
|
||
456 | break;
|
||
457 | case SEG_QUADTO:
|
||
458 | index -= 4;
|
||
459 | break;
|
||
460 | case SEG_CUBICTO:
|
||
461 | index -= 6;
|
||
462 | break;
|
||
463 | case SEG_CLOSE:
|
||
464 | break;
|
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | return new Point2D.Double(pointCoords[index - 2], |
||
469 | pointCoords[index - 1]);
|
||
470 | } |
||
471 | |||
472 | /**
|
||
473 | * Resets the path to empty. The append position is set back to the
|
||
474 | * beginning of the path and all coordinates and point types are
|
||
475 | * forgotten.
|
||
476 | */
|
||
477 | public synchronized void reset() { |
||
478 | numTypes = numCoords = 0;
|
||
479 | } |
||
480 | |||
481 | /**
|
||
482 | * Transforms the geometry of this path using the specified
|
||
483 | * {@link AffineTransform}.
|
||
484 | * The geometry is transformed in place, which permanently changes the
|
||
485 | * boundary defined by this object.
|
||
486 | * @param at the <code>AffineTransform</code> used to transform the area
|
||
487 | */
|
||
488 | public void transform(AffineTransform at) { |
||
489 | at.transform(pointCoords, 0, pointCoords, 0, numCoords / 2); |
||
490 | } |
||
491 | 885 | fjp | |
492 | public void reProject(ICoordTrans ct) |
||
493 | { |
||
494 | Point2D pt = new Point2D.Double(); |
||
495 | for (int i = 0; i < numCoords; i+=2) |
||
496 | { |
||
497 | pt.setLocation(pointCoords[i], pointCoords[i+1]);
|
||
498 | pt = ct.convert(pt,null);
|
||
499 | pointCoords[i] = pt.getX(); |
||
500 | pointCoords[i+1] = pt.getY();
|
||
501 | } |
||
502 | |||
503 | } |
||
504 | 214 | fernando | |
505 | 2859 | fjp | |
506 | 214 | fernando | /**
|
507 | * Returns a new transformed <code>Shape</code>.
|
||
508 | * @param at the <code>AffineTransform</code> used to transform a
|
||
509 | * new <code>Shape</code>.
|
||
510 | * @return a new <code>Shape</code>, transformed with the specified
|
||
511 | * <code>AffineTransform</code>.
|
||
512 | */
|
||
513 | public synchronized Shape createTransformedShape(AffineTransform at) { |
||
514 | GeneralPathX gp = (GeneralPathX) clone(); |
||
515 | if (at != null) { |
||
516 | gp.transform(at); |
||
517 | } |
||
518 | return gp;
|
||
519 | } |
||
520 | |||
521 | /**
|
||
522 | * Return the bounding box of the path.
|
||
523 | * @return a {@link java.awt.Rectangle} object that
|
||
524 | * bounds the current path.
|
||
525 | */
|
||
526 | public java.awt.Rectangle getBounds() {
|
||
527 | return getBounds2D().getBounds();
|
||
528 | } |
||
529 | |||
530 | /**
|
||
531 | * Returns the bounding box of the path.
|
||
532 | * @return a {@link Rectangle2D} object that
|
||
533 | * bounds the current path.
|
||
534 | */
|
||
535 | public synchronized Rectangle2D getBounds2D() { |
||
536 | double x1, y1, x2, y2;
|
||
537 | int i = numCoords;
|
||
538 | if (i > 0) { |
||
539 | y1 = y2 = pointCoords[--i]; |
||
540 | x1 = x2 = pointCoords[--i]; |
||
541 | while (i > 0) { |
||
542 | double y = pointCoords[--i];
|
||
543 | double x = pointCoords[--i];
|
||
544 | if (x < x1) x1 = x;
|
||
545 | if (y < y1) y1 = y;
|
||
546 | if (x > x2) x2 = x;
|
||
547 | if (y > y2) y2 = y;
|
||
548 | } |
||
549 | } else {
|
||
550 | x1 = y1 = x2 = y2 = 0.0f;
|
||
551 | } |
||
552 | return new Rectangle2D.Double(x1, y1, x2 - x1, y2 - y1); |
||
553 | } |
||
554 | |||
555 | /**
|
||
556 | * Tests if the specified coordinates are inside the boundary of
|
||
557 | * this <code>Shape</code>.
|
||
558 | * @param x, y the specified coordinates
|
||
559 | * @return <code>true</code> if the specified coordinates are inside this
|
||
560 | * <code>Shape</code>; <code>false</code> otherwise
|
||
561 | */
|
||
562 | public boolean contains(double x, double y) { |
||
563 | if (numTypes < 2) { |
||
564 | return false; |
||
565 | } |
||
566 | int cross = Curve.crossingsForPath(getPathIterator(null), x, y); |
||
567 | if (windingRule == WIND_NON_ZERO) {
|
||
568 | return (cross != 0); |
||
569 | } else {
|
||
570 | return ((cross & 1) != 0); |
||
571 | } |
||
572 | } |
||
573 | |||
574 | /**
|
||
575 | * Tests if the specified <code>Point2D</code> is inside the boundary
|
||
576 | * of this <code>Shape</code>.
|
||
577 | * @param p the specified <code>Point2D</code>
|
||
578 | * @return <code>true</code> if this <code>Shape</code> contains the
|
||
579 | * specified <code>Point2D</code>, <code>false</code> otherwise.
|
||
580 | */
|
||
581 | public boolean contains(Point2D p) { |
||
582 | return contains(p.getX(), p.getY());
|
||
583 | } |
||
584 | |||
585 | /**
|
||
586 | * Tests if the specified rectangular area is inside the boundary of
|
||
587 | * this <code>Shape</code>.
|
||
588 | * @param x, y the specified coordinates
|
||
589 | * @param w the width of the specified rectangular area
|
||
590 | * @param h the height of the specified rectangular area
|
||
591 | * @return <code>true</code> if this <code>Shape</code> contains
|
||
592 | * the specified rectangluar area; <code>false</code> otherwise.
|
||
593 | */
|
||
594 | public boolean contains(double x, double y, double w, double h) { |
||
595 | Crossings c = Crossings.findCrossings(getPathIterator(null),
|
||
596 | x, y, x+w, y+h); |
||
597 | return (c != null && c.covers(y, y+h)); |
||
598 | } |
||
599 | |||
600 | /**
|
||
601 | * Tests if the specified <code>Rectangle2D</code>
|
||
602 | * is inside the boundary of this <code>Shape</code>.
|
||
603 | * @param r a specified <code>Rectangle2D</code>
|
||
604 | * @return <code>true</code> if this <code>Shape</code> bounds the
|
||
605 | * specified <code>Rectangle2D</code>; <code>false</code> otherwise.
|
||
606 | */
|
||
607 | public boolean contains(Rectangle2D r) { |
||
608 | return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
|
||
609 | } |
||
610 | |||
611 | /**
|
||
612 | * Tests if the interior of this <code>Shape</code> intersects the
|
||
613 | * interior of a specified set of rectangular coordinates.
|
||
614 | * @param x, y the specified coordinates
|
||
615 | * @param w the width of the specified rectangular coordinates
|
||
616 | * @param h the height of the specified rectangular coordinates
|
||
617 | * @return <code>true</code> if this <code>Shape</code> and the
|
||
618 | * interior of the specified set of rectangular coordinates intersect
|
||
619 | * each other; <code>false</code> otherwise.
|
||
620 | */
|
||
621 | public boolean intersects(double x, double y, double w, double h) { |
||
622 | Crossings c = Crossings.findCrossings(getPathIterator(null),
|
||
623 | x, y, x+w, y+h); |
||
624 | return (c == null || !c.isEmpty()); |
||
625 | } |
||
626 | |||
627 | /**
|
||
628 | * Tests if the interior of this <code>Shape</code> intersects the
|
||
629 | * interior of a specified <code>Rectangle2D</code>.
|
||
630 | * @param r the specified <code>Rectangle2D</code>
|
||
631 | * @return <code>true</code> if this <code>Shape</code> and the interior
|
||
632 | * of the specified <code>Rectangle2D</code> intersect each
|
||
633 | * other; <code>false</code> otherwise.
|
||
634 | */
|
||
635 | public boolean intersects(Rectangle2D r) { |
||
636 | return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
|
||
637 | } |
||
638 | |||
639 | /**
|
||
640 | * Returns a <code>PathIterator</code> object that iterates along the
|
||
641 | * boundary of this <code>Shape</code> and provides access to the
|
||
642 | * geometry of the outline of this <code>Shape</code>.
|
||
643 | * The iterator for this class is not multi-threaded safe,
|
||
644 | * which means that this <code>GeneralPathX</code> class does not
|
||
645 | * guarantee that modifications to the geometry of this
|
||
646 | * <code>GeneralPathX</code> object do not affect any iterations of
|
||
647 | * that geometry that are already in process.
|
||
648 | * @param at an <code>AffineTransform</code>
|
||
649 | * @return a new <code>PathIterator</code> that iterates along the
|
||
650 | * boundary of this <code>Shape</code> and provides access to the
|
||
651 | * geometry of this <code>Shape</code>'s outline
|
||
652 | */
|
||
653 | public PathIterator getPathIterator(AffineTransform at) { |
||
654 | return new GeneralPathXIterator(this, at); |
||
655 | } |
||
656 | |||
657 | /**
|
||
658 | * Returns a <code>PathIterator</code> object that iterates along the
|
||
659 | * boundary of the flattened <code>Shape</code> and provides access to the
|
||
660 | * geometry of the outline of the <code>Shape</code>.
|
||
661 | * The iterator for this class is not multi-threaded safe,
|
||
662 | * which means that this <code>GeneralPathX</code> class does not
|
||
663 | * guarantee that modifications to the geometry of this
|
||
664 | * <code>GeneralPathX</code> object do not affect any iterations of
|
||
665 | * that geometry that are already in process.
|
||
666 | * @param at an <code>AffineTransform</code>
|
||
667 | * @param flatness the maximum distance that the line segments used to
|
||
668 | * approximate the curved segments are allowed to deviate
|
||
669 | * from any point on the original curve
|
||
670 | * @return a new <code>PathIterator</code> that iterates along the flattened
|
||
671 | * <code>Shape</code> boundary.
|
||
672 | */
|
||
673 | public PathIterator getPathIterator(AffineTransform at, double flatness) { |
||
674 | return new FlatteningPathIterator(getPathIterator(at), flatness); |
||
675 | } |
||
676 | |||
677 | /**
|
||
678 | * Creates a new object of the same class as this object.
|
||
679 | *
|
||
680 | * @return a clone of this instance.
|
||
681 | * @exception OutOfMemoryError if there is not enough memory.
|
||
682 | * @see java.lang.Cloneable
|
||
683 | * @since 1.2
|
||
684 | */
|
||
685 | public Object clone() { |
||
686 | try {
|
||
687 | GeneralPathX copy = (GeneralPathX) super.clone();
|
||
688 | copy.pointTypes = (byte[]) pointTypes.clone(); |
||
689 | copy.pointCoords = (double[]) pointCoords.clone(); |
||
690 | return copy;
|
||
691 | } catch (CloneNotSupportedException e) { |
||
692 | // this shouldn't happen, since we are Cloneable
|
||
693 | throw new InternalError(); |
||
694 | } |
||
695 | } |
||
696 | |||
697 | GeneralPathX(int windingRule,
|
||
698 | byte[] pointTypes, |
||
699 | int numTypes,
|
||
700 | double[] pointCoords, |
||
701 | int numCoords) {
|
||
702 | |||
703 | // used to construct from native
|
||
704 | |||
705 | this.windingRule = windingRule;
|
||
706 | this.pointTypes = pointTypes;
|
||
707 | this.numTypes = numTypes;
|
||
708 | this.pointCoords = pointCoords;
|
||
709 | this.numCoords = numCoords;
|
||
710 | } |
||
711 | |||
712 | public void flip() |
||
713 | { |
||
714 | byte[] pointTypesAux = new byte[numTypes]; |
||
715 | double[] pointCoordsAux = new double[numCoords]; |
||
716 | int i;
|
||
717 | |||
718 | for (i=0; i< numTypes; i++) |
||
719 | pointTypesAux[numTypes - i -1] = pointTypes[i];
|
||
720 | int numPoints = numCoords/2; |
||
721 | for (i=0; i< numPoints; i++) |
||
722 | { |
||
723 | // la x
|
||
724 | pointCoordsAux[2*(numPoints - i -1)] = pointCoords[2*i]; |
||
725 | // la y
|
||
726 | pointCoordsAux[2*(numPoints - i -1) + 1] = pointCoords[2*i + 1]; |
||
727 | } |
||
728 | |||
729 | pointTypes = pointTypesAux; |
||
730 | pointCoords = pointCoordsAux; |
||
731 | } |
||
732 | } |