KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > Graphic


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

50
51 package com.lowagie.text;
52
53 import java.awt.Color;
54 import java.util.ArrayList;
55 import java.util.Iterator;
56 import java.util.HashMap;
57
58 import com.lowagie.text.pdf.PdfContentByte;
59
60 /**
61  * A <CODE>Graphic</CODE> element can contain several geometric figures (curves, lines,...).
62  * <P>
63  * If you want to use this <CODE>Element</CODE>, please read the Sections 8.4 and 8.5 of
64  * the PDF Reference Manual version 1.3 first.
65  *
66  * @see Element
67  */

68
69 public class Graphic extends PdfContentByte implements Element {
70     
71 /** This is a type of Graphic. */
72     public static final String HORIZONTAL_LINE = "HORIZONTAL";
73     
74 /** This is a type of Graphic. */
75     public static final String BORDER = "BORDER";
76     
77 /** Contains some of the attributes for this Graphic. */
78     private HashMap attributes;
79     
80     // constructor
81

82 /**
83  * Constructs a <CODE>Graphic</CODE>-object.
84  */

85     
86     public Graphic() {
87         super(null);
88     }
89     
90     // implementation of the Element interface
91

92 /**
93  * Processes the element by adding it (or the different parts) to an
94  * <CODE>ElementListener</CODE>.
95  *
96  * @param listener an <CODE>ElementListener</CODE>
97  * <CODE>true</CODE> if the element was processed successfully
98  * @return true if processing this object succeeded
99  */

100     
101     public boolean process(ElementListener listener) {
102         try {
103             return listener.add(this);
104         }
105         catch(DocumentException de) {
106             return false;
107         }
108     }
109     
110 /**
111  * Gets the type of the text element.
112  *
113  * @return a type
114  */

115     
116     public int type() {
117         return Element.GRAPHIC;
118     }
119     
120 /**
121  * Gets all the chunks in this element.
122  *
123  * @return an <CODE>ArrayList</CODE>
124  */

125     
126     public ArrayList getChunks() {
127         return new ArrayList();
128     }
129     
130     /**
131      * Orders this graphic to draw a horizontal, centered line.
132      * @param linewidth the line width
133      * @param percentage the percentage horizontal width in relation to the margins or if negative, an absolute value
134      */

135     
136     public void setHorizontalLine(float linewidth, float percentage) {
137         if (attributes == null) attributes = new HashMap();
138         attributes.put(HORIZONTAL_LINE, new Object[]{new Float(linewidth), new Float(percentage), Color.black, new Integer(Element.ALIGN_CENTER)});
139     }
140     
141     /**
142      * Orders this graphic to draw a horizontal line with some alignment.
143      * @param linewidth the line width
144      * @param percentage the percentage horizontal width in relation to the margins or if negative, an absolute value
145      * @param align the line alignment
146      */

147     public void setHorizontalLine(float linewidth, float percentage, int align) {
148         if (attributes == null) attributes = new HashMap();
149         attributes.put(HORIZONTAL_LINE, new Object[]{new Float(linewidth), new Float(percentage), Color.black, new Integer(align)});
150     }
151     
152     /**
153      * Orders this graphic to draw a horizontal, centered line.
154      * @param linewidth the line width
155      * @param percentage the percentage horizontal width in relation to the margins or if negative, an absolute value
156      * @param color the color of the line
157      */

158     
159     public void setHorizontalLine(float linewidth, float percentage, Color color) {
160         if (attributes == null) attributes = new HashMap();
161         attributes.put(HORIZONTAL_LINE, new Object[]{new Float(linewidth), new Float(percentage), color, new Integer(Element.ALIGN_CENTER)});
162     }
163     
164     /**
165      * Orders this graphic to draw a horizontal, centered line.
166      * @param linewidth the line width
167      * @param percentage the percentage horizontal width in relation to the margins or if negative, an absolute value
168      * @param color the color of the line
169      * @param align the line alignment
170      */

171     public void setHorizontalLine(float linewidth, float percentage, Color color, int align) {
172         if (attributes == null) attributes = new HashMap();
173         attributes.put(HORIZONTAL_LINE, new Object[]{new Float(linewidth), new Float(percentage), color, new Integer(align)});
174     }
175     
176 /**
177  * draws a horizontal line.
178  * @param lineWidth width of the line
179  * @param color color of the line
180  * @param x1 start position of the line
181  * @param x2 end position of the line
182  * @param y y-coordinate of the line
183  */

184     
185     public void drawHorizontalLine(float lineWidth, Color color, float x1, float x2, float y) {
186         setLineWidth(lineWidth);
187         setColorStroke(color);
188         moveTo(x1, y);
189         lineTo(x2, y);
190         stroke();
191         resetRGBColorStroke();
192     }
193     
194 /**
195  * Orders this graphic to draw a horizontal line.
196  * @param linewidth linewidth of the border
197  * @param extraSpace extraspace needed as marging on the page
198  */

199     
200     public void setBorder(float linewidth, float extraSpace) {
201         if (attributes == null) attributes = new HashMap();
202         attributes.put(BORDER, new Object[]{new Float(linewidth), new Float(extraSpace), new Color(0, 0, 0)});
203     }
204     
205 /**
206  * Orders this graphic to draw a horizontal line.
207  * @param linewidth linewidth of the border
208  * @param extraSpace extraspace needed as marging on the page
209  * @param color color of the borderbox
210  */

211     
212     public void setBorder(float linewidth, float extraSpace, Color color) {
213         if (attributes == null) attributes = new HashMap();
214         attributes.put(BORDER, new Object[]{new Float(linewidth), new Float(extraSpace), color});
215     }
216     
217 /**
218  * Draws a border
219  * @param lineWidth linewidth of the border
220  * @param color color of the borderbox
221  * @param llx lower left x coordinate
222  * @param lly lower left y coordinate
223  * @param urx upper right x coordinate
224  * @param ury upper right y coordinate
225  */

226     public void drawBorder(float lineWidth, Color color, float llx, float lly, float urx, float ury) {
227         setLineWidth(lineWidth);
228         setColorStroke(color);
229         rectangle(llx, lly, urx - llx, ury - lly);
230         stroke();
231         resetRGBColorStroke();
232     }
233     
234 /**
235  * Processes the attributes of this object.
236  * @param llx lower left x coordinate
237  * @param lly lower left y coordinate
238  * @param urx upper right x coordinate
239  * @param ury upper right y coordinate
240  * @param y
241  */

242     
243     public void processAttributes(float llx, float lly, float urx, float ury, float y) {
244         if (attributes == null) return;
245         String attribute;
246         Object[] o;
247         for (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) {
248             attribute = (String) i.next();
249             o = (Object[]) attributes.get(attribute);
250             if (HORIZONTAL_LINE.equals(attribute)) {
251                 float p = ((Float)o[1]).floatValue();
252                 float w;
253                 if (p < 0)
254                     w = -p;
255                 else
256                     w = (urx - llx) * p / 100.0f;
257                 int align = ((Integer)o[3]).intValue();
258                 float s;
259                 switch (align) {
260                     case Element.ALIGN_LEFT:
261                         s = 0;
262                         break;
263                     case Element.ALIGN_RIGHT:
264                         s = urx - llx - w;
265                         break;
266                     default:
267                         s = (urx - llx - w) / 2;
268                 }
269                 drawHorizontalLine(((Float)o[0]).floatValue(), (Color)o[2], s + llx, s + w + llx, y);
270             }
271             if (BORDER.equals(attribute)) {
272                 float extra = ((Float)o[1]).floatValue();
273                 drawBorder(((Float)o[0]).floatValue(), (Color)o[2], llx - extra, lly - extra, urx + extra, ury + extra);
274             }
275         }
276     }
277 }
278
Popular Tags