KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > rendered > FloodRed


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.rendered;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.Paint JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.awt.image.ColorModel JavaDoc;
27 import java.awt.image.Raster JavaDoc;
28 import java.awt.image.SampleModel JavaDoc;
29 import java.awt.image.WritableRaster JavaDoc;
30
31 import org.apache.batik.ext.awt.image.GraphicsUtil;
32
33 /**
34  * This implementation of RenderedImage will generate an infinate
35  * field of a single color. It reports bounds but will in fact render
36  * out to infinity.
37  *
38  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
39  * @version $Id: FloodRed.java,v 1.6 2004/08/18 07:14:08 vhardy Exp $
40  */

41 public class FloodRed extends AbstractRed {
42
43     /**
44      * A single tile that we move around as needed...
45      */

46     private WritableRaster JavaDoc raster;
47
48     /**
49      * Construct a fully transparent black image <tt>bounds</tt> size.
50      * @param bounds the bounds of the image (in fact will respond with
51      * any request).
52      */

53     public FloodRed(Rectangle JavaDoc bounds) {
54         this(bounds, new Color JavaDoc(0, 0, 0, 0));
55     }
56
57     /**
58      * Construct a fully transparent image <tt>bounds</tt> size, will
59      * paint one tile with paint. Thus paint should not be a pattered
60      * paint or gradient but should be a solid color.
61      * @param bounds the bounds of the image (in fact will respond with
62      * any request).
63      */

64     public FloodRed(Rectangle JavaDoc bounds,
65                     Paint JavaDoc paint) {
66         super(); // We _must_ call init...
67

68         ColorModel JavaDoc cm = GraphicsUtil.sRGB_Unpre;
69         
70         int defSz = AbstractTiledRed.getDefaultTileSize();
71
72         int tw = bounds.width;
73         if (tw > defSz) tw = defSz;
74         int th = bounds.height;
75         if (th > defSz) th = defSz;
76
77         // fix my sample model so it makes sense given my size.
78
SampleModel JavaDoc sm = cm.createCompatibleSampleModel(tw, th);
79
80         // Finish initializing our base class...
81
init((CachableRed)null, bounds, cm, sm, 0, 0, null);
82
83         raster = Raster.createWritableRaster(sm, new Point JavaDoc(0, 0));
84         BufferedImage JavaDoc offScreen = new BufferedImage JavaDoc(cm, raster,
85                                                     cm.isAlphaPremultiplied(),
86                                                     null);
87
88         Graphics2D JavaDoc g = GraphicsUtil.createGraphics(offScreen);
89         g.setPaint(paint);
90         g.fillRect(0, 0, bounds.width, bounds.height);
91         g.dispose();
92     }
93
94     public Raster JavaDoc getTile(int x, int y) {
95         // We have a Single raster that we translate where needed
96
// position. So just offest appropriately.
97
int tx = tileGridXOff+x*tileWidth;
98         int ty = tileGridYOff+y*tileHeight;
99         return raster.createTranslatedChild(tx, ty);
100     }
101
102     public WritableRaster JavaDoc copyData(WritableRaster JavaDoc wr) {
103         int tx0 = getXTile(wr.getMinX());
104         int ty0 = getYTile(wr.getMinY());
105         int tx1 = getXTile(wr.getMinX()+wr.getWidth() -1);
106         int ty1 = getYTile(wr.getMinY()+wr.getHeight()-1);
107
108         final boolean is_INT_PACK =
109             GraphicsUtil.is_INT_PACK_Data(getSampleModel(), false);
110
111         for (int y=ty0; y<=ty1; y++)
112             for (int x=tx0; x<=tx1; x++) {
113                 Raster JavaDoc r = getTile(x, y);
114                 if (is_INT_PACK)
115                     GraphicsUtil.copyData_INT_PACK(r, wr);
116                 else
117                     GraphicsUtil.copyData_FALLBACK(r, wr);
118             }
119
120         return wr;
121     }
122 }
123
124
125
126
127
Popular Tags