001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor V. Stolyarov
019: * @version $Revision$
020: */package java.awt.image.renderable;
021:
022: import java.awt.RenderingHints;
023: import java.awt.geom.AffineTransform;
024: import java.awt.geom.Rectangle2D;
025: import java.awt.image.RenderedImage;
026: import java.util.Vector;
027:
028: import org.apache.harmony.awt.internal.nls.Messages;
029:
030: public class RenderableImageOp implements RenderableImage {
031:
032: ContextualRenderedImageFactory CRIF;
033: ParameterBlock paramBlock;
034: float minX, minY, width, height;
035:
036: public RenderableImageOp(ContextualRenderedImageFactory CRIF,
037: ParameterBlock paramBlock) {
038: this .CRIF = CRIF;
039: this .paramBlock = (ParameterBlock) paramBlock.clone();
040: Rectangle2D r = CRIF.getBounds2D(paramBlock);
041: minX = (float) r.getMinX();
042: minY = (float) r.getMinY();
043: width = (float) r.getWidth();
044: height = (float) r.getHeight();
045: }
046:
047: public Object getProperty(String name) {
048: return CRIF.getProperty(paramBlock, name);
049: }
050:
051: public ParameterBlock setParameterBlock(ParameterBlock paramBlock) {
052: ParameterBlock oldParam = this .paramBlock;
053: this .paramBlock = (ParameterBlock) paramBlock.clone();
054: return oldParam;
055: }
056:
057: public RenderedImage createRendering(RenderContext renderContext) {
058:
059: Vector<RenderableImage> sources = getSources();
060: ParameterBlock rdParam = (ParameterBlock) paramBlock.clone();
061:
062: if (sources != null) {
063: Vector<Object> rdSources = new Vector<Object>();
064: int i = 0;
065: while (i < sources.size()) {
066: RenderContext newContext = CRIF.mapRenderContext(i,
067: renderContext, paramBlock, this );
068: RenderedImage rdim = sources.elementAt(i)
069: .createRendering(newContext);
070:
071: if (rdim != null) {
072: rdSources.addElement(rdim);
073: }
074: i++;
075: }
076: if (rdSources.size() > 0) {
077: rdParam.setSources(rdSources);
078: }
079: }
080: return CRIF.create(renderContext, rdParam);
081: }
082:
083: public RenderedImage createScaledRendering(int w, int h,
084: RenderingHints hints) {
085: if (w == 0 && h == 0) {
086: // awt.60=Width and Height mustn't be equal zero both
087: throw new IllegalArgumentException(Messages
088: .getString("awt.60")); //$NON-NLS-1$
089: }
090: if (w == 0) {
091: w = Math.round(h * (getWidth() / getHeight()));
092: }
093:
094: if (h == 0) {
095: h = Math.round(w * (getHeight() / getWidth()));
096: }
097:
098: double sx = (double) w / getWidth();
099: double sy = (double) h / getHeight();
100:
101: AffineTransform at = AffineTransform.getScaleInstance(sx, sy);
102: RenderContext context = new RenderContext(at, hints);
103: return createRendering(context);
104: }
105:
106: public Vector<RenderableImage> getSources() {
107: if (paramBlock.getNumSources() == 0) {
108: return null;
109: }
110: Vector<RenderableImage> v = new Vector<RenderableImage>();
111: int i = 0;
112: while (i < paramBlock.getNumSources()) {
113: Object o = paramBlock.getSource(i);
114: if (o instanceof RenderableImage) {
115: v.addElement((RenderableImage) o);
116: }
117: i++;
118: }
119: return v;
120: }
121:
122: public String[] getPropertyNames() {
123: return CRIF.getPropertyNames();
124: }
125:
126: public ParameterBlock getParameterBlock() {
127: return paramBlock;
128: }
129:
130: public RenderedImage createDefaultRendering() {
131: AffineTransform at = new AffineTransform();
132: RenderContext context = new RenderContext(at);
133: return createRendering(context);
134: }
135:
136: public boolean isDynamic() {
137: return CRIF.isDynamic();
138: }
139:
140: public float getWidth() {
141: return width;
142: }
143:
144: public float getMinY() {
145: return minY;
146: }
147:
148: public float getMinX() {
149: return minX;
150: }
151:
152: public float getHeight() {
153: return height;
154: }
155:
156: }
|