1 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 68 69 public class Graphic extends PdfContentByte implements Element { 70 71 72 public static final String HORIZONTAL_LINE = "HORIZONTAL"; 73 74 75 public static final String BORDER = "BORDER"; 76 77 78 private HashMap attributes; 79 80 82 85 86 public Graphic() { 87 super(null); 88 } 89 90 92 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 115 116 public int type() { 117 return Element.GRAPHIC; 118 } 119 120 125 126 public ArrayList getChunks() { 127 return new ArrayList(); 128 } 129 130 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 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 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 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 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 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 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 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 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 |