KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > internal > PolylineShape


1 /*
2  * $Id: PolylineShape.java 2772 2007-05-21 14:06:08Z blowagie $
3  *
4  * Copyright 2007 Bruno Lowagie and Wil
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf.internal;
51
52 import java.awt.Shape JavaDoc;
53 import java.awt.Rectangle JavaDoc;
54 import java.awt.geom.AffineTransform JavaDoc;
55 import java.awt.geom.PathIterator JavaDoc;
56 import java.awt.geom.Line2D JavaDoc;
57 import java.awt.geom.Point2D JavaDoc;
58 import java.awt.geom.Rectangle2D JavaDoc;
59
60 /**
61  * Class that defines a Polyline shape.
62  * This class was originally written by wil - amristar.com.au
63  * and integrated into iText by Bruno.
64  */

65 public class PolylineShape implements Shape JavaDoc {
66     /** All the X-values of the coordinates in the polyline. */
67     protected int[] x;
68     /** All the Y-values of the coordinates in the polyline. */
69     protected int[] y;
70     /** The total number of points. */
71     protected int np;
72
73     /** Creates a PolylineShape. */
74     public PolylineShape(int[] x, int[] y, int nPoints) {
75         // Should copy array (as done in Polygon)
76
this.np = nPoints;
77         // Take a copy.
78
this.x = new int[np];
79         this.y = new int[np];
80         System.arraycopy(x, 0, this.x, 0, np);
81         System.arraycopy(y, 0, this.y, 0, np);
82     }
83
84     /**
85      * Returns the bounding box of this polyline.
86      *
87      * @return a {@link Rectangle2D} that is the high-precision
88      * bounding box of this line.
89      * @see java.awt.Shape#getBounds2D()
90      */

91     public Rectangle2D JavaDoc getBounds2D() {
92         int[] r = rect();
93         return r==null?null:new Rectangle2D.Double JavaDoc(r[0], r[1], r[2], r[3]);
94     }
95     
96     /**
97      * Returns the bounding box of this polyline.
98      * @see java.awt.Shape#getBounds()
99      */

100     public Rectangle JavaDoc getBounds() {
101         return getBounds2D().getBounds();
102     }
103
104     /**
105      * Calculates the origin (X, Y) and the width and height
106      * of a rectangle that contains all the segments of the
107      * polyline.
108      */

109     private int[] rect() {
110          if(np==0)return null;
111         int xMin = x[0], yMin=y[0], xMax=x[0],yMax=y[0];
112
113          for(int i=1;i<np;i++) {
114              if(x[i]<xMin)xMin=x[i];
115              else if(x[i]>xMax)xMax=x[i];
116              if(y[i]<yMin)yMin=y[i];
117              else if(y[i]>yMax)yMax=y[i];
118          }
119
120          return new int[] { xMin, yMin, xMax-xMin, yMax-yMin };
121     }
122
123     /**
124      * A polyline can't contain a point.
125      * @see java.awt.Shape#contains(double, double)
126      */

127     public boolean contains(double x, double y) { return false; }
128     
129     /**
130      * A polyline can't contain a point.
131      * @see java.awt.Shape#contains(java.awt.geom.Point2D)
132      */

133     public boolean contains(Point2D JavaDoc p) { return false; }
134     
135     /**
136      * A polyline can't contain a point.
137      * @see java.awt.Shape#contains(double, double, double, double)
138      */

139     public boolean contains(double x, double y, double w, double h) { return false; }
140     
141     /**
142      * A polyline can't contain a point.
143      * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
144      */

145     public boolean contains(Rectangle2D JavaDoc r) { return false; }
146
147     /**
148      * Checks if one of the lines in the polyline intersects
149      * with a given rectangle.
150      * @see java.awt.Shape#intersects(double, double, double, double)
151      */

152     public boolean intersects(double x, double y, double w, double h) {
153         return intersects(new Rectangle2D.Double JavaDoc(x, y, w, h));
154     }
155
156     /**
157      * Checks if one of the lines in the polyline intersects
158      * with a given rectangle.
159      * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
160      */

161     public boolean intersects(Rectangle2D JavaDoc r) {
162         if(np==0)return false;
163         Line2D JavaDoc line = new Line2D.Double JavaDoc(x[0],y[0],x[0],y[0]);
164         for (int i = 1; i < np; i++) {
165             line.setLine(x[i-1], y[i-1], x[i], y[i]);
166             if(line.intersects(r))return true;
167         }
168         return false;
169     }
170
171     /**
172      * Returns an iteration object that defines the boundary of the polyline.
173      * @param at the specified {@link AffineTransform}
174      * @return a {@link PathIterator} that defines the boundary of this polyline.
175      * @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
176      */

177     public PathIterator JavaDoc getPathIterator(AffineTransform JavaDoc at) {
178         return new PolylineShapeIterator(this, at);
179     }
180
181     /**
182      * There's no difference with getPathIterator(AffineTransform at);
183      * we just need this method to implement the Shape interface.
184      */

185     public PathIterator JavaDoc getPathIterator(AffineTransform JavaDoc at, double flatness) {
186         return new PolylineShapeIterator(this, at);
187     }
188
189 }
190
191
Popular Tags