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

fp.bugs 4175 Container.locate call to inside can cause misdelivered events

XMLWordPrintable

    • sparc
    • solaris_2.5

      From: Larry Liang <###@###.###>
      This does not look like form output to me.


      Hi,

      There is a bug in the Container.java class,

      Container.class line 349
      if ((comp != null) && comp.inside(x - comp.x, y - comp.y)){

      the inside() call should be:
      comp.inside(x, y)

      because it expects coordinates of the parent object, not relative to
      the current component. This bug caused deliverEvent() to deliver
      events to the wrong component. Thanks.

      Larry
      // This is an example program to show two bugs in AWT:
      // The Container.locate() function and
      // the Component.inside() function
      //
      // Container.locate(x, y) is supposed to return the component where the
      // (x, y) coordinate falls. But in the call to comp.inside(), it incorrectly
      // subtracted the location x and y value from the (x, y) parameter
      // respectively, and causes the component returned by the locate function
      // to be incorrect.
      //
      // Component.inside(x, y) is supposed to return true if the (x, y) point
      // is inside the component, and false otherwise. But it fails to check
      // if the (x, y) is less than the left edge and top edge. Therefore if
      // the x < location().x or y < location().y, it still returns true.
      //
      // Usage:
      // start the applet
      // click in any one of the box
      // The output should be:
      // print the current box number
      // print the box number to the right of current box
      // iterate through all components of the parent, and
      // print the box number of all those that have inside(x,y)
      // returning a true value. If the inside function is
      // correct, there should only be one box, the current box.
      //
      // Fix:
      // In file "Container.java" line 349, replace:
      // if ((comp != null) && comp.inside(x - comp.x, y - comp.y)) {
      // with:
      // if ((comp != null) && comp.inside(x, y)) {
      //
      // In file "Component.java" line 804, replace:
      // return (x >= 0) && ((x-this.x) < width) && (y >= 0) && ((y-this.y) < height);
      // with:
      // return ((x-this.x) >= 0) && ((x-this.x) < width) && ((y-this.y) >= 0) && ((y-this.y) < height);
      //

      import java.awt.*;
      import java.applet.*;

      public class awtbug extends Applet {
         static int total = 9;

         public awtbug() {
            setLayout(new GridLayout(1, total));
            for(int i = 0; i < total; i++) {
               add(new TestButton(i));
            }
         }
      }

      class TestButton extends Canvas {
         public TestButton(int num) {
            this.num = num;
         }

         public void paint(Graphics g) {
            g.drawRect(0, 0, size().width - 2, size().height - 2);
            g.drawString(Integer.toString(num), size().width / 2,
                         size().height / 2);
         }

         public boolean mouseDown(Event e, int x, int y) {
            identify();
            Container parent = getParent();
            if(num < awtbug.total - 1) {
               e.translate(location().x, location().y);
               TestButton sibling = (TestButton) parent.locate(e.x+size().width, e.y);
               System.out.print("My right hand button is: ");
               sibling.identify();
            }

            System.out.println("("+e.x+","+e.y+") is in the following boxes:");
            int cnt = parent.countComponents();
            for(int i = 0; i < cnt; i++) {
               TestButton box = (TestButton) parent.getComponent(i);
               if(box.inside(e.x, e.y)) {
                  box.identify();
               }
            }

            return(false);
         }

         public void identify() {
            System.out.println("Button " + num);
         }

         int num;
      }

            amfowler Anne Fowler (Inactive)
            bhagen Benjamin Hagen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: