< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2015, 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


2588          *
2589          * Note: the AbstractButton must have a valid size (e.g. have
2590          * been added to a parent container whose ancestor container
2591          * is a valid top-level window) for this method to be able
2592          * to return a meaningful value.
2593          *
2594          * @param p the Point in local coordinates
2595          * @return the zero-based index of the character under Point p; if
2596          * Point is invalid returns -1.
2597          * @since 1.3
2598          */
2599         public int getIndexAtPoint(Point p) {
2600             View view = (View) AbstractButton.this.getClientProperty("html");
2601             if (view != null) {
2602                 Rectangle r = getTextRectangle();
2603                 if (r == null) {
2604                     return -1;
2605                 }
2606                 Rectangle2D.Float shape =
2607                     new Rectangle2D.Float(r.x, r.y, r.width, r.height);
2608                 Position.Bias bias[] = new Position.Bias[1];
2609                 return view.viewToModel(p.x, p.y, shape, bias);
2610             } else {
2611                 return -1;
2612             }
2613         }
2614 
2615         /**
2616          * Determine the bounding box of the character at the given
2617          * index into the string.  The bounds are returned in local
2618          * coordinates.  If the index is invalid an empty rectangle is
2619          * returned.
2620          *
2621          * Note: the AbstractButton must have a valid size (e.g. have
2622          * been added to a parent container whose ancestor container
2623          * is a valid top-level window) for this method to be able
2624          * to return a meaningful value.
2625          *
2626          * @param i the index into the String
2627          * @return the screen coordinates of the character's the bounding box,
2628          * if index is invalid returns an empty rectangle.


   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


2588          *
2589          * Note: the AbstractButton must have a valid size (e.g. have
2590          * been added to a parent container whose ancestor container
2591          * is a valid top-level window) for this method to be able
2592          * to return a meaningful value.
2593          *
2594          * @param p the Point in local coordinates
2595          * @return the zero-based index of the character under Point p; if
2596          * Point is invalid returns -1.
2597          * @since 1.3
2598          */
2599         public int getIndexAtPoint(Point p) {
2600             View view = (View) AbstractButton.this.getClientProperty("html");
2601             if (view != null) {
2602                 Rectangle r = getTextRectangle();
2603                 if (r == null) {
2604                     return -1;
2605                 }
2606                 Rectangle2D.Float shape =
2607                     new Rectangle2D.Float(r.x, r.y, r.width, r.height);
2608                 Position.Bias[] bias = new Position.Bias[1];
2609                 return view.viewToModel(p.x, p.y, shape, bias);
2610             } else {
2611                 return -1;
2612             }
2613         }
2614 
2615         /**
2616          * Determine the bounding box of the character at the given
2617          * index into the string.  The bounds are returned in local
2618          * coordinates.  If the index is invalid an empty rectangle is
2619          * returned.
2620          *
2621          * Note: the AbstractButton must have a valid size (e.g. have
2622          * been added to a parent container whose ancestor container
2623          * is a valid top-level window) for this method to be able
2624          * to return a meaningful value.
2625          *
2626          * @param i the index into the String
2627          * @return the screen coordinates of the character's the bounding box,
2628          * if index is invalid returns an empty rectangle.


< prev index next >