Source for java.awt.image.renderable.RenderableImageOp

   1: /* RenderableImageOp.java -- 
   2:    Copyright (C) 2002 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt.image.renderable;
  40: 
  41: import java.awt.RenderingHints;
  42: import java.awt.geom.AffineTransform;
  43: import java.awt.image.RenderedImage;
  44: import java.util.Vector;
  45: 
  46: public class RenderableImageOp implements RenderableImage
  47: {
  48:   private final ContextualRenderedImageFactory crif;
  49:   private ParameterBlock block;
  50: 
  51:   public RenderableImageOp(ContextualRenderedImageFactory crif,
  52:                            ParameterBlock block)
  53:   {
  54:     this.crif = crif;
  55:     this.block = (ParameterBlock) block.clone();
  56:   }
  57: 
  58:   public Vector<RenderableImage> getSources()
  59:   {
  60:     if (block.sources == null)
  61:       return null;
  62:     int size = block.sources.size();
  63:     Vector v = new Vector();
  64:     for (int i = 0; i < size; i++)
  65:       {
  66:         Object o = block.sources.get(i);
  67:         if (o instanceof RenderableImage)
  68:           v.add(o);
  69:       }
  70:     return v;
  71:   }
  72: 
  73:   public Object getProperty(String name)
  74:   {
  75:     return crif.getProperty(block, name);
  76:   }
  77: 
  78:   public String[] getPropertyNames()
  79:   {
  80:     return crif.getPropertyNames();
  81:   }
  82: 
  83:   public boolean isDynamic()
  84:   {
  85:     return crif.isDynamic();
  86:   }
  87: 
  88:   public float getWidth()
  89:   {
  90:     return (float) crif.getBounds2D(block).getWidth();
  91:   }
  92: 
  93:   public float getHeight()
  94:   {
  95:     return (float) crif.getBounds2D(block).getHeight();
  96:   }
  97: 
  98:   public float getMinX()
  99:   {
 100:     return (float) crif.getBounds2D(block).getX();
 101:   }
 102: 
 103:   public float getMinY()
 104:   {
 105:     return (float) crif.getBounds2D(block).getY();
 106:   }
 107: 
 108:   public ParameterBlock setParameterBlock(ParameterBlock block)
 109:   {
 110:     ParameterBlock result = this.block;
 111:     this.block = (ParameterBlock) block.clone();
 112:     return result;
 113:   }
 114: 
 115:   public ParameterBlock getParameterBlock()
 116:   {
 117:     return block;
 118:   }
 119: 
 120:   public RenderedImage createScaledRendering(int w, int h,
 121:                                              RenderingHints hints)
 122:   {
 123:     if (w == 0)
 124:       if (h == 0)
 125:         throw new IllegalArgumentException();
 126:       else
 127:         w = Math.round(h * getWidth() / getHeight());
 128:     if (h == 0)
 129:       h = Math.round(w * getHeight() / getWidth());
 130:     AffineTransform xform = AffineTransform.getScaleInstance(w * getWidth(),
 131:                                                              h * getHeight());
 132:     return createRendering(new RenderContext(xform, hints));
 133:   }
 134: 
 135:   public RenderedImage createDefaultRendering()
 136:   {
 137:     return createRendering(new RenderContext(new AffineTransform()));
 138:   }
 139: 
 140:   public RenderedImage createRendering(RenderContext context)
 141:   {
 142:     ParameterBlock copy = (ParameterBlock) block.clone();
 143:     int i = block.sources.size();
 144:     while (--i >= 0)
 145:       {
 146:         Object o = block.sources.get(i);
 147:         if (o instanceof RenderableImage)
 148:           {
 149:             RenderableImage ri = (RenderableImage) o;
 150:             RenderContext rc = crif.mapRenderContext(i, context, block, ri);
 151:             copy.sources.set(i, ri.createRendering(rc));
 152:           }
 153:       }
 154:     // Now copy.sources should be only RenderedImages.
 155:     return crif.create(context, copy);
 156:   }
 157: } // class RenderableImageOp