KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2000-2001 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.GeneralPath JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24
25 /**
26  * This class produces a polyline shape from a reader.
27  *
28  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
29  * @version $Id: AWTPolylineProducer.java,v 1.4 2004/08/18 07:14:45 vhardy Exp $
30  */

31 public class AWTPolylineProducer implements PointsHandler, ShapeProducer {
32     /**
33      * The current path.
34      */

35     protected GeneralPath JavaDoc path;
36
37     /**
38      * Is the current path a new one?
39      */

40     protected boolean newPath;
41
42     /**
43      * The winding rule to use to construct the path.
44      */

45     protected int windingRule;
46
47     /**
48      * Utility method for creating an ExtendedGeneralPath.
49      * @param r The reader used to read the path specification.
50      * @param wr The winding rule to use for creating the path.
51      */

52     public static Shape JavaDoc createShape(Reader JavaDoc r, int wr)
53         throws IOException JavaDoc,
54                ParseException {
55         PointsParser p = new PointsParser();
56         AWTPolylineProducer ph = new AWTPolylineProducer();
57
58         ph.setWindingRule(wr);
59         p.setPointsHandler(ph);
60         p.parse(r);
61
62         return ph.getShape();
63     }
64
65     /**
66      * Sets the winding rule used to construct the path.
67      */

68     public void setWindingRule(int i) {
69         windingRule = i;
70     }
71
72     /**
73      * Returns the current winding rule.
74      */

75     public int getWindingRule() {
76         return windingRule;
77     }
78
79     /**
80      * Returns the Shape object initialized during the last parsing.
81      * @return the shape or null if this handler has not been used by
82      * a parser.
83      */

84     public Shape JavaDoc getShape() {
85         return path;
86     }
87
88     /**
89      * Implements {@link PointsHandler#startPoints()}.
90      */

91     public void startPoints() throws ParseException {
92         path = new GeneralPath JavaDoc(windingRule);
93         newPath = true;
94     }
95
96     /**
97      * Implements {@link PointsHandler#point(float,float)}.
98      */

99     public void point(float x, float y) throws ParseException {
100         if (newPath) {
101             newPath = false;
102             path.moveTo(x, y);
103         } else {
104             path.lineTo(x, y);
105         }
106     }
107
108     /**
109      * Implements {@link PointsHandler#endPoints()}.
110      */

111     public void endPoints() throws ParseException {
112     }
113 }
114
Popular Tags