KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.Shape JavaDoc;
24 import java.awt.geom.AffineTransform 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.PadMode;
30 import org.apache.batik.ext.awt.image.rendered.CachableRed;
31 import org.apache.batik.ext.awt.image.rendered.FloodRed;
32 import org.apache.batik.ext.awt.image.rendered.PadRed;
33
34 /**
35  * Concrete implementation of the FloodRable interface.
36  * This fills the input image with a given flood paint
37  *
38  * @author <a HREF="mailto:dean@w3.org">Dean Jackson</a>
39  * @version $Id: FloodRable8Bit.java,v 1.10 2004/08/18 07:13:59 vhardy Exp $
40  */

41
42 public class FloodRable8Bit extends AbstractRable
43     implements FloodRable {
44
45     /**
46      * Paint to use to flood the floodRegion
47      */

48     Paint JavaDoc floodPaint;
49
50     /**
51      * Region to fill with floodPaint
52      */

53     Rectangle2D JavaDoc floodRegion;
54
55     /**
56      * @param floodRegion region to be filled with floodPaint
57      * @param floodPaint paint to use to flood the floodRegion
58      */

59     public FloodRable8Bit(Rectangle2D JavaDoc floodRegion,
60                               Paint JavaDoc floodPaint) {
61         setFloodPaint(floodPaint);
62         setFloodRegion(floodRegion);
63     }
64
65     /**
66      * Set the flood fill paint
67      * @param paint The paint to use when flood filling the input image
68      */

69     public void setFloodPaint(Paint JavaDoc paint) {
70         touch();
71         if (paint == null) {
72             // create a transparent flood fill
73
floodPaint = new Color JavaDoc(0, 0, 0, 0);
74         } else {
75             floodPaint = paint;
76         }
77     }
78
79     /**
80      * Get the flood fill paint.
81      * @return the paint used to flood fill the input image
82      */

83     public Paint JavaDoc getFloodPaint() {
84         // Paint is immutable, we can return it
85
return floodPaint;
86     }
87
88     public Rectangle2D JavaDoc getBounds2D() {
89
90         return (Rectangle2D JavaDoc)floodRegion.clone();
91     }
92
93     /**
94      * Returns the flood region
95      */

96     public Rectangle2D JavaDoc getFloodRegion(){
97         return (Rectangle2D JavaDoc)floodRegion.clone();
98     }
99
100     /**
101      * Sets the flood region
102      */

103     public void setFloodRegion(Rectangle2D JavaDoc floodRegion){
104         if(floodRegion == null){
105             throw new IllegalArgumentException JavaDoc();
106         }
107
108         touch();
109         this.floodRegion = floodRegion;
110     }
111
112     /**
113      * Create a RenderedImage that is filled with the current
114      * flood fill paint
115      * @param rc The current render context
116      * @return A RenderedImage with the flood fill
117      */

118
119     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
120         // Get user space to device space transform
121
AffineTransform JavaDoc usr2dev = rc.getTransform();
122         if (usr2dev == null) {
123             usr2dev = new AffineTransform JavaDoc();
124         }
125
126         Rectangle2D JavaDoc imageRect = getBounds2D();
127
128         // Now, take area of interest into account. It is
129
// defined in user space.
130
Rectangle2D JavaDoc userAOI;
131         Shape JavaDoc aoi = rc.getAreaOfInterest();
132         if (aoi == null) {
133             aoi = imageRect;
134             userAOI = imageRect;
135         } else {
136             userAOI = aoi.getBounds2D();
137
138             // No intersection with the area of interest so return null..
139
if (imageRect.intersects(userAOI) == false)
140                 return null;
141
142             // intersect the filter area and the AOI in user space
143
Rectangle2D.intersect(imageRect, userAOI, userAOI);
144         }
145
146         // The rendered area is the interesection of the
147
// user space renderable area and the user space AOI bounds
148
final Rectangle JavaDoc renderedArea
149             = usr2dev.createTransformedShape(userAOI).getBounds();
150
151         if ((renderedArea.width <= 0) || (renderedArea.height <= 0)) {
152             // If there is no intersection, return null
153
return null;
154         }
155
156         CachableRed cr;
157         cr = new FloodRed(renderedArea, getFloodPaint());
158         // We use a pad because while FloodRed will advertise it's
159
// bounds based on renderedArea it will actually provide the
160
// flood data anywhere.
161
cr = new PadRed(cr, renderedArea, PadMode.ZERO_PAD, null);
162
163         return cr;
164     }
165 }
166
Popular Tags