changeset 10896:4184d3dccefa jdk-11+9

8130379: Enhance the Bounds class with getCenter method Reviewed-by: kcr
author nlisker
date Fri, 06 Apr 2018 15:46:05 -0700
parents e6a9f15fe930
children 3555fafbabf0
files modules/javafx.graphics/src/main/java/javafx/geometry/Bounds.java modules/javafx.graphics/src/test/java/test/javafx/geometry/BoundingBoxTest.java
diffstat 2 files changed, 52 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.graphics/src/main/java/javafx/geometry/Bounds.java	Fri Apr 06 15:46:05 2018 -0700
+++ b/modules/javafx.graphics/src/main/java/javafx/geometry/Bounds.java	Fri Apr 06 15:46:05 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
  * @since JavaFX 2.0
  */
 public abstract class Bounds {
+
     /**
      * The x coordinate of the upper-left corner of this {@code Bounds}.
      *
@@ -51,6 +52,7 @@
      */
     public final double getMinY() { return minY; }
     private double minY;
+
     /**
      * The minimum z coordinate of this {@code Bounds}.
      *
@@ -59,6 +61,7 @@
      */
     public final double getMinZ() { return minZ; }
     private double minZ;
+
     /**
      * The width of this {@code Bounds}.
      *
@@ -67,6 +70,7 @@
      */
     public final double getWidth() { return width; }
     private double width;
+
     /**
      * The height of this {@code Bounds}.
      *
@@ -75,6 +79,7 @@
      */
     public final double getHeight() { return height; }
     private double height;
+
     /**
      * The depth of this {@code Bounds}.
      *
@@ -83,6 +88,7 @@
      */
     public final double getDepth() { return depth; }
     private double depth;
+
     /**
      * The x coordinate of the lower-right corner of this {@code Bounds}.
      *
@@ -91,6 +97,7 @@
      */
     public final double getMaxX() { return maxX; }
     private double maxX;
+
     /**
      * The y coordinate of the lower-right corner of this {@code Bounds}.
      *
@@ -99,6 +106,7 @@
      */
     public final double getMaxY() { return maxY; }
     private double maxY;
+
     /**
      * The maximum z coordinate of this {@code Bounds}.
      *
@@ -109,6 +117,39 @@
     private double maxZ;
 
     /**
+     * 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;
+    }
+
+    /**
+     * The central y coordinate of this {@code Bounds}.
+     *
+     * @return the central y coordinate
+     * @implSpec This call is equivalent to {@code (getMaxY() + getMinY())/2.0}.
+     * @since 11
+     */
+    public final double getCenterY() {
+        return (getMaxY() + getMinY()) * 0.5;
+    }
+
+    /**
+     * The central z coordinate of this {@code Bounds}.
+     *
+     * @return the central z coordinate
+     * @implSpec This call is equivalent to {@code (getMaxZ() + getMinZ())/2.0}.
+     * @since 11
+     */
+    public final double getCenterZ() {
+        return (getMaxZ() + getMinZ()) * 0.5;
+    }
+
+    /**
      * Indicates whether any of the dimensions(width, height or depth) of this bounds
      * is less than zero.
      * @return true if any of the dimensions(width, height or depth) of this bounds
--- a/modules/javafx.graphics/src/test/java/test/javafx/geometry/BoundingBoxTest.java	Fri Apr 06 15:46:05 2018 -0700
+++ b/modules/javafx.graphics/src/test/java/test/javafx/geometry/BoundingBoxTest.java	Fri Apr 06 15:46:05 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,10 +28,8 @@
 import javafx.geometry.BoundingBox;
 import javafx.geometry.Bounds;
 import javafx.geometry.Point2D;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 
+import static org.junit.Assert.*;
 import org.junit.Test;
 
 public class BoundingBoxTest {
@@ -60,6 +58,14 @@
     }
 
     @Test
+    public void testCenter() {
+        BoundingBox bb = new BoundingBox(-2, -1, 0,  2, 2, 1);
+        assertEquals(bb.getCenterX(), -1, 0);
+        assertEquals(bb.getCenterY(), 0, 0);
+        assertEquals(bb.getCenterZ(), 0.5, 0);
+    }
+
+    @Test
     public void testContains() {
         BoundingBox bb = new BoundingBox(0, 0, 2, 2);
         assertFalse(bb.contains((Point2D)null));