001: /*
002: * Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.awt.Component;
029: import java.awt.Graphics;
030: import java.awt.Rectangle;
031: import java.awt.event.PaintEvent;
032:
033: /**
034: * The <code>RepaintArea</code> is a geometric construct created for the
035: * purpose of holding the geometry of several coalesced paint events.
036: * This geometry is accessed synchronously, although it is written such
037: * that painting may still be executed asynchronously.
038: *
039: * @author Eric Hawkes
040: * @version 1.31 06/05/07
041: * @since 1.3
042: */
043: public class RepaintArea {
044:
045: /**
046: * Maximum ratio of bounding rectangle to benefit for which
047: * both the vertical and horizontal unions are repainted.
048: * For smaller ratios the whole bounding rectangle is repainted.
049: * @see #paint
050: */
051: private static final int MAX_BENEFIT_RATIO = 4;
052:
053: private static final int HORIZONTAL = 0;
054: private static final int VERTICAL = 1;
055: private static final int UPDATE = 2;
056:
057: private static final int RECT_COUNT = UPDATE + 1;
058:
059: private Rectangle paintRects[] = new Rectangle[RECT_COUNT];
060:
061: /**
062: * Constructs a new <code>RepaintArea</code>
063: * @since 1.3
064: */
065: public RepaintArea() {
066: }
067:
068: /**
069: * Constructs a new <code>RepaintArea</code> initialized to match
070: * the values of the specified RepaintArea.
071: *
072: * @param ra the <code>RepaintArea</code> from which to copy initial
073: * values to a newly constructed RepaintArea
074: * @since 1.3
075: */
076: private RepaintArea(RepaintArea ra) {
077: // This constructor is private because it should only be called
078: // from the cloneAndReset method
079: for (int i = 0; i < RECT_COUNT; i++) {
080: paintRects[i] = ra.paintRects[i];
081: }
082: }
083:
084: /**
085: * Adds a <code>Rectangle</code> to this <code>RepaintArea</code>.
086: * PAINT Rectangles are divided into mostly vertical and mostly horizontal.
087: * Each group is unioned together.
088: * UPDATE Rectangles are unioned.
089: *
090: * @param r the specified <code>Rectangle</code>
091: * @param id possible values PaintEvent.UPDATE or PaintEvent.PAINT
092: * @since 1.3
093: */
094: public synchronized void add(Rectangle r, int id) {
095: // Make sure this new rectangle has positive dimensions
096: if (r.isEmpty()) {
097: return;
098: }
099: int addTo = UPDATE;
100: if (id == PaintEvent.PAINT) {
101: addTo = (r.width > r.height) ? HORIZONTAL : VERTICAL;
102: }
103: if (paintRects[addTo] != null) {
104: paintRects[addTo].add(r);
105: } else {
106: paintRects[addTo] = new Rectangle(r);
107: }
108: }
109:
110: /**
111: * Creates a new <code>RepaintArea</code> with the same geometry as this
112: * RepaintArea, then removes all of the geometry from this
113: * RepaintArea and restores it to an empty RepaintArea.
114: *
115: * @return ra a new <code>RepaintArea</code> having the same geometry as
116: * this RepaintArea.
117: * @since 1.3
118: */
119: private synchronized RepaintArea cloneAndReset() {
120: RepaintArea ra = new RepaintArea(this );
121: for (int i = 0; i < RECT_COUNT; i++) {
122: paintRects[i] = null;
123: }
124: return ra;
125: }
126:
127: public boolean isEmpty() {
128: for (int i = 0; i < RECT_COUNT; i++) {
129: if (paintRects[i] != null) {
130: return false;
131: }
132: }
133: return true;
134: }
135:
136: /**
137: * Constrains the size of the repaint area to the passed in bounds.
138: */
139: public synchronized void constrain(int x, int y, int w, int h) {
140: for (int i = 0; i < RECT_COUNT; i++) {
141: Rectangle rect = paintRects[i];
142: if (rect != null) {
143: if (rect.x < x) {
144: rect.width -= (x - rect.x);
145: rect.x = x;
146: }
147: if (rect.y < y) {
148: rect.height -= (y - rect.y);
149: rect.y = y;
150: }
151: int xDelta = rect.x + rect.width - x - w;
152: if (xDelta > 0) {
153: rect.width -= xDelta;
154: }
155: int yDelta = rect.y + rect.height - y - h;
156: if (yDelta > 0) {
157: rect.height -= yDelta;
158: }
159: if (rect.width <= 0 || rect.height <= 0) {
160: paintRects[i] = null;
161: }
162: }
163: }
164: }
165:
166: /**
167: * Marks the passed in region as not needing to be painted. It's possible
168: * this will do nothing.
169: */
170: public synchronized void subtract(int x, int y, int w, int h) {
171: Rectangle subtract = new Rectangle(x, y, w, h);
172: for (int i = 0; i < RECT_COUNT; i++) {
173: if (subtract(paintRects[i], subtract)) {
174: if (paintRects[i] != null && paintRects[i].isEmpty()) {
175: paintRects[i] = null;
176: }
177: }
178: }
179: }
180:
181: /**
182: * Invokes paint and update on target Component with optimal
183: * rectangular clip region.
184: * If PAINT bounding rectangle is less than
185: * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
186: * painted separately. Otherwise the entire bounding rectangle is painted.
187: *
188: * @param target Component to <code>paint</code> or <code>update</code>
189: * @since 1.4
190: */
191: public void paint(Object target, boolean shouldClearRectBeforePaint) {
192: Component comp = (Component) target;
193:
194: if (isEmpty()) {
195: return;
196: }
197:
198: if (!comp.isVisible()) {
199: return;
200: }
201:
202: RepaintArea ra = this .cloneAndReset();
203:
204: if (!subtract(ra.paintRects[VERTICAL],
205: ra.paintRects[HORIZONTAL])) {
206: subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
207: }
208:
209: if (ra.paintRects[HORIZONTAL] != null
210: && ra.paintRects[VERTICAL] != null) {
211: Rectangle paintRect = ra.paintRects[HORIZONTAL]
212: .union(ra.paintRects[VERTICAL]);
213: int square = paintRect.width * paintRect.height;
214: int benefit = square - ra.paintRects[HORIZONTAL].width
215: * ra.paintRects[HORIZONTAL].height
216: - ra.paintRects[VERTICAL].width
217: * ra.paintRects[VERTICAL].height;
218: // if benefit is comparable with bounding box
219: if (MAX_BENEFIT_RATIO * benefit < square) {
220: ra.paintRects[HORIZONTAL] = paintRect;
221: ra.paintRects[VERTICAL] = null;
222: }
223: }
224: for (int i = 0; i < paintRects.length; i++) {
225: if (ra.paintRects[i] != null && !ra.paintRects[i].isEmpty()) {
226: // Should use separate Graphics for each paint() call,
227: // since paint() can change Graphics state for next call.
228: Graphics g = comp.getGraphics();
229: if (g != null) {
230: try {
231: g.setClip(ra.paintRects[i]);
232: if (i == UPDATE) {
233: updateComponent(comp, g);
234: } else {
235: if (shouldClearRectBeforePaint) {
236: g.clearRect(ra.paintRects[i].x,
237: ra.paintRects[i].y,
238: ra.paintRects[i].width,
239: ra.paintRects[i].height);
240: }
241: paintComponent(comp, g);
242: }
243: } finally {
244: g.dispose();
245: }
246: }
247: }
248: }
249: }
250:
251: /**
252: * Calls <code>Component.update(Graphics)</code> with given Graphics.
253: */
254: protected void updateComponent(Component comp, Graphics g) {
255: if (comp != null) {
256: comp.update(g);
257: }
258: }
259:
260: /**
261: * Calls <code>Component.paint(Graphics)</code> with given Graphics.
262: */
263: protected void paintComponent(Component comp, Graphics g) {
264: if (comp != null) {
265: comp.paint(g);
266: }
267: }
268:
269: /**
270: * Subtracts subtr from rect. If the result is rectangle
271: * changes rect and returns true. Otherwise false.
272: */
273: static boolean subtract(Rectangle rect, Rectangle subtr) {
274: if (rect == null || subtr == null) {
275: return true;
276: }
277: Rectangle common = rect.intersection(subtr);
278: if (common.isEmpty()) {
279: return true;
280: }
281: if (rect.x == common.x && rect.y == common.y) {
282: if (rect.width == common.width) {
283: rect.y += common.height;
284: rect.height -= common.height;
285: return true;
286: } else if (rect.height == common.height) {
287: rect.x += common.width;
288: rect.width -= common.width;
289: return true;
290: }
291: } else if (rect.x + rect.width == common.x + common.width
292: && rect.y + rect.height == common.y + common.height) {
293: if (rect.width == common.width) {
294: rect.height -= common.height;
295: return true;
296: } else if (rect.height == common.height) {
297: rect.width -= common.width;
298: return true;
299: }
300: }
301: return false;
302: }
303:
304: public String toString() {
305: return super .toString() + "[ horizontal=" + paintRects[0]
306: + " vertical=" + paintRects[1] + " update="
307: + paintRects[2] + "]";
308: }
309: }
|