KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > renderable > AffineRable8Bit


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.image.renderable;
19
20 import java.awt.Graphics2D JavaDoc;
21 import java.awt.RenderingHints JavaDoc;
22 import java.awt.Shape JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.awt.geom.NoninvertibleTransformException JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.awt.image.RenderedImage JavaDoc;
27 import java.awt.image.renderable.RenderContext JavaDoc;
28
29 import org.apache.batik.ext.awt.image.GraphicsUtil;
30
31 /**
32  * Concrete implementation of the AffineRable interface.
33  * This adjusts the input images coordinate system by a general affine
34  *
35  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
36  * @version $Id: AffineRable8Bit.java,v 1.6 2004/08/18 07:13:58 vhardy Exp $
37  */

38 public class AffineRable8Bit
39     extends AbstractRable
40     implements AffineRable, PaintRable {
41
42     AffineTransform JavaDoc affine;
43     AffineTransform JavaDoc invAffine;
44
45     public AffineRable8Bit(Filter src, AffineTransform JavaDoc affine) {
46         init(src);
47         setAffine(affine);
48     }
49
50     public Rectangle2D JavaDoc getBounds2D() {
51         Filter src = getSource();
52         Rectangle2D JavaDoc r = src.getBounds2D();
53         return affine.createTransformedShape(r).getBounds2D();
54     }
55       /**
56        * Returns the source to be affine.
57        */

58     public Filter getSource() {
59         return (Filter)srcs.get(0);
60     }
61
62     /**
63      * Sets the source to be affine.
64      * @param src image to affine.
65      */

66     public void setSource(Filter src) {
67         init(src);
68     }
69
70       /**
71        * Set the affine transform.
72        * @param affine the new Affine transform to apply.
73        */

74     public void setAffine(AffineTransform JavaDoc affine) {
75         touch();
76         this.affine = affine;
77         try {
78             invAffine = affine.createInverse();
79         } catch (NoninvertibleTransformException JavaDoc e) {
80             invAffine = null;
81         }
82     }
83
84       /**
85        * Get the Affine.
86        * @return the Affine transform currently in effect.
87        */

88     public AffineTransform JavaDoc getAffine() {
89         return (AffineTransform JavaDoc)affine.clone();
90     }
91
92     /**
93      * Should perform the equivilent action as
94      * createRendering followed by drawing the RenderedImage.
95      *
96      * @param g2d The Graphics2D to draw to.
97      * @return true if the paint call succeeded, false if
98      * for some reason the paint failed (in which
99      * case a createRendering should be used).
100      */

101     public boolean paintRable(Graphics2D JavaDoc g2d) {
102         AffineTransform JavaDoc at = g2d.getTransform();
103
104         g2d.transform(getAffine());
105         GraphicsUtil.drawImage(g2d, getSource());
106
107         g2d.setTransform(at);
108
109         return true;
110     }
111
112
113     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
114         // Degenerate Affine no output image..
115
if (invAffine == null) return null;
116
117         // Just copy over the rendering hints.
118
RenderingHints JavaDoc rh = rc.getRenderingHints();
119         if (rh == null) rh = new RenderingHints JavaDoc(null);
120
121         // Map the area of interest to our input...
122
Shape JavaDoc aoi = rc.getAreaOfInterest();
123         if (aoi != null)
124             aoi = invAffine.createTransformedShape(aoi);
125
126         // update the current affine transform
127
AffineTransform JavaDoc at = rc.getTransform();
128         at.concatenate(affine);
129
130         // Return what our input creates (it should factor in our affine).
131
return getSource().createRendering(new RenderContext JavaDoc(at, aoi, rh));
132     }
133
134     public Shape JavaDoc getDependencyRegion(int srcIndex, Rectangle2D JavaDoc outputRgn) {
135         if (srcIndex != 0)
136             throw new IndexOutOfBoundsException JavaDoc("Affine only has one input");
137         if (invAffine == null)
138             return null;
139         return invAffine.createTransformedShape(outputRgn);
140     }
141
142     public Shape JavaDoc getDirtyRegion(int srcIndex, Rectangle2D JavaDoc inputRgn) {
143         if (srcIndex != 0)
144             throw new IndexOutOfBoundsException JavaDoc("Affine only has one input");
145         return affine.createTransformedShape(inputRgn);
146     }
147
148 }
149
Popular Tags