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

Enhance the Bounds class with getCenter method

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: openjfx11
    • Component/s: javafx
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      source, binary
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      New API, no compatibility risk
    • Interface Kind:
      Java API
    • Scope:
      JDK

      Description

      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/

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                nlisker Nir Lisker
                Reporter:
                webbuggrp Webbug Group
                Reviewed By:
                Kevin Rushforth
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: