KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > common > PDRectangle


1 /**
2  * Copyright (c) 2003-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.common;
32
33 import org.pdfbox.cos.COSArray;
34 import org.pdfbox.cos.COSBase;
35 import org.pdfbox.cos.COSFloat;
36 import org.pdfbox.cos.COSNumber;
37
38 import org.fontbox.util.BoundingBox;
39
40 import java.awt.Dimension JavaDoc;
41
42 /**
43  * This represents a rectangle in a PDF document.
44  *
45  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
46  * @version $Revision: 1.12 $
47  */

48 public class PDRectangle implements COSObjectable
49 {
50     private COSArray rectArray;
51
52     /**
53      * Constructor.
54      *
55      * Initializes to 0,0,0,0
56      */

57     public PDRectangle()
58     {
59         rectArray = new COSArray();
60         rectArray.add( new COSFloat( 0.0f ) );
61         rectArray.add( new COSFloat( 0.0f ) );
62         rectArray.add( new COSFloat( 0.0f ) );
63         rectArray.add( new COSFloat( 0.0f ) );
64     }
65     
66     /**
67      * Constructor.
68      *
69      * @param width The width of the rectangle.
70      * @param height The height of the rectangle.
71      */

72     public PDRectangle( float width, float height )
73     {
74         rectArray = new COSArray();
75         rectArray.add( new COSFloat( 0.0f ) );
76         rectArray.add( new COSFloat( 0.0f ) );
77         rectArray.add( new COSFloat( width ) );
78         rectArray.add( new COSFloat( height ) );
79     }
80
81     /**
82      * Constructor.
83      *
84      * @param box The non PD bouding box.
85      */

86     public PDRectangle( BoundingBox box )
87     {
88         rectArray = new COSArray();
89         rectArray.add( new COSFloat( box.getLowerLeftX() ) );
90         rectArray.add( new COSFloat( box.getLowerLeftY() ) );
91         rectArray.add( new COSFloat( box.getUpperRightX() ) );
92         rectArray.add( new COSFloat( box.getUpperRightY() ) );
93     }
94
95     /**
96      * Constructor.
97      *
98      * @param array An array of numbers as specified in the PDF Reference for a rectangle type.
99      */

100     public PDRectangle( COSArray array )
101     {
102         rectArray = array;
103     }
104     
105     /**
106      * Method to determine if the x/y point is inside this rectangle.
107      * @param x The x-coordinate to test.
108      * @param y The y-coordinate to test.
109      * @return True if the point is inside this rectangle.
110      */

111     public boolean contains( float x, float y )
112     {
113         float llx = getLowerLeftX();
114         float urx = getUpperRightX();
115         float lly = getLowerLeftY();
116         float ury = getUpperRightY();
117         return x >= llx && x <= urx &&
118                y >= lly && y <= ury;
119     }
120
121     /**
122      * This will create a translated rectangle based off of this rectangle, such
123      * that the new rectangle retains the same dimensions(height/width), but the
124      * lower left x,y values are zero. <br />
125      * 100, 100, 400, 400 (llx, lly, urx, ury ) <br />
126      * will be translated to 0,0,300,300
127      *
128      * @return A new rectangle that has been translated back to the origin.
129      */

130     public PDRectangle createRetranslatedRectangle()
131     {
132         PDRectangle retval = new PDRectangle();
133         retval.setUpperRightX( getWidth() );
134         retval.setUpperRightY( getHeight() );
135         return retval;
136     }
137
138     /**
139      * This will get the underlying array for this rectangle.
140      *
141      * @return The cos array.
142      */

143     public COSArray getCOSArray()
144     {
145         return rectArray;
146     }
147
148     /**
149      * This will get the lower left x coordinate.
150      *
151      * @return The lower left x.
152      */

153     public float getLowerLeftX()
154     {
155         return ((COSNumber)rectArray.get(0)).floatValue();
156     }
157
158     /**
159      * This will set the lower left x coordinate.
160      *
161      * @param value The lower left x.
162      */

163     public void setLowerLeftX(float value)
164     {
165         rectArray.set(0, new COSFloat( value ) );
166     }
167
168     /**
169      * This will get the lower left y coordinate.
170      *
171      * @return The lower left y.
172      */

173     public float getLowerLeftY()
174     {
175         return ((COSNumber)rectArray.get(1)).floatValue();
176     }
177
178     /**
179      * This will set the lower left y coordinate.
180      *
181      * @param value The lower left y.
182      */

183     public void setLowerLeftY(float value)
184     {
185         rectArray.set(1, new COSFloat( value ) );
186     }
187
188     /**
189      * This will get the upper right x coordinate.
190      *
191      * @return The upper right x .
192      */

193     public float getUpperRightX()
194     {
195         return ((COSNumber)rectArray.get(2)).floatValue();
196     }
197
198     /**
199      * This will set the upper right x coordinate.
200      *
201      * @param value The upper right x .
202      */

203     public void setUpperRightX(float value)
204     {
205         rectArray.set(2, new COSFloat( value ) );
206     }
207
208     /**
209      * This will get the upper right y coordinate.
210      *
211      * @return The upper right y.
212      */

213     public float getUpperRightY()
214     {
215         return ((COSNumber)rectArray.get(3)).floatValue();
216     }
217
218     /**
219      * This will set the upper right y coordinate.
220      *
221      * @param value The upper right y.
222      */

223     public void setUpperRightY(float value)
224     {
225         rectArray.set(3, new COSFloat( value ) );
226     }
227
228     /**
229      * This will get the width of this rectangle as calculated by
230      * upperRightX - lowerLeftX.
231      *
232      * @return The width of this rectangle.
233      */

234     public float getWidth()
235     {
236         return getUpperRightX() - getLowerLeftX();
237     }
238
239     /**
240      * This will get the height of this rectangle as calculated by
241      * upperRightY - lowerLeftY.
242      *
243      * @return The height of this rectangle.
244      */

245     public float getHeight()
246     {
247         return getUpperRightY() - getLowerLeftY();
248     }
249
250     /**
251      * A convenience method to create a dimension object for AWT operations.
252      *
253      * @return A dimension that matches the width and height of this rectangle.
254      */

255     public Dimension JavaDoc createDimension()
256     {
257         return new Dimension JavaDoc( (int)getWidth(), (int)getHeight() );
258     }
259     
260     /**
261     * This will move the rectangle the given relative amount.
262     *
263     * @param horizontalAmount positive values will move rectangle to the right, negative's to the left.
264     * @param verticalAmount positive values will move the rectangle up, negative's down.
265     */

266     public void move(float horizontalAmount, float verticalAmount)
267     {
268         setUpperRightX(getUpperRightX() + horizontalAmount);
269         setLowerLeftX(getLowerLeftX() + horizontalAmount);
270         setUpperRightY(getUpperRightY() + verticalAmount);
271         setLowerLeftY(getLowerLeftY() + verticalAmount);
272     }
273     
274     /**
275      * Convert this standard java object to a COS object.
276      *
277      * @return The cos object that matches this Java object.
278      */

279     public COSBase getCOSObject()
280     {
281         return rectArray;
282     }
283
284
285     /**
286      * This will return a string representation of this rectangle.
287      *
288      * @return This object as a string.
289      */

290     public String JavaDoc toString()
291     {
292         return "[" + getLowerLeftX() + "," + getLowerLeftY() + "," +
293                      getUpperRightX() + "," + getUpperRightY() +"]";
294     }
295 }
Popular Tags