1 18 package org.apache.batik.gvt; 19 20 import java.awt.Graphics2D ; 21 import java.awt.Shape ; 22 import java.awt.geom.Area ; 23 import java.awt.geom.Point2D ; 24 import java.awt.geom.Rectangle2D ; 25 26 32 public class CompositeShapePainter implements ShapePainter { 33 34 37 protected Shape shape; 38 39 42 protected ShapePainter [] painters; 43 44 47 protected int count; 48 49 52 public CompositeShapePainter(Shape shape) { 53 if (shape == null) { 54 throw new IllegalArgumentException (); 55 } 56 this.shape = shape; 57 } 58 59 64 public void addShapePainter(ShapePainter shapePainter) { 65 if (shapePainter == null) { 66 return; 67 } 68 if (this.shape != shapePainter.getShape()) { 69 shapePainter.setShape(shape); 70 } 71 if (painters == null) { 72 painters = new ShapePainter[2]; 73 } 74 if (count == painters.length) { 75 ShapePainter [] newPainters = new ShapePainter[(count*3)/2 + 1]; 76 System.arraycopy(painters, 0, newPainters, 0, count); 77 painters = newPainters; 78 } 79 painters[count++] = shapePainter; 80 } 81 82 88 100 101 106 public ShapePainter getShapePainter(int index) { 107 return painters[index]; 108 } 109 110 113 public int getShapePainterCount() { 114 return count; 115 } 116 117 122 public void paint(Graphics2D g2d) { 123 if (painters != null) { 124 for (int i=0; i < count; ++i) { 125 painters[i].paint(g2d); 126 } 127 } 128 } 129 130 133 public Shape getPaintedArea(){ 134 if (painters == null) 135 return null; 136 Area paintedArea = new Area (); 137 for (int i=0; i < count; ++i) { 138 Shape s = painters[i].getPaintedArea(); 139 if (s != null) { 140 paintedArea.add(new Area (s)); 141 } 142 } 143 return paintedArea; 144 } 145 146 149 public Rectangle2D getPaintedBounds2D(){ 150 if (painters == null) 151 return null; 152 153 Rectangle2D bounds = null; 154 for (int i=0; i < count; ++i) { 155 Rectangle2D pb = painters[i].getPaintedBounds2D(); 156 if (pb == null) continue; 157 if (bounds == null) bounds = (Rectangle2D )pb.clone(); 158 else bounds.add(pb); 159 } 160 return bounds; 161 } 162 163 166 public boolean inPaintedArea(Point2D pt){ 167 if (painters == null) 168 return false; 169 for (int i=0; i < count; ++i) { 170 if (painters[i].inPaintedArea(pt)) 171 return true; 172 } 173 return false; 174 } 175 176 180 public Shape getSensitiveArea() { 181 if (painters == null) 182 return null; 183 Area paintedArea = new Area (); 184 for (int i=0; i < count; ++i) { 185 Shape s = painters[i].getSensitiveArea(); 186 if (s != null) { 187 paintedArea.add(new Area (s)); 188 } 189 } 190 return paintedArea; 191 } 192 193 196 public Rectangle2D getSensitiveBounds2D() { 197 if (painters == null) 198 return null; 199 200 Rectangle2D bounds = null; 201 for (int i=0; i < count; ++i) { 202 Rectangle2D pb = painters[i].getSensitiveBounds2D(); 203 if (bounds == null) bounds = (Rectangle2D )pb.clone(); 204 else bounds.add(pb); 205 } 206 return bounds; 207 } 208 209 212 public boolean inSensitiveArea(Point2D pt){ 213 if (painters == null) 214 return false; 215 for (int i=0; i < count; ++i) { 216 if (painters[i].inSensitiveArea(pt)) 217 return true; 218 } 219 return false; 220 } 221 222 228 public void setShape(Shape shape){ 229 if (shape == null) { 230 throw new IllegalArgumentException (); 231 } 232 if (painters != null) { 233 for (int i=0; i < count; ++i) { 234 painters[i].setShape(shape); 235 } 236 } 237 this.shape = shape; 238 } 239 240 245 public Shape getShape(){ 246 return shape; 247 } 248 } 249 | Popular Tags |