KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2001 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.image.Raster JavaDoc;
21
22 import org.apache.batik.util.HaltingThread;
23
24 /**
25  * This is a Grid based implementation of the TileStore.
26  * This makes it pretty quick, but it can use a fair amount of
27  * memory for large tile grids.
28  */

29
30 public class TileGrid implements TileStore {
31     private static final boolean DEBUG = false;
32     private static final boolean COUNT = false;
33
34     private int xSz, ySz;
35     private int minTileX, minTileY;
36     private TileLRUMember [][] rasters=null;
37     private TileGenerator source = null;
38     private LRUCache cache = null;
39
40     public TileGrid(int minTileX, int minTileY,
41                     int xSz, int ySz,
42                     TileGenerator source,
43                     LRUCache cache) {
44         this.cache = cache;
45         this.source = source;
46         this.minTileX = minTileX;
47         this.minTileY = minTileY;
48         this.xSz = xSz;
49         this.ySz = ySz;
50
51         rasters = new TileLRUMember[ySz][];
52     }
53
54     public void setTile(int x, int y, Raster JavaDoc ras) {
55         x-= minTileX;
56         y-= minTileY;
57         if ((x<0) || (x>=xSz)) return;
58         if ((y<0) || (y>=ySz)) return;
59
60         TileLRUMember [] row = rasters[y];
61         TileLRUMember item;
62         if (ras == null) {
63             // Clearing entry.
64
if (row == null) return;
65             item = row[x];
66             if (item == null) return;
67
68             row[x] = null;
69             cache.remove(item);
70             return;
71         }
72         
73         if (row != null) {
74             item = row[x];
75             if (item == null) {
76                 item = new TileLRUMember();
77                 row[x] = item;
78             }
79         } else {
80             row = new TileLRUMember[xSz];
81             item = new TileLRUMember();
82             row[x] = item;
83             rasters[y] = row;
84         }
85         item.setRaster(ras);
86         
87         cache.add(item);
88
89         if (DEBUG) System.out.println("Setting: (" + (x+minTileX) + ", " +
90                                       (y+minTileY) + ")");
91     }
92
93     // Returns Raster if the tile is _currently_ in the cache.
94
// If it is not currently in the cache it returns null.
95
public Raster JavaDoc getTileNoCompute(int x, int y) {
96         x-=minTileX;
97         y-=minTileY;
98         if ((x<0) || (x>=xSz)) return null;
99         if ((y<0) || (y>=ySz)) return null;
100
101         TileLRUMember [] row = rasters[y];
102         if (row == null)
103             return null;
104         TileLRUMember item = row[x];
105         if (item == null)
106             return null;
107         Raster JavaDoc ret = item.retrieveRaster();
108         if (ret != null)
109             cache.add(item);
110         return ret;
111     }
112
113     public Raster JavaDoc getTile(int x, int y) {
114         x-=minTileX;
115         y-=minTileY;
116         if ((x<0) || (x>=xSz)) return null;
117         if ((y<0) || (y>=ySz)) return null;
118
119         if (DEBUG) System.out.println("Fetching: (" + (x+minTileX) + ", " +
120                                       (y+minTileY) + ")");
121         if (COUNT) synchronized (TileGrid.class) { requests++; }
122
123         Raster JavaDoc ras = null;
124         TileLRUMember [] row = rasters[y];
125         TileLRUMember item = null;
126         if (row != null) {
127             item = row[x];
128             if (item != null)
129                 ras = item.retrieveRaster();
130             else {
131                 item = new TileLRUMember();
132                 row[x] = item;
133             }
134         } else {
135             row = new TileLRUMember[xSz];
136             rasters[y] = row;
137             item = new TileLRUMember();
138             row[x] = item;
139         }
140
141         if (ras == null) {
142             if (DEBUG) System.out.println("Generating: ("+(x+minTileX)+", "+
143                                           (y+minTileY) + ")");
144             if (COUNT) synchronized (TileGrid.class) { misses++; }
145             ras = source.genTile(x+minTileX, y+minTileY);
146
147             // In all likelyhood the contents of this tile is junk!
148
// So don't cache it (returning is probably fine since it
149
// won't come back to haunt us...
150
if (HaltingThread.hasBeenHalted())
151                 return ras;
152
153             item.setRaster(ras);
154         }
155
156         // Update the item's position in the cache..
157
cache.add(item);
158
159         return ras;
160     }
161
162     static int requests;
163     static int misses;
164 }
165
Popular Tags