001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import java.awt.Rectangle;
022: import java.awt.Shape;
023: import java.awt.geom.AffineTransform;
024: import java.awt.geom.Rectangle2D;
025: import java.awt.image.BufferedImage;
026: import java.util.ArrayList;
027: import java.util.List;
028: import java.util.Iterator;
029: import java.util.Collection;
030:
031: import org.apache.batik.gvt.renderer.ImageRenderer;
032: import org.apache.batik.ext.awt.geom.RectListManager;
033:
034: /**
035: * This class manages the rendering of a GVT tree.
036: *
037: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
038: * @author <a href="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
039: * @version $Id: RepaintManager.java 478160 2006-11-22 13:35:06Z dvholten $
040: */
041: public class RepaintManager {
042: static final int COPY_OVERHEAD = 10000;
043: static final int COPY_LINE_OVERHEAD = 10;
044:
045: /**
046: * The renderer used to repaint the buffer.
047: */
048: protected ImageRenderer renderer;
049:
050: /**
051: * Creates a new repaint manager.
052: */
053: public RepaintManager(ImageRenderer r) {
054: renderer = r;
055: }
056:
057: /**
058: * Updates the rendering buffer.
059: * @param areas The areas of interest in renderer space units.
060: * @return the list of the rectangles to repaint.
061: */
062: public Collection updateRendering(Collection areas)
063: throws InterruptedException {
064: renderer.flush(areas);
065: List rects = new ArrayList(areas.size());
066: AffineTransform at = renderer.getTransform();
067:
068: Iterator i = areas.iterator();
069: while (i.hasNext()) {
070: Shape s = (Shape) i.next();
071: s = at.createTransformedShape(s);
072: Rectangle2D r2d = s.getBounds2D();
073: int x0 = (int) Math.floor(r2d.getX());
074: int y0 = (int) Math.floor(r2d.getY());
075: int x1 = (int) Math.ceil(r2d.getX() + r2d.getWidth());
076: int y1 = (int) Math.ceil(r2d.getY() + r2d.getHeight());
077: // This rectangle must be outset one pixel to ensure
078: // it includes the effects of anti-aliasing on objects.
079: Rectangle r = new Rectangle(x0 - 1, y0 - 1, x1 - x0 + 3, y1
080: - y0 + 3);
081:
082: rects.add(r);
083: }
084: RectListManager devRLM = null;
085: try {
086: devRLM = new RectListManager(rects);
087: devRLM.mergeRects(COPY_OVERHEAD, COPY_LINE_OVERHEAD);
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091:
092: renderer.repaint(devRLM);
093: return devRLM;
094: }
095:
096: /**
097: * Sets up the renderer so that it is ready to render for the new
098: * 'context' defined by the user to device transform, double buffering
099: * state, area of interest and width/height.
100: * @param u2d The user to device transform.
101: * @param dbr Whether the double buffering should be used.
102: * @param aoi The area of interest in the renderer space units.
103: * @param width The offscreen buffer width.
104: * @param height The offscreen buffer width.
105: */
106: public void setupRenderer(AffineTransform u2d, boolean dbr,
107: Shape aoi, int width, int height) {
108: renderer.setTransform(u2d);
109: renderer.setDoubleBuffered(dbr);
110: renderer.updateOffScreen(width, height);
111: renderer.clearOffScreen();
112: }
113:
114: /**
115: * Returns the renderer's offscreen, i.e., the current state as rendered
116: * by the associated renderer.
117: */
118: public BufferedImage getOffScreen() {
119: return renderer.getOffScreen();
120: }
121: }
|