< prev index next >

src/java.desktop/share/classes/javax/swing/SwingUtilities.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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


 293 
 294     /**
 295      * Returns the deepest visible descendent Component of <code>parent</code>
 296      * that contains the location <code>x</code>, <code>y</code>.
 297      * If <code>parent</code> does not contain the specified location,
 298      * then <code>null</code> is returned.  If <code>parent</code> is not a
 299      * container, or none of <code>parent</code>'s visible descendents
 300      * contain the specified location, <code>parent</code> is returned.
 301      *
 302      * @param parent the root component to begin the search
 303      * @param x the x target location
 304      * @param y the y target location
 305      *
 306      * @return the deepest component
 307      */
 308     public static Component getDeepestComponentAt(Component parent, int x, int y) {
 309         if (!parent.contains(x, y)) {
 310             return null;
 311         }
 312         if (parent instanceof Container) {
 313             Component components[] = ((Container)parent).getComponents();
 314             for (Component comp : components) {
 315                 if (comp != null && comp.isVisible()) {
 316                     Point loc = comp.getLocation();
 317                     if (comp instanceof Container) {
 318                         comp = getDeepestComponentAt(comp, x - loc.x, y - loc.y);
 319                     } else {
 320                         comp = comp.getComponentAt(x - loc.x, y - loc.y);
 321                     }
 322                     if (comp != null && comp.isVisible()) {
 323                         return comp;
 324                     }
 325                 }
 326             }
 327         }
 328         return parent;
 329     }
 330 
 331 
 332     /**
 333      * Returns a MouseEvent similar to <code>sourceEvent</code> except that its x


 589     }
 590 
 591     /**
 592      * Convenience returning an array of rect representing the regions within
 593      * <code>rectA</code> that do not overlap with <code>rectB</code>. If the
 594      * two Rects do not overlap, returns an empty array
 595      *
 596      * @param rectA the first rectangle
 597      * @param rectB the second rectangle
 598      *
 599      * @return an array of rectangles representing the regions within {@code rectA}
 600      *         that do not overlap with {@code rectB}.
 601      */
 602     public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) {
 603         if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) {
 604             return new Rectangle[0];
 605         }
 606 
 607         Rectangle t = new Rectangle();
 608         Rectangle a=null,b=null,c=null,d=null;
 609         Rectangle result[];
 610         int rectCount = 0;
 611 
 612         /* rectA contains rectB */
 613         if (isRectangleContainingRectangle(rectA,rectB)) {
 614             t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height;
 615             if(t.width > 0 && t.height > 0) {
 616                 a = new Rectangle(t);
 617                 rectCount++;
 618             }
 619 
 620             t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y;
 621             if(t.width > 0 && t.height > 0) {
 622                 b = new Rectangle(t);
 623                 rectCount++;
 624             }
 625 
 626             t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width;
 627             t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
 628             if(t.width > 0 && t.height > 0) {
 629                 c = new Rectangle(t);


   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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


 293 
 294     /**
 295      * Returns the deepest visible descendent Component of <code>parent</code>
 296      * that contains the location <code>x</code>, <code>y</code>.
 297      * If <code>parent</code> does not contain the specified location,
 298      * then <code>null</code> is returned.  If <code>parent</code> is not a
 299      * container, or none of <code>parent</code>'s visible descendents
 300      * contain the specified location, <code>parent</code> is returned.
 301      *
 302      * @param parent the root component to begin the search
 303      * @param x the x target location
 304      * @param y the y target location
 305      *
 306      * @return the deepest component
 307      */
 308     public static Component getDeepestComponentAt(Component parent, int x, int y) {
 309         if (!parent.contains(x, y)) {
 310             return null;
 311         }
 312         if (parent instanceof Container) {
 313             Component[] components = ((Container)parent).getComponents();
 314             for (Component comp : components) {
 315                 if (comp != null && comp.isVisible()) {
 316                     Point loc = comp.getLocation();
 317                     if (comp instanceof Container) {
 318                         comp = getDeepestComponentAt(comp, x - loc.x, y - loc.y);
 319                     } else {
 320                         comp = comp.getComponentAt(x - loc.x, y - loc.y);
 321                     }
 322                     if (comp != null && comp.isVisible()) {
 323                         return comp;
 324                     }
 325                 }
 326             }
 327         }
 328         return parent;
 329     }
 330 
 331 
 332     /**
 333      * Returns a MouseEvent similar to <code>sourceEvent</code> except that its x


 589     }
 590 
 591     /**
 592      * Convenience returning an array of rect representing the regions within
 593      * <code>rectA</code> that do not overlap with <code>rectB</code>. If the
 594      * two Rects do not overlap, returns an empty array
 595      *
 596      * @param rectA the first rectangle
 597      * @param rectB the second rectangle
 598      *
 599      * @return an array of rectangles representing the regions within {@code rectA}
 600      *         that do not overlap with {@code rectB}.
 601      */
 602     public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) {
 603         if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) {
 604             return new Rectangle[0];
 605         }
 606 
 607         Rectangle t = new Rectangle();
 608         Rectangle a=null,b=null,c=null,d=null;
 609         Rectangle[] result;
 610         int rectCount = 0;
 611 
 612         /* rectA contains rectB */
 613         if (isRectangleContainingRectangle(rectA,rectB)) {
 614             t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height;
 615             if(t.width > 0 && t.height > 0) {
 616                 a = new Rectangle(t);
 617                 rectCount++;
 618             }
 619 
 620             t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y;
 621             if(t.width > 0 && t.height > 0) {
 622                 b = new Rectangle(t);
 623                 rectCount++;
 624             }
 625 
 626             t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width;
 627             t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
 628             if(t.width > 0 && t.height > 0) {
 629                 c = new Rectangle(t);


< prev index next >