KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > parser > AWTPathProducer


1 /*
2
3    Copyright 2000-2001,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.parser;
19
20 import java.awt.Shape JavaDoc;
21 import java.awt.geom.Point2D JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24
25 import org.apache.batik.ext.awt.geom.ExtendedGeneralPath;
26
27 /**
28  * This class provides an implementation of the PathHandler that initializes
29  * a Shape from the value of a path's 'd' attribute.
30  *
31  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
32  * @version $Id: AWTPathProducer.java,v 1.7 2004/08/18 07:14:45 vhardy Exp $
33  */

34 public class AWTPathProducer implements PathHandler, ShapeProducer {
35     /**
36      * The temporary value of extendedGeneralPath.
37      */

38     protected ExtendedGeneralPath path;
39
40     /**
41      * The current x position.
42      */

43     protected float currentX;
44
45     /**
46      * The current y position.
47      */

48     protected float currentY;
49
50     /**
51      * The reference x point for smooth arcs.
52      */

53     protected float xCenter;
54
55     /**
56      * The reference y point for smooth arcs.
57      */

58     protected float yCenter;
59
60     /**
61      * The winding rule to use to construct the path.
62      */

63     protected int windingRule;
64
65     /**
66      * Utility method for creating an ExtendedGeneralPath.
67      * @param r The reader used to read the path specification.
68      * @param wr The winding rule to use for creating the path.
69      */

70     public static Shape JavaDoc createShape(Reader JavaDoc r, int wr)
71         throws IOException JavaDoc,
72                ParseException {
73         PathParser p = new PathParser();
74         AWTPathProducer ph = new AWTPathProducer();
75
76         ph.setWindingRule(wr);
77         p.setPathHandler(ph);
78         p.parse(r);
79
80         return ph.getShape();
81     }
82
83     /**
84      * Sets the winding rule used to construct the path.
85      */

86     public void setWindingRule(int i) {
87         windingRule = i;
88     }
89
90     /**
91      * Returns the current winding rule.
92      */

93     public int getWindingRule() {
94         return windingRule;
95     }
96
97     /**
98      * Returns the Shape object initialized during the last parsing.
99      * @return the shape or null if this handler has not been used by
100      * a parser.
101      */

102     public Shape JavaDoc getShape() {
103         return path;
104     }
105
106     /**
107      * Implements {@link PathHandler#startPath()}.
108      */

109     public void startPath() throws ParseException {
110         currentX = 0;
111         currentY = 0;
112         xCenter = 0;
113         yCenter = 0;
114         path = new ExtendedGeneralPath(windingRule);
115     }
116
117     /**
118      * Implements {@link PathHandler#endPath()}.
119      */

120     public void endPath() throws ParseException {
121     }
122
123     /**
124      * Implements {@link PathHandler#movetoRel(float,float)}.
125      */

126     public void movetoRel(float x, float y) throws ParseException {
127         path.moveTo(xCenter = currentX += x, yCenter = currentY += y);
128     }
129
130     /**
131      * Implements {@link PathHandler#movetoAbs(float,float)}.
132      */

133     public void movetoAbs(float x, float y) throws ParseException {
134         path.moveTo(xCenter = currentX = x, yCenter = currentY = y);
135     }
136
137     /**
138      * Implements {@link PathHandler#closePath()}.
139      */

140     public void closePath() throws ParseException {
141         path.closePath();
142         Point2D JavaDoc pt = path.getCurrentPoint();
143         currentX = (float)pt.getX();
144         currentY = (float)pt.getY();
145     }
146
147     /**
148      * Implements {@link PathHandler#linetoRel(float,float)}.
149      */

150     public void linetoRel(float x, float y) throws ParseException {
151         path.lineTo(xCenter = currentX += x, yCenter = currentY += y);
152     }
153
154     /**
155      * Implements {@link PathHandler#linetoAbs(float,float)}.
156      */

157     public void linetoAbs(float x, float y) throws ParseException {
158         path.lineTo(xCenter = currentX = x, yCenter = currentY = y);
159     }
160
161     /**
162      * Implements {@link PathHandler#linetoHorizontalRel(float)}.
163      */

164     public void linetoHorizontalRel(float x) throws ParseException {
165         path.lineTo(xCenter = currentX += x, yCenter = currentY);
166     }
167
168     /**
169      * Implements {@link PathHandler#linetoHorizontalAbs(float)}.
170      */

171     public void linetoHorizontalAbs(float x) throws ParseException {
172         path.lineTo(xCenter = currentX = x, yCenter = currentY);
173     }
174
175     /**
176      * Implements {@link PathHandler#linetoVerticalRel(float)}.
177      */

178     public void linetoVerticalRel(float y) throws ParseException {
179         path.lineTo(xCenter = currentX, yCenter = currentY += y);
180     }
181
182     /**
183      * Implements {@link PathHandler#linetoVerticalAbs(float)}.
184      */

185     public void linetoVerticalAbs(float y) throws ParseException {
186         path.lineTo(xCenter = currentX, yCenter = currentY = y);
187     }
188
189     /**
190      * Implements {@link
191      * PathHandler#curvetoCubicRel(float,float,float,float,float,float)}.
192      */

193     public void curvetoCubicRel(float x1, float y1,
194                                 float x2, float y2,
195                                 float x, float y) throws ParseException {
196         path.curveTo(currentX + x1, currentY + y1,
197                      xCenter = currentX + x2, yCenter = currentY + y2,
198                      currentX += x, currentY += y);
199     }
200
201     /**
202      * Implements {@link
203      * PathHandler#curvetoCubicAbs(float,float,float,float,float,float)}.
204      */

205     public void curvetoCubicAbs(float x1, float y1,
206                                 float x2, float y2,
207                                 float x, float y) throws ParseException {
208         path.curveTo(x1, y1, xCenter = x2, yCenter = y2, currentX = x,
209                      currentY = y);
210     }
211
212     /**
213      * Implements
214      * {@link PathHandler#curvetoCubicSmoothRel(float,float,float,float)}.
215      */

216     public void curvetoCubicSmoothRel(float x2, float y2,
217                                       float x, float y) throws ParseException {
218         path.curveTo(currentX * 2 - xCenter,
219                      currentY * 2 - yCenter,
220                      xCenter = currentX + x2,
221                      yCenter = currentY + y2,
222                      currentX += x,
223                      currentY += y);
224     }
225
226     /**
227      * Implements
228      * {@link PathHandler#curvetoCubicSmoothAbs(float,float,float,float)}.
229      */

230     public void curvetoCubicSmoothAbs(float x2, float y2,
231                                       float x, float y) throws ParseException {
232         path.curveTo(currentX * 2 - xCenter,
233                      currentY * 2 - yCenter,
234                      xCenter = x2,
235                      yCenter = y2,
236                      currentX = x,
237                      currentY = y);
238     }
239
240     /**
241      * Implements
242      * {@link PathHandler#curvetoQuadraticRel(float,float,float,float)}.
243      */

244     public void curvetoQuadraticRel(float x1, float y1,
245                                     float x, float y) throws ParseException {
246         path.quadTo(xCenter = currentX + x1, yCenter = currentY + y1,
247                     currentX += x, currentY += y);
248     }
249
250     /**
251      * Implements
252      * {@link PathHandler#curvetoQuadraticAbs(float,float,float,float)}.
253      */

254     public void curvetoQuadraticAbs(float x1, float y1,
255                                     float x, float y) throws ParseException {
256         path.quadTo(xCenter = x1, yCenter = y1, currentX = x, currentY = y);
257     }
258
259     /**
260      * Implements {@link PathHandler#curvetoQuadraticSmoothRel(float,float)}.
261      */

262     public void curvetoQuadraticSmoothRel(float x, float y)
263         throws ParseException {
264         path.quadTo(xCenter = currentX * 2 - xCenter,
265                     yCenter = currentY * 2 - yCenter,
266                     currentX += x,
267                     currentY += y);
268     }
269
270     /**
271      * Implements {@link PathHandler#curvetoQuadraticSmoothAbs(float,float)}.
272      */

273     public void curvetoQuadraticSmoothAbs(float x, float y)
274         throws ParseException {
275         path.quadTo(xCenter = currentX * 2 - xCenter,
276                     yCenter = currentY * 2 - yCenter,
277                     currentX = x,
278                     currentY = y);
279     }
280
281     /**
282      * Implements {@link
283      * PathHandler#arcRel(float,float,float,boolean,boolean,float,float)}.
284      */

285     public void arcRel(float rx, float ry,
286                        float xAxisRotation,
287                        boolean largeArcFlag, boolean sweepFlag,
288                        float x, float y) throws ParseException {
289         path.arcTo(rx, ry, xAxisRotation, largeArcFlag, sweepFlag,
290                    xCenter = currentX += x, yCenter = currentY += y);
291     }
292
293     /**
294      * Implements {@link
295      * PathHandler#arcAbs(float,float,float,boolean,boolean,float,float)}.
296      */

297     public void arcAbs(float rx, float ry,
298                        float xAxisRotation,
299                        boolean largeArcFlag, boolean sweepFlag,
300                        float x, float y) throws ParseException {
301         path.arcTo(rx, ry, xAxisRotation, largeArcFlag, sweepFlag,
302                    xCenter = currentX = x, yCenter = currentY = y);
303     }
304 }
305
Popular Tags