001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.parser;
020:
021: import java.awt.Shape;
022: import java.awt.geom.GeneralPath;
023: import java.io.IOException;
024: import java.io.Reader;
025:
026: /**
027: * This class produces a polyline shape from a reader.
028: *
029: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
030: * @version $Id: AWTPolylineProducer.java 475477 2006-11-15 22:44:28Z cam $
031: */
032: public class AWTPolylineProducer implements PointsHandler,
033: ShapeProducer {
034: /**
035: * The current path.
036: */
037: protected GeneralPath path;
038:
039: /**
040: * Is the current path a new one?
041: */
042: protected boolean newPath;
043:
044: /**
045: * The winding rule to use to construct the path.
046: */
047: protected int windingRule;
048:
049: /**
050: * Utility method for creating an ExtendedGeneralPath.
051: * @param r The reader used to read the path specification.
052: * @param wr The winding rule to use for creating the path.
053: */
054: public static Shape createShape(Reader r, int wr)
055: throws IOException, ParseException {
056: PointsParser p = new PointsParser();
057: AWTPolylineProducer ph = new AWTPolylineProducer();
058:
059: ph.setWindingRule(wr);
060: p.setPointsHandler(ph);
061: p.parse(r);
062:
063: return ph.getShape();
064: }
065:
066: /**
067: * Sets the winding rule used to construct the path.
068: */
069: public void setWindingRule(int i) {
070: windingRule = i;
071: }
072:
073: /**
074: * Returns the current winding rule.
075: */
076: public int getWindingRule() {
077: return windingRule;
078: }
079:
080: /**
081: * Returns the Shape object initialized during the last parsing.
082: * @return the shape or null if this handler has not been used by
083: * a parser.
084: */
085: public Shape getShape() {
086: return path;
087: }
088:
089: /**
090: * Implements {@link PointsHandler#startPoints()}.
091: */
092: public void startPoints() throws ParseException {
093: path = new GeneralPath(windingRule);
094: newPath = true;
095: }
096:
097: /**
098: * Implements {@link PointsHandler#point(float,float)}.
099: */
100: public void point(float x, float y) throws ParseException {
101: if (newPath) {
102: newPath = false;
103: path.moveTo(x, y);
104: } else {
105: path.lineTo(x, y);
106: }
107: }
108:
109: /**
110: * Implements {@link PointsHandler#endPoints()}.
111: */
112: public void endPoints() throws ParseException {
113: }
114: }
|