Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4219023

Polygon.getBounds() returns not an object copy

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.1.7, 1.2.0
    • client-libs
    • beta
    • generic
    • generic


      ingrid.yao@Eng 1999-03-10

      In JDK 1.1.7B, Polygon.getBounds() returns not an object copy but a
      reference copy of its field. It can cause serious side-effects.

      test program
      =============

      import java.awt.*;

      public class PolyTest {
          public PolyTest() {
              int[] x = {0,10,10,0};
              int[] y = {0,0,10,10};
              Polygon p = new Polygon(x,y,4);
              System.out.println(p.getBounds());
              Rectangle r = p.getBounds();
              r.translate(-5,-5);
              System.out.println(p.getBounds());
          }

           public static void main(String[] args) {
                new PolyTest();
           }
      }

      Translate r, the bounds of polygon is also translated
      because getBounds() returns only regerence copy.

            prssunw Prs Prs (Inactive)
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: