KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > geom > SegmentList


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.geom;
19
20 import java.awt.Shape JavaDoc;
21 import java.awt.geom.PathIterator JavaDoc;
22 import java.awt.geom.Point2D JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * A class representing a list of path segments.
30  *
31  * @version $Id: SegmentList.java,v 1.4 2005/04/02 14:26:09 deweese Exp $
32  */

33 public class SegmentList {
34     List JavaDoc segments = new LinkedList JavaDoc();
35
36     public SegmentList() {
37     }
38
39     public SegmentList(Shape JavaDoc s) {
40         PathIterator JavaDoc pi = s.getPathIterator(null);
41         float pts [] = new float[6];
42         int type;
43         Point2D.Double JavaDoc loc = null;
44         Point2D.Double JavaDoc openLoc = null;
45         while (!pi.isDone()) {
46             type = pi.currentSegment(pts);
47             switch (type) {
48             case PathIterator.SEG_MOVETO:
49                 openLoc = loc = new Point2D.Double JavaDoc(pts[0], pts[1] );
50                 break;
51             case PathIterator.SEG_LINETO: {
52                 Point2D.Double JavaDoc p0 = new Point2D.Double JavaDoc(pts[0], pts[1] );
53                 segments.add(new Linear(loc, p0));
54                 loc = p0;
55             }
56                 break;
57
58             case PathIterator.SEG_QUADTO: {
59                 Point2D.Double JavaDoc p0 = new Point2D.Double JavaDoc(pts[0], pts[1] );
60                 Point2D.Double JavaDoc p1 = new Point2D.Double JavaDoc(pts[2], pts[3] );
61                 segments.add(new Quadradic(loc, p0, p1));
62                 loc = p1;
63             }
64                 break;
65
66             case PathIterator.SEG_CUBICTO: {
67                 Point2D.Double JavaDoc p0 = new Point2D.Double JavaDoc(pts[0], pts[1] );
68                 Point2D.Double JavaDoc p1 = new Point2D.Double JavaDoc(pts[2], pts[3] );
69                 Point2D.Double JavaDoc p2 = new Point2D.Double JavaDoc(pts[4], pts[5] );
70                 segments.add(new Cubic(loc, p0, p1, p2));
71                 loc = p2;
72             }
73                 break;
74
75             case PathIterator.SEG_CLOSE:
76                 segments.add(new Linear(loc, openLoc));
77                 loc = openLoc;
78                 break;
79             }
80             pi.next();
81         }
82     }
83
84     public Rectangle2D JavaDoc getBounds2D() {
85         Iterator JavaDoc iter = iterator();
86         if (!iter.hasNext()) return null;
87
88         Rectangle2D JavaDoc ret;
89         ret = (Rectangle2D JavaDoc)((Segment)iter.next()).getBounds2D().clone();
90         while (iter.hasNext()) {
91             Segment seg = (Segment)iter.next();
92             Rectangle2D JavaDoc segB = seg.getBounds2D();
93             Rectangle2D.union(segB, ret, ret);
94         }
95         return ret;
96     }
97
98     public void add(Segment s) {
99         segments.add(s);
100     }
101
102     public Iterator JavaDoc iterator() { return segments.iterator(); }
103
104     public int size() { return segments.size(); }
105
106     public SegmentList.SplitResults split(double y) {
107         Iterator JavaDoc iter = segments.iterator();
108         SegmentList above = null;
109         SegmentList below = null;
110         while (iter.hasNext()) {
111             Segment seg = (Segment)iter.next();
112             Segment.SplitResults results = seg.split(y);
113             if (results == null) {
114         Rectangle2D JavaDoc bounds = seg.getBounds2D();
115                 if (bounds.getY() > y) {
116             if (below == null) below = new SegmentList();
117             below.add(seg);
118         } else if (bounds.getY() == y) {
119             if (bounds.getHeight() != 0) {
120             if (below == null) below = new SegmentList();
121             below.add(seg);
122             }
123                 } else {
124                     if (above == null) above = new SegmentList();
125                     above.add(seg);
126                 }
127                 continue;
128             }
129
130             Segment [] resAbove = results.getAbove();
131             for(int i=0; i<resAbove.length; i++) {
132                 if (above == null) above = new SegmentList();
133                 above.add(resAbove[i]);
134             }
135
136             Segment [] resBelow = results.getBelow();
137             for(int i=0; i<resBelow.length; i++) {
138                 if (below == null) below = new SegmentList();
139                 below.add(resBelow[i]);
140             }
141         }
142         return new SegmentList.SplitResults(above, below);
143     }
144
145     public static class SplitResults {
146         SegmentList above, below;
147         public SplitResults(SegmentList above, SegmentList below) {
148             this.above = above;
149             this.below = below;
150         }
151
152         public SegmentList getAbove() { return above; }
153         public SegmentList getBelow() { return below; }
154     }
155 }
156
Popular Tags