I already have changed lots of Rectangle.union() and
Rectangle.intersection() calls in Swing to use
SwingUtilities.computeUnion() and SwingUtilities.computeIntersection()
instead. The whole point of these utility methods is avoiding the
creation of new Rectangle objects. Really useful, except that we still
created new Rectangles there... I have fixed that and adjusted the API
comments accordingly. Our Swing should now be creating much less
Rectangles I would think.

2006-02-27  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/SwingUtilities.java
        (computeIntersection): Changed to store result in rect, instead of
        creating new Rectangle instances. Fixed API docs accordingly.
        (computeUnion): Changed to store result in rect, instead of
        creating new Rectangle instances. Fixed API docs accordingly.

/Roman
Index: javax/swing/SwingUtilities.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/SwingUtilities.java,v
retrieving revision 1.44
diff -u -r1.44 SwingUtilities.java
--- javax/swing/SwingUtilities.java	14 Feb 2006 22:30:49 -0000	1.44
+++ javax/swing/SwingUtilities.java	27 Feb 2006 13:16:01 -0000
@@ -1246,26 +1246,31 @@
   }
 
   /**
-   * Calculates the intersection of two rectangles.
+   * Calculates the intersection of two rectangles. The result is stored
+   * in <code>rect</code>. This is basically the same
+   * like [EMAIL PROTECTED] Rectangle#intersection(Rectangle)}, only that it does not
+   * create new Rectangle instances. The tradeoff is that you loose any data in
+   * <code>rect</code>.
    *
    * @param x upper-left x coodinate of first rectangle
    * @param y upper-left y coodinate of first rectangle
    * @param w width of first rectangle
    * @param h height of first rectangle
    * @param rect a Rectangle object of the second rectangle
-   * @throws NullPointerException if rect is null.
+   *
+   * @throws NullPointerException if rect is null
    *
    * @return a rectangle corresponding to the intersection of the
-   * two rectangles. A zero rectangle is returned if the rectangles
-   * do not overlap.
+   *         two rectangles. An empty rectangle is returned if the rectangles
+   *         do not overlap
    */
   public static Rectangle computeIntersection(int x, int y, int w, int h,
                                               Rectangle rect)
   {
-    int x2 = (int) rect.getX();
-    int y2 = (int) rect.getY();
-    int w2 = (int) rect.getWidth();
-    int h2 = (int) rect.getHeight();
+    int x2 = (int) rect.x;
+    int y2 = (int) rect.y;
+    int w2 = (int) rect.width;
+    int h2 = (int) rect.height;
 
     int dx = (x > x2) ? x : x2;
     int dy = (y > y2) ? y : y2;
@@ -1273,9 +1278,11 @@
     int dh = (y + h < y2 + h2) ? (y + h - dy) : (y2 + h2 - dy);
 
     if (dw >= 0 && dh >= 0)
-      return new Rectangle(dx, dy, dw, dh);
+      rect.setBounds(dx, dy, dw, dh);
+    else
+      rect.setBounds(0, 0, 0, 0);
 
-    return new Rectangle(0, 0, 0, 0);
+    return rect;
   }
   
   /**
@@ -1292,26 +1299,31 @@
   }
 
   /**
-   * Calculates the union of two rectangles.
+   * Calculates the union of two rectangles. The result is stored in
+   * <code>rect</code>. This is basically the same as
+   * [EMAIL PROTECTED] Rectangle#union(Rectangle)} except that it avoids creation of new
+   * Rectangle objects. The tradeoff is that you loose any data in
+   * <code>rect</code>.
    *
    * @param x upper-left x coodinate of first rectangle
    * @param y upper-left y coodinate of first rectangle
    * @param w width of first rectangle
    * @param h height of first rectangle
    * @param rect a Rectangle object of the second rectangle
-   * @throws NullPointerException if rect is null.
+   *
+   * @throws NullPointerException if rect is null
    *
    * @return a rectangle corresponding to the union of the
-   * two rectangles. A rectangle encompassing both is returned if the
-   * rectangles do not overlap.
+   *         two rectangles; a rectangle encompassing both is returned if the
+   *         rectangles do not overlap
    */
   public static Rectangle computeUnion(int x, int y, int w, int h,
                                        Rectangle rect)
   {
-    int x2 = (int) rect.getX();
-    int y2 = (int) rect.getY();
-    int w2 = (int) rect.getWidth();
-    int h2 = (int) rect.getHeight();
+    int x2 = (int) rect.x;
+    int y2 = (int) rect.y;
+    int w2 = (int) rect.width;
+    int h2 = (int) rect.height;
 
     int dx = (x < x2) ? x : x2;
     int dy = (y < y2) ? y : y2;
@@ -1319,9 +1331,10 @@
     int dh = (y + h > y2 + h2) ? (y + h - dy) : (y2 + h2 - dy);
 
     if (dw >= 0 && dh >= 0)
-      return new Rectangle(dx, dy, dw, dh);
-
-    return new Rectangle(0, 0, 0, 0);
+      rect.setBounds(dx, dy, dw, dh);
+    else
+      rect.setBounds(0, 0, 0, 0);
+    return rect;
   }
 
   /**

Reply via email to