< prev index next >

src/java.desktop/share/classes/sun/awt/RepaintArea.java

Print this page




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt;
  27 
  28 import java.awt.Component;
  29 import java.awt.Graphics;
  30 import java.awt.Rectangle;
  31 import java.awt.event.PaintEvent;
  32 
  33 /**
  34  * The <code>RepaintArea</code> is a geometric construct created for the
  35  * purpose of holding the geometry of several coalesced paint events.
  36  * This geometry is accessed synchronously, although it is written such
  37  * that painting may still be executed asynchronously.
  38  *
  39  * @author      Eric Hawkes
  40  * @since       1.3
  41  */
  42 public class RepaintArea {
  43 
  44     /**
  45      * Maximum ratio of bounding rectangle to benefit for which
  46      * both the vertical and horizontal unions are repainted.
  47      * For smaller ratios the whole bounding rectangle is repainted.
  48      * @see #paint
  49      */
  50     private static final int MAX_BENEFIT_RATIO = 4;
  51 
  52     private static final int HORIZONTAL = 0;
  53     private static final int VERTICAL = 1;
  54     private static final int UPDATE = 2;
  55 
  56     private static final int RECT_COUNT = UPDATE + 1;
  57 
  58     private Rectangle paintRects[] = new Rectangle[RECT_COUNT];
  59 
  60 
  61     /**
  62      * Constructs a new <code>RepaintArea</code>
  63      * @since   1.3
  64      */
  65     public RepaintArea() {
  66     }
  67 
  68     /**
  69      * Constructs a new <code>RepaintArea</code> initialized to match
  70      * the values of the specified RepaintArea.
  71      *
  72      * @param   ra  the <code>RepaintArea</code> from which to copy initial
  73      *              values to a newly constructed RepaintArea
  74      * @since   1.3
  75      */
  76     private RepaintArea(RepaintArea ra) {
  77         // This constructor is private because it should only be called
  78         // from the cloneAndReset method
  79         for (int i = 0; i < RECT_COUNT; i++) {
  80             paintRects[i] = ra.paintRects[i];
  81         }
  82     }
  83 
  84     /**
  85      * Adds a <code>Rectangle</code> to this <code>RepaintArea</code>.
  86      * PAINT Rectangles are divided into mostly vertical and mostly horizontal.
  87      * Each group is unioned together.
  88      * UPDATE Rectangles are unioned.
  89      *
  90      * @param   r   the specified <code>Rectangle</code>
  91      * @param   id  possible values PaintEvent.UPDATE or PaintEvent.PAINT
  92      * @since   1.3
  93      */
  94     public synchronized void add(Rectangle r, int id) {
  95         // Make sure this new rectangle has positive dimensions
  96         if (r.isEmpty()) {
  97             return;
  98         }
  99         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     /**
 112      * Creates a new <code>RepaintArea</code> with the same geometry as this
 113      * RepaintArea, then removes all of the geometry from this
 114      * RepaintArea and restores it to an empty RepaintArea.
 115      *
 116      * @return  ra a new <code>RepaintArea</code> having the same geometry as
 117      *          this RepaintArea.
 118      * @since   1.3
 119      */
 120     private synchronized RepaintArea cloneAndReset() {
 121         RepaintArea ra = new RepaintArea(this);
 122         for (int i = 0; i < RECT_COUNT; i++) {
 123             paintRects[i] = null;
 124         }
 125         return ra;
 126     }
 127 
 128     public boolean isEmpty() {
 129         for (int i = 0; i < RECT_COUNT; i++) {
 130             if (paintRects[i] != null) {
 131                 return false;
 132             }
 133         }
 134         return true;
 135     }
 136 


 169      * this will do nothing.
 170      */
 171     public synchronized void subtract(int x, int y, int w, int h) {
 172         Rectangle subtract = new Rectangle(x, y, w, h);
 173         for (int i = 0; i < RECT_COUNT; i++) {
 174             if (subtract(paintRects[i], subtract)) {
 175                 if (paintRects[i] != null && paintRects[i].isEmpty()) {
 176                     paintRects[i] = null;
 177                 }
 178             }
 179         }
 180     }
 181 
 182     /**
 183      * Invokes paint and update on target Component with optimal
 184      * rectangular clip region.
 185      * If PAINT bounding rectangle is less than
 186      * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 187      * painted separately.  Otherwise the entire bounding rectangle is painted.
 188      *
 189      * @param   target Component to <code>paint</code> or <code>update</code>
 190      * @since   1.4
 191      */
 192     public void paint(Object target, boolean shouldClearRectBeforePaint) {
 193         Component comp = (Component)target;
 194 
 195         if (isEmpty()) {
 196             return;
 197         }
 198 
 199         if (!comp.isVisible()) {
 200             return;
 201         }
 202 
 203         RepaintArea ra = this.cloneAndReset();
 204 
 205         if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
 206             subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
 207         }
 208 
 209         if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {


 231                         if (i == UPDATE) {
 232                             updateComponent(comp, g);
 233                         } else {
 234                             if (shouldClearRectBeforePaint) {
 235                                 g.clearRect( ra.paintRects[i].x,
 236                                              ra.paintRects[i].y,
 237                                              ra.paintRects[i].width,
 238                                              ra.paintRects[i].height);
 239                             }
 240                             paintComponent(comp, g);
 241                         }
 242                     } finally {
 243                         g.dispose();
 244                     }
 245                 }
 246             }
 247         }
 248     }
 249 
 250     /**
 251      * Calls <code>Component.update(Graphics)</code> with given Graphics.
 252      */
 253     protected void updateComponent(Component comp, Graphics g) {
 254         if (comp != null) {
 255             comp.update(g);
 256         }
 257     }
 258 
 259     /**
 260      * Calls <code>Component.paint(Graphics)</code> with given Graphics.
 261      */
 262     protected void paintComponent(Component comp, Graphics g) {
 263         if (comp != null) {
 264             comp.paint(g);
 265         }
 266     }
 267 
 268     /**
 269      * Subtracts subtr from rect. If the result is rectangle
 270      * changes rect and returns true. Otherwise false.
 271      */
 272     static boolean subtract(Rectangle rect, Rectangle subtr) {
 273         if (rect == null || subtr == null) {
 274             return true;
 275         }
 276         Rectangle common = rect.intersection(subtr);
 277         if (common.isEmpty()) {
 278             return true;
 279         }
 280         if (rect.x == common.x && rect.y == common.y) {




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt;
  27 
  28 import java.awt.Component;
  29 import java.awt.Graphics;
  30 import java.awt.Rectangle;
  31 import java.awt.event.PaintEvent;
  32 
  33 /**
  34  * The {@code RepaintArea} is a geometric construct created for the
  35  * purpose of holding the geometry of several coalesced paint events.
  36  * This geometry is accessed synchronously, although it is written such
  37  * that painting may still be executed asynchronously.
  38  *
  39  * @author      Eric Hawkes
  40  * @since       1.3
  41  */
  42 public class RepaintArea {
  43 
  44     /**
  45      * Maximum ratio of bounding rectangle to benefit for which
  46      * both the vertical and horizontal unions are repainted.
  47      * For smaller ratios the whole bounding rectangle is repainted.
  48      * @see #paint
  49      */
  50     private static final int MAX_BENEFIT_RATIO = 4;
  51 
  52     private static final int HORIZONTAL = 0;
  53     private static final int VERTICAL = 1;
  54     private static final int UPDATE = 2;
  55 
  56     private static final int RECT_COUNT = UPDATE + 1;
  57 
  58     private Rectangle paintRects[] = new Rectangle[RECT_COUNT];
  59 
  60 
  61     /**
  62      * Constructs a new {@code RepaintArea}
  63      * @since   1.3
  64      */
  65     public RepaintArea() {
  66     }
  67 
  68     /**
  69      * Constructs a new {@code RepaintArea} initialized to match
  70      * the values of the specified RepaintArea.
  71      *
  72      * @param   ra  the {@code RepaintArea} from which to copy initial
  73      *              values to a newly constructed RepaintArea
  74      * @since   1.3
  75      */
  76     private RepaintArea(RepaintArea ra) {
  77         // This constructor is private because it should only be called
  78         // from the cloneAndReset method
  79         for (int i = 0; i < RECT_COUNT; i++) {
  80             paintRects[i] = ra.paintRects[i];
  81         }
  82     }
  83 
  84     /**
  85      * Adds a {@code Rectangle} to this {@code RepaintArea}.
  86      * PAINT Rectangles are divided into mostly vertical and mostly horizontal.
  87      * Each group is unioned together.
  88      * UPDATE Rectangles are unioned.
  89      *
  90      * @param   r   the specified {@code Rectangle}
  91      * @param   id  possible values PaintEvent.UPDATE or PaintEvent.PAINT
  92      * @since   1.3
  93      */
  94     public synchronized void add(Rectangle r, int id) {
  95         // Make sure this new rectangle has positive dimensions
  96         if (r.isEmpty()) {
  97             return;
  98         }
  99         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     /**
 112      * Creates a new {@code RepaintArea} with the same geometry as this
 113      * RepaintArea, then removes all of the geometry from this
 114      * RepaintArea and restores it to an empty RepaintArea.
 115      *
 116      * @return  ra a new {@code RepaintArea} having the same geometry as
 117      *          this RepaintArea.
 118      * @since   1.3
 119      */
 120     private synchronized RepaintArea cloneAndReset() {
 121         RepaintArea ra = new RepaintArea(this);
 122         for (int i = 0; i < RECT_COUNT; i++) {
 123             paintRects[i] = null;
 124         }
 125         return ra;
 126     }
 127 
 128     public boolean isEmpty() {
 129         for (int i = 0; i < RECT_COUNT; i++) {
 130             if (paintRects[i] != null) {
 131                 return false;
 132             }
 133         }
 134         return true;
 135     }
 136 


 169      * this will do nothing.
 170      */
 171     public synchronized void subtract(int x, int y, int w, int h) {
 172         Rectangle subtract = new Rectangle(x, y, w, h);
 173         for (int i = 0; i < RECT_COUNT; i++) {
 174             if (subtract(paintRects[i], subtract)) {
 175                 if (paintRects[i] != null && paintRects[i].isEmpty()) {
 176                     paintRects[i] = null;
 177                 }
 178             }
 179         }
 180     }
 181 
 182     /**
 183      * Invokes paint and update on target Component with optimal
 184      * rectangular clip region.
 185      * If PAINT bounding rectangle is less than
 186      * MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
 187      * painted separately.  Otherwise the entire bounding rectangle is painted.
 188      *
 189      * @param   target Component to {@code paint} or {@code update}
 190      * @since   1.4
 191      */
 192     public void paint(Object target, boolean shouldClearRectBeforePaint) {
 193         Component comp = (Component)target;
 194 
 195         if (isEmpty()) {
 196             return;
 197         }
 198 
 199         if (!comp.isVisible()) {
 200             return;
 201         }
 202 
 203         RepaintArea ra = this.cloneAndReset();
 204 
 205         if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
 206             subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
 207         }
 208 
 209         if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {


 231                         if (i == UPDATE) {
 232                             updateComponent(comp, g);
 233                         } else {
 234                             if (shouldClearRectBeforePaint) {
 235                                 g.clearRect( ra.paintRects[i].x,
 236                                              ra.paintRects[i].y,
 237                                              ra.paintRects[i].width,
 238                                              ra.paintRects[i].height);
 239                             }
 240                             paintComponent(comp, g);
 241                         }
 242                     } finally {
 243                         g.dispose();
 244                     }
 245                 }
 246             }
 247         }
 248     }
 249 
 250     /**
 251      * Calls {@code Component.update(Graphics)} with given Graphics.
 252      */
 253     protected void updateComponent(Component comp, Graphics g) {
 254         if (comp != null) {
 255             comp.update(g);
 256         }
 257     }
 258 
 259     /**
 260      * Calls {@code Component.paint(Graphics)} with given Graphics.
 261      */
 262     protected void paintComponent(Component comp, Graphics g) {
 263         if (comp != null) {
 264             comp.paint(g);
 265         }
 266     }
 267 
 268     /**
 269      * Subtracts subtr from rect. If the result is rectangle
 270      * changes rect and returns true. Otherwise false.
 271      */
 272     static boolean subtract(Rectangle rect, Rectangle subtr) {
 273         if (rect == null || subtr == null) {
 274             return true;
 275         }
 276         Rectangle common = rect.intersection(subtr);
 277         if (common.isEmpty()) {
 278             return true;
 279         }
 280         if (rect.x == common.x && rect.y == common.y) {


< prev index next >