jdk/src/share/classes/sun/awt/RepaintArea.java
author denis
Wed, 27 Mar 2013 16:19:51 +0400
changeset 16705 1caaa379eded
parent 5506 202f599c92aa
permissions -rw-r--r--
7075105: WIN: Provide a way to format HTML on drop Reviewed-by: uta, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.PaintEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The <code>RepaintArea</code> is a geometric construct created for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * purpose of holding the geometry of several coalesced paint events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This geometry is accessed synchronously, although it is written such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * that painting may still be executed asynchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      Eric Hawkes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since       1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class RepaintArea {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Maximum ratio of bounding rectangle to benefit for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * both the vertical and horizontal unions are repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * For smaller ratios the whole bounding rectangle is repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int MAX_BENEFIT_RATIO = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int HORIZONTAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int VERTICAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int UPDATE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final int RECT_COUNT = UPDATE + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private Rectangle paintRects[] = new Rectangle[RECT_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Constructs a new <code>RepaintArea</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public RepaintArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Constructs a new <code>RepaintArea</code> initialized to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the values of the specified RepaintArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param   ra  the <code>RepaintArea</code> from which to copy initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *              values to a newly constructed RepaintArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private RepaintArea(RepaintArea ra) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // This constructor is private because it should only be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // from the cloneAndReset method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (int i = 0; i < RECT_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            paintRects[i] = ra.paintRects[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Adds a <code>Rectangle</code> to this <code>RepaintArea</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * PAINT Rectangles are divided into mostly vertical and mostly horizontal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Each group is unioned together.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * UPDATE Rectangles are unioned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param   r   the specified <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param   id  possible values PaintEvent.UPDATE or PaintEvent.PAINT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public synchronized void add(Rectangle r, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // Make sure this new rectangle has positive dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (r.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int addTo = UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (id == PaintEvent.PAINT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            addTo = (r.width > r.height) ? HORIZONTAL : VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (paintRects[addTo] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            paintRects[addTo].add(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            paintRects[addTo] = new Rectangle(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Creates a new <code>RepaintArea</code> with the same geometry as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * RepaintArea, then removes all of the geometry from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * RepaintArea and restores it to an empty RepaintArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return  ra a new <code>RepaintArea</code> having the same geometry as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          this RepaintArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private synchronized RepaintArea cloneAndReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        RepaintArea ra = new RepaintArea(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < RECT_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            paintRects[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return ra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i = 0; i < RECT_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (paintRects[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Constrains the size of the repaint area to the passed in bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public synchronized void constrain(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i = 0; i < RECT_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Rectangle rect = paintRects[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (rect.x < x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    rect.width -= (x - rect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    rect.x = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (rect.y < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    rect.height -= (y - rect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    rect.y = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                int xDelta = rect.x + rect.width - x - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (xDelta > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    rect.width -= xDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                int yDelta = rect.y + rect.height - y - h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (yDelta > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    rect.height -= yDelta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (rect.width <= 0 || rect.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    paintRects[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Marks the passed in region as not needing to be painted. It's possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * this will do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public synchronized void subtract(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Rectangle subtract = new Rectangle(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        for (int i = 0; i < RECT_COUNT; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (subtract(paintRects[i], subtract)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (paintRects[i] != null && paintRects[i].isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    paintRects[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Invokes paint and update on target Component with optimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * rectangular clip region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * If PAINT bounding rectangle is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * painted separately.  Otherwise the entire bounding rectangle is painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param   target Component to <code>paint</code> or <code>update</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public void paint(Object target, boolean shouldClearRectBeforePaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Component comp = (Component)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (!comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        RepaintArea ra = this.cloneAndReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            int square = paintRect.width * paintRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int benefit = square - ra.paintRects[HORIZONTAL].width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                * ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                * ra.paintRects[VERTICAL].height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // if benefit is comparable with bounding box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (MAX_BENEFIT_RATIO * benefit < square) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                ra.paintRects[HORIZONTAL] = paintRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                ra.paintRects[VERTICAL] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int i = 0; i < paintRects.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (ra.paintRects[i] != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                && !ra.paintRects[i].isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                // Should use separate Graphics for each paint() call,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                // since paint() can change Graphics state for next call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                Graphics g = comp.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (g != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        g.setClip(ra.paintRects[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        if (i == UPDATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            updateComponent(comp, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            if (shouldClearRectBeforePaint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                g.clearRect( ra.paintRects[i].x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                             ra.paintRects[i].y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                             ra.paintRects[i].width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                             ra.paintRects[i].height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                            paintComponent(comp, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Calls <code>Component.update(Graphics)</code> with given Graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    protected void updateComponent(Component comp, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            comp.update(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Calls <code>Component.paint(Graphics)</code> with given Graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    protected void paintComponent(Component comp, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            comp.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Subtracts subtr from rect. If the result is rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * changes rect and returns true. Otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static boolean subtract(Rectangle rect, Rectangle subtr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (rect == null || subtr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Rectangle common = rect.intersection(subtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (common.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (rect.x == common.x && rect.y == common.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (rect.width == common.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                rect.y += common.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                rect.height -= common.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (rect.height == common.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                rect.x += common.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                rect.width -= common.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (rect.x + rect.width == common.x + common.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            && rect.y + rect.height == common.y + common.height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (rect.width == common.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                rect.height -= common.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (rect.height == common.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                rect.width -= common.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return super.toString() + "[ horizontal=" + paintRects[0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            " vertical=" + paintRects[1] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            " update=" + paintRects[2] + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}