1 18 package org.apache.batik.ext.awt.image.renderable; 19 20 import java.awt.Rectangle ; 21 import java.awt.RenderingHints ; 22 import java.awt.Shape ; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.geom.Rectangle2D ; 25 import java.awt.image.RenderedImage ; 26 import java.awt.image.renderable.RenderContext ; 27 import java.awt.image.renderable.RenderableImage ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.Vector ; 34 35 import org.apache.batik.ext.awt.image.PadMode; 36 import org.apache.batik.ext.awt.image.rendered.CachableRed; 37 import org.apache.batik.ext.awt.image.rendered.PadRed; 38 import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed; 39 40 49 public abstract class AbstractRable implements Filter { 50 51 protected Vector srcs; 52 protected Map props = new HashMap (); 53 protected long stamp = 0; 54 55 61 protected AbstractRable() { 62 srcs = new Vector (); 63 } 64 65 71 protected AbstractRable(Filter src) { 72 init(src, null); 73 } 74 75 81 protected AbstractRable(Filter src, Map props) { 82 init(src, props); 83 } 84 85 92 protected AbstractRable(List srcs) { 93 this(srcs, null); 94 } 95 96 103 protected AbstractRable(List srcs, Map props) { 104 init(srcs, props); 105 } 106 107 112 public final void touch() { stamp++; } 113 114 120 public long getTimeStamp() { return stamp; } 121 122 128 protected void init(Filter src) { 129 touch(); 130 131 this.srcs = new Vector (1); 132 if (src != null) { 133 this.srcs.add(src); 134 } 135 } 136 137 146 protected void init(Filter src, Map props) { 147 init (src); 148 if(props != null){ 149 this.props.putAll(props); 150 } 151 } 152 153 159 protected void init(List srcs) { 160 touch(); 161 this.srcs = new Vector (srcs); 162 } 163 164 172 protected void init(List srcs, Map props) { 173 init (srcs); 174 if(props != null) 175 this.props.putAll(props); 176 } 177 178 public Rectangle2D getBounds2D() { 179 Rectangle2D bounds = null; 180 if (this.srcs.size() != 0) { 181 Iterator i = srcs.iterator(); 182 Filter src = (Filter)i.next(); 183 bounds = (Rectangle2D )src.getBounds2D().clone(); 184 Rectangle2D r; 185 while (i.hasNext()) { 186 src = (Filter)i.next(); 187 r = src.getBounds2D(); 188 Rectangle2D.union(bounds, r, bounds); 189 } 190 } 191 return bounds; 192 } 193 194 public Vector getSources() { 195 return srcs; 196 } 197 198 public RenderedImage createDefaultRendering() { 199 return createScaledRendering(100, 100, null); 200 } 201 202 public RenderedImage createScaledRendering(int w, int h, 203 RenderingHints hints) { 204 float sX = w/getWidth(); 205 float sY = h/getHeight(); 206 float scale = Math.min(sX, sY); 207 208 AffineTransform at = AffineTransform.getScaleInstance(scale, scale); 209 RenderContext rc = new RenderContext (at, hints); 210 211 float dX = (getWidth()*scale)-w; 212 float dY = (getHeight()*scale)-h; 213 214 RenderedImage ri = createRendering(rc); 215 CachableRed cr = RenderedImageCachableRed.wrap(ri); 216 return new PadRed(cr, new Rectangle ((int)(dX/2), (int)(dY/2), w, h), 217 PadMode.ZERO_PAD, null); 218 } 219 220 public float getMinX() { 221 return (float)getBounds2D().getX(); 222 } 223 public float getMinY() { 224 return (float)getBounds2D().getY(); 225 } 226 public float getWidth() { 227 return (float)getBounds2D().getWidth(); 228 } 229 public float getHeight() { 230 return (float)getBounds2D().getHeight(); 231 } 232 233 public Object getProperty(String name) { 234 Object ret = props.get(name); 235 if (ret != null) return ret; 236 Iterator i = srcs.iterator(); 237 while (i.hasNext()) { 238 RenderableImage ri = (RenderableImage )i.next(); 239 ret = ri.getProperty(name); 240 if (ret != null) return ret; 241 } 242 return null; 243 } 244 245 public String [] getPropertyNames() { 246 Set keys = props.keySet(); 247 Iterator iter = keys.iterator(); 248 String [] ret = new String [keys.size()]; 249 int i=0; 250 while (iter.hasNext()) { 251 ret[i++] = (String )iter.next(); 252 } 253 254 iter = srcs.iterator(); 255 while (iter.hasNext()) { 256 RenderableImage ri = (RenderableImage )iter.next(); 257 String [] srcProps = ri.getPropertyNames(); 258 if (srcProps.length != 0) { 259 String [] tmp = new String [ret.length+srcProps.length]; 260 System.arraycopy(tmp,0,tmp,0,ret.length); 261 System.arraycopy(tmp,ret.length,srcProps,0,srcProps.length); 262 ret = tmp; 263 } 264 } 265 266 return ret; 267 } 268 269 public boolean isDynamic() { return false; } 270 271 public Shape getDependencyRegion(int srcIndex, 272 Rectangle2D outputRgn) { 273 if ((srcIndex < 0) || (srcIndex > srcs.size())) 274 throw new IndexOutOfBoundsException 275 ("Nonexistant source requested."); 276 277 Rectangle2D srect = (Rectangle2D )outputRgn.clone(); 280 Rectangle2D bounds = getBounds2D(); 281 282 if (bounds.intersects(srect) == false) 284 return new Rectangle2D.Float (); 285 286 Rectangle2D.intersect(srect, bounds, srect); 287 return srect; 288 } 289 290 public Shape getDirtyRegion(int srcIndex, 291 Rectangle2D inputRgn) { 292 if ((srcIndex < 0) || (srcIndex > srcs.size())) 293 throw new IndexOutOfBoundsException 294 ("Nonexistant source requested."); 295 296 Rectangle2D drect = (Rectangle2D )inputRgn.clone(); 299 Rectangle2D bounds = getBounds2D(); 300 301 if (bounds.intersects(drect) == false) 303 return new Rectangle2D.Float (); 304 305 Rectangle2D.intersect(drect, bounds, drect); 306 return drect; 307 } 308 309 310 313 } 314 | Popular Tags |