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

Enhance the Bounds class with getCenter method

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • jfx11
    • javafx
    • None
    • source, binary
    • minimal
    • New API, no compatibility risk
    • Java API
    • JDK

      Summary

      Add convenience methods to the Bounds class which compute the center coordinate for each axis of the bounds.

      Problem

      The Bounds class is missing common convenience operations. These methods are useful for centering calculations. The default coordinate for a Node is its 0, however, user interfaces many times rely on the center coordinate for positioning. In some 2D contexts, an alignment/position variable can be set, but in custom calculations such as animations, transformations (e.g., rotation around the center), and 3D (which has no layout management), these coordinates must be calculated.

      Solution

      Add convenience methods to the Bounds class:

      getCenterX(), which gets the center coordinate by (getMinX() + getMaxX()) / 2 and similarly for Y and Z.

      Usage example:

      Bounds bounds = node.getBoundsInLocal();
      double centerX = (bounds.getMinX() + bounds.getMaxX()) / 2;
      double centerY = (bounds.getMinY() + bounds.getMaxY()) / 2;
      
      // Alternatively
      
      Bounds bounds = node.getBoundsInLocal();
      double centerX = bounds.getCenterX();
      double centerY = bounds.getCenterY();

      Specification

      /**
       * The central x coordinate of this {@code Bounds}.
       *
       * @return the central x coordinate
       * @implSpec This call is equivalent to {@code (getMaxX() + getMinX())/2.0}.
       * @since 11
       */
      public final double getCenterX() {
          return (getMaxX() + getMinX()) * 0.5;
      }
      
      // same for y and z

      Webrev: http://cr.openjdk.java.net/~nlisker/8130379/webrev.01/

            nlisker Nir Lisker
            webbuggrp Webbug Group
            Kevin Rushforth
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: