Exemplo n.º 1
0
  /**
   * This utility function makes it easy to check whether the coordinates of the user's mouse click
   * (px,py) are on (or very close to) the ellipse defined by the bounding box with corners at
   * (x1,y1) and (x2,y2).
   *
   * @param px x coordinate of mouse click
   * @param py y coordinate of mouse click
   * @param x1 x point of the first coordinate of object to check for mouse click proximity
   * @param y1 y point of the first coordinate of object to check for mouse click proximity
   * @param x2 x point of the second coordinate of object to check for mouse click proximity
   * @param y2 y point of the second coordinate of object to check for mouse click proximity
   * @return true if the coordinates of the user's mouse click (px,py) are on close to the ellipse
   *     defined by the bounding box with corners at (x1,y1) and (x2,y2).
   */
  public static boolean clickHitEllipse(int px, int py, int x1, int y1, int x2, int y2) {
    int minX = (x1 < x2 ? x1 : x2);
    int minY = (y1 < y2 ? y1 : y2);
    int maxX = (x1 > x2 ? x1 : x2);
    int maxY = (y1 > y2 ? y1 : y2);

    Ellipse2D.Float innerEllipse =
        new Ellipse2D.Float(
            minX + SELECTION_DISTANCE,
            minY + SELECTION_DISTANCE,
            maxX - minX - 2 * SELECTION_DISTANCE,
            maxY - minY - 2 * SELECTION_DISTANCE);

    Ellipse2D.Float outerEllipse =
        new Ellipse2D.Float(
            minX - SELECTION_DISTANCE,
            minY - SELECTION_DISTANCE,
            maxX - minX + 2 * SELECTION_DISTANCE,
            maxY - minY + 2 * SELECTION_DISTANCE);

    return outerEllipse.contains(px, py) && !innerEllipse.contains(px, py);
  }