1 28 package net.sf.jasperreports.engine; 29 30 import java.awt.Polygon ; 31 import java.awt.Rectangle ; 32 import java.awt.Shape ; 33 import java.awt.geom.Ellipse2D ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 38 45 public class JRPrintImageArea 46 { 47 48 public final static byte SHAPE_DEFAULT = 0; 49 public final static byte SHAPE_RECTANGLE = 1; 50 public final static byte SHAPE_CIRCLE = 2; 51 public final static byte SHAPE_POLYGON = 3; 52 53 public final static String SHAPE_HTML_DEFAULT = "default"; 54 public final static String SHAPE_HTML_RECTANGLE = "rect"; 55 public final static String SHAPE_HTML_CIRCLE = "circle"; 56 public final static String SHAPE_HTML_POLYGON = "poly"; 57 58 private final static Map htmlShapes; 59 60 static 61 { 62 htmlShapes = new HashMap (); 63 htmlShapes.put(SHAPE_HTML_DEFAULT, new Byte (SHAPE_DEFAULT)); 64 htmlShapes.put(SHAPE_HTML_RECTANGLE, new Byte (SHAPE_RECTANGLE)); 65 htmlShapes.put(SHAPE_HTML_CIRCLE, new Byte (SHAPE_CIRCLE)); 66 htmlShapes.put(SHAPE_HTML_POLYGON, new Byte (SHAPE_POLYGON)); 67 } 68 69 70 76 public static byte getShape(String htmlShape) 77 { 78 Byte shape = (Byte ) htmlShapes.get(htmlShape.toLowerCase()); 79 if (shape == null) 80 { 81 throw new JRRuntimeException("Unknown HTML image area shape \"" + htmlShape + "\""); 82 } 83 return shape.byteValue(); 84 } 85 86 87 93 public static String getHtmlShape(byte shape) 94 { 95 String htmlShape; 96 switch (shape) 97 { 98 case SHAPE_DEFAULT: 99 htmlShape = SHAPE_HTML_DEFAULT; 100 break; 101 case SHAPE_RECTANGLE: 102 htmlShape = SHAPE_HTML_RECTANGLE; 103 break; 104 case SHAPE_CIRCLE: 105 htmlShape = SHAPE_HTML_CIRCLE; 106 break; 107 case SHAPE_POLYGON: 108 htmlShape = SHAPE_HTML_POLYGON; 109 break; 110 default: 111 throw new JRRuntimeException("Unknown image area shape " + shape + ""); 112 } 113 return htmlShape; 114 } 115 116 private byte shape = SHAPE_DEFAULT; 117 private int[] coordinates; 118 119 private transient Shape cachedAWTShape; 120 121 124 public JRPrintImageArea() 125 { 126 } 127 128 129 134 public byte getShape() 135 { 136 return shape; 137 } 138 139 140 151 public void setShape(byte shape) 152 { 153 this.shape = shape; 154 } 155 156 157 162 public int[] getCoordinates() 163 { 164 return coordinates; 165 } 166 167 168 173 public void setCoordinates(int[] coordinates) 174 { 175 this.coordinates = coordinates; 176 } 177 178 179 186 public boolean containsPoint(int x, int y) 187 { 188 boolean contains; 189 if (hasAWTShape()) 190 { 191 ensureAWTShape(); 192 contains = cachedAWTShape.contains(x, y); 193 } 194 else 195 { 196 contains = true; 197 } 198 return contains; 199 } 200 201 202 protected void ensureAWTShape() 203 { 204 if (cachedAWTShape == null) 205 { 206 cachedAWTShape = createAWTShape(); 207 } 208 } 209 210 211 protected boolean hasAWTShape() 212 { 213 return shape != SHAPE_DEFAULT; 214 } 215 216 217 protected Shape createAWTShape() 218 { 219 Shape awtShape; 220 switch (shape) 221 { 222 case SHAPE_RECTANGLE: 223 awtShape = createAWTRectangle(); 224 break; 225 case SHAPE_CIRCLE: 226 awtShape = createAWTCircle(); 227 break; 228 case SHAPE_POLYGON: 229 awtShape = createAWTPolygon(); 230 break; 231 default: 232 awtShape = null; 233 break; 234 } 235 return awtShape; 236 } 237 238 239 protected Shape createAWTRectangle() 240 { 241 if (coordinates == null || coordinates.length != 4) 242 { 243 throw new JRRuntimeException("A rectangle must have exactly 4 coordinates"); 244 } 245 246 return new Rectangle ( 247 coordinates[0], 248 coordinates[1], 249 coordinates[2] - coordinates[0], 250 coordinates[3] - coordinates[1]); 251 } 252 253 254 private Shape createAWTCircle() 255 { 256 if (coordinates == null || coordinates.length != 3) 257 { 258 throw new JRRuntimeException("A circle must have exactly 4 coordinates"); 259 } 260 261 return new Ellipse2D.Float (coordinates[0], coordinates[1], coordinates[2], coordinates[2]); 262 } 263 264 265 private Shape createAWTPolygon() 266 { 267 if (coordinates == null || coordinates.length == 0 || coordinates.length % 2 != 0) 268 { 269 throw new JRRuntimeException("A polygon must have an even number of coordinates"); 270 } 271 272 Polygon polygon = new Polygon (); 273 274 int i; 275 for (i = 0; i < coordinates.length - 2; i += 2) 276 { 277 polygon.addPoint(coordinates[i], coordinates[i + 1]); 278 } 279 if (coordinates[i] != coordinates[0] || coordinates[i + 1] != coordinates[1]) 280 { 281 polygon.addPoint(coordinates[i], coordinates[i + 1]); 282 } 283 284 return polygon; 285 } 286 287 } 288 | Popular Tags |