KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > RepaintManager


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.bridge;
19
20 import java.awt.Rectangle JavaDoc;
21 import java.awt.Shape JavaDoc;
22 import java.awt.geom.AffineTransform JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Collection JavaDoc;
29
30 import org.apache.batik.gvt.renderer.ImageRenderer;
31 import org.apache.batik.ext.awt.geom.RectListManager;
32
33 /**
34  * This class manages the rendering of a GVT tree.
35  *
36  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
37  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
38  * @version $Id: RepaintManager.java,v 1.18 2005/03/27 08:58:30 cam Exp $
39  */

40 public class RepaintManager {
41     final static int COPY_OVERHEAD = 10000;
42     final static int COPY_LINE_OVERHEAD = 10;
43
44     /**
45      * The renderer used to repaint the buffer.
46      */

47     protected ImageRenderer renderer;
48
49     /**
50      * Creates a new repaint manager.
51      */

52     public RepaintManager(ImageRenderer r) {
53         renderer = r;
54     }
55     
56     /**
57      * Updates the rendering buffer.
58      * @param areas The areas of interest in renderer space units.
59      * @return the list of the rectangles to repaint.
60      */

61     public Collection JavaDoc updateRendering(Collection JavaDoc areas)
62         throws InterruptedException JavaDoc {
63         renderer.flush(areas);
64         List JavaDoc rects = new ArrayList JavaDoc(areas.size());
65         AffineTransform JavaDoc at = renderer.getTransform();
66
67         Iterator JavaDoc i = areas.iterator();
68         while (i.hasNext()) {
69             Shape JavaDoc s = (Shape JavaDoc)i.next();
70             s = at.createTransformedShape(s);
71             Rectangle2D JavaDoc r2d = s.getBounds2D();
72             int x0 = (int)Math.floor(r2d.getX());
73             int y0 = (int)Math.floor(r2d.getY());
74             int x1 = (int)Math.ceil(r2d.getX()+r2d.getWidth());
75             int y1 = (int)Math.ceil(r2d.getY()+r2d.getHeight());
76             // This rectangle must be outset one pixel to ensure
77
// it includes the effects of anti-aliasing on object.s
78
Rectangle JavaDoc r = new Rectangle JavaDoc(x0-1, y0-1, x1-x0+3, y1-y0+3);
79                 
80             rects.add(r);
81         }
82         RectListManager devRLM =null;
83         try {
84              devRLM = new RectListManager(rects);
85              devRLM.mergeRects(COPY_OVERHEAD, COPY_LINE_OVERHEAD);
86         } catch(Exception JavaDoc e) {
87             e.printStackTrace();
88         }
89
90         renderer.repaint(devRLM);
91         return devRLM;
92     }
93
94     /**
95      * Sets up the renderer so that it is ready to render for the new
96      * 'context' defined by the user to device transform, double buffering
97      * state, area of interest and width/height.
98      * @param u2d The user to device transform.
99      * @param dbr Whether the double buffering should be used.
100      * @param aoi The area of interest in the renderer space units.
101      * @param width The offscreen buffer width.
102      * @param height The offscreen buffer width.
103      */

104     public void setupRenderer(AffineTransform JavaDoc u2d,
105                               boolean dbr,
106                               Shape JavaDoc aoi,
107                               int width,
108                               int height) {
109         renderer.setTransform(u2d);
110         renderer.setDoubleBuffered(dbr);
111         renderer.updateOffScreen(width, height);
112         renderer.clearOffScreen();
113     }
114
115     /**
116      * Returns the renderer's offscreen, i.e., the current state as rendered
117      * by the associated renderer.
118      */

119     public BufferedImage JavaDoc getOffScreen(){
120         return renderer.getOffScreen();
121     }
122 }
123
Popular Tags