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

fp.bugs 3456 Win32 locations of AWT components are reported incorrectly

XMLWordPrintable

    • 1.0.2
    • sparc
    • solaris_2.5
    • Not verified

      >From: ###@###.### (Doug Stein)
      This does not look like form output to me.


      ----------
      X-Sun-Data-Type: text
      X-Sun-Data-Description: text
      X-Sun-Data-Name: text
      X-Sun-Charset: us-ascii
      X-Sun-Content-Lines: 106


      Category: AWT

      Priority: CRITICAL! (Low/Medium/High/Critical)

      JDK version: JDK-1_0-win32-x86.exe

      Platforms: Windows 95 [Version 4.00.950]
      Windows NT version 3.51

      Synopsis
      --------

      The locations of AWT components are reported incorrectly on Windows.


      Description
      -----------

      This bug occurs on Windows95 and WindowsNT.

      The "location" method of the Component class returns the location of
      a component relative to its parent. On Windows this location is not
      always correct.

      I did some experimentation to figure out what was going on. The
      "reshape" method takes an x, y, width, and height. I subclassed a
      component and overrode reshape to keep track of the current location
      of the component. I discovered that the x and y values reported by
      the "location" method do not always match the x and y values given in
      the most recent call to "reshape"!


      Test Case
      ---------

      - Compile and run location.java:

      javac location.java
      java location

      - Resize the window several times.

      On Motif, you get the correct output for the x and y values:

      LOCATIONS
      x=5 y=25 w=274 h=242
      x=10 y=10 w=254 h=222
      x=10 y=10 w=234 h=202
      x=10 y=10 w=214 h=182
      x=10 y=10 w=194 h=162
      x=10 y=10 w=174 h=142
      x=10 y=10 w=154 h=122
      x=10 y=10 w=134 h=102
      x=10 y=10 w=114 h=82
      x=10 y=10 w=94 h=62
      x=10 y=10 w=74 h=42

      However, on Windows, you get something like this:

      LOCATIONS
      x=4 y=23 w=268 h=244
      x=14 y=33 w=248 h=224
      x=24 y=43 w=228 h=204
      x=34 y=53 w=208 h=184
      x=44 y=63 w=188 h=164
      x=54 y=73 w=168 h=144
      x=64 y=83 w=148 h=124
      x=74 y=93 w=128 h=104
      x=84 y=103 w=108 h=84
      x=94 y=113 w=88 h=64
      x=104 y=123 w=68 h=44

      You may also see a mixture of correct and incorrect values:

      LOCATIONS
      x=4 y=23 w=217 h=392
      x=10 y=10 w=197 h=372
      x=10 y=10 w=177 h=352
      x=10 y=10 w=157 h=332
      x=10 y=10 w=137 h=312
      x=10 y=10 w=117 h=292
      x=64 y=83 w=97 h=272
      x=74 y=93 w=77 h=252
      x=84 y=103 w=57 h=232
      x=10 y=10 w=37 h=212
      x=10 y=10 w=17 h=192

      Normally, I get a lot more incorrect locations than correct locations.
      However, when I redirect the output of the test program to a file, I
      get more correct positions than incorrect positions. This indicates a
      race condition of some sort.


      ----------
      X-Sun-Data-Type: default
      X-Sun-Data-Description: default
      X-Sun-Data-Name: location.java
      X-Sun-Charset: us-ascii
      X-Sun-Content-Lines: 106


      import java.awt.*;

      class BorderPanel extends Panel {
        private static final int borderWidth = 10;

        public Dimension minimumSize() {
          Dimension d;

          if (countComponents() > 0) {
            Component comp = getComponent(0);
            d = comp.minimumSize();
          }
          else {
            d = new Dimension(0, 0);
          }

          d.width += 2*borderWidth;
          d.height += 2*borderWidth;

          return d;
        }

        public Dimension preferredSize() {
          return minimumSize();
        }

        public void layout() {
          Dimension d = size();
          Component components[] = getComponents();

          for (int i=0; i<components.length; i++) {
            if (i==0) {
      components[i].reshape(borderWidth, borderWidth,
      d.width - 2*borderWidth,
      d.height - 2*borderWidth);
            }
            else {
      components[i].reshape(-1, -1, 0, 0);
            }
          }
        }

        public void paint(Graphics g) {
          Dimension d = size();
          g.drawRect(0, 0, d.width-1, d.height-1);
          g.drawRect(1, 1, d.width-3, d.height-3);
        }
      }

      public class location extends Frame {
        private int validateCount;

        public static void main(String argv[]) {
          new location();
        }

        public location() {
          Button button = new Button("Button");
          Panel p = new BorderPanel();
          createNestedPanels(button, p, 10);
          add("Center", p);

          show();
          resize(preferredSize());
          validate();
        }

        public void createNestedPanels(Component c, Panel parent, int num) {
          if (num == 0)
            parent.add(c);
          else {
            Panel bp = new BorderPanel();
            parent.add(bp);
            createNestedPanels(c, bp, num-1);
          }
        }

        public synchronized void validate() {
          boolean valid = isValid();
          super.validate();

          System.out.println("validate " + validateCount++);
          if (!valid)
            printLocations();
        }

        public void printLocations() {
          Point p;
          Dimension d;
          Panel panel;
          Component c = getComponent(0);

          System.out.println("LOCATIONS");
          while (c instanceof Panel) {
            panel = (Panel)c;
            p = panel.location();
            d = panel.size();

            System.out.println("x=" + p.x + "\\ty=" + p.y +
      "\\tw=" + d.width + "\\th=" + d.height);
            c = panel.getComponent(0);
          }
          System.out.println("");
        }
      }

            egilbertsunw Eric Gilbertson (Inactive)
            bhagen Benjamin Hagen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: