Author: rwhitcomb Date: Sat Feb 27 05:43:47 2021 New Revision: 1886971 URL: http://svn.apache.org/viewvc?rev=1886971&view=rev Log: Add "enlarge" methods to Bounds and upgrade BoundsTest.
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java?rev=1886971&r1=1886970&r2=1886971&view=diff ============================================================================== --- pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java (original) +++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java Sat Feb 27 05:43:47 2021 @@ -311,6 +311,31 @@ public final class Bounds implements Ser } /** + * Create a new bounds object that is both offset and expanded by a single + * amount in both directions. The X- and Y-offset values are moved up/left + * and the width and height are expanded by twice the amount. The center point + * of the bounds remains the same. + * <p> This has the same effect as {@code translate(amt, amt).expand(2*amt, 2*amt)}. + * @param amt The amount to expand both the X- and Y- directions. + * @return A new enlarged bounds. + */ + public Bounds enlarge(final int amt) { + return new Bounds(x - amt, y - amt, width + (2 * amt), height + (2 * amt)); + } + + /** + * Create a new bounds object that is enlarged by different amounts in the + * X- and Y-directions. The center point of the bounds remains the same. + * <p> This has the same effect as {@code translate(dx, dy).expand(2*dx, 2*dy)}. + * @param dx The amount to enlarge in the X-direction. + * @param dy The amount to enlarge in the Y-direction. + * @return A new enlarged bounds. + */ + public Bounds enlarge(final int dx, final int dy) { + return new Bounds(x - dx, y - dy, width + (2 * dx), height + (2 * dy)); + } + + /** * @return Whether this bounded area contains the given point. * @param point The other point to test (must not be {@code null}). * @throws IllegalArgumentException if the point argument is {@code null}. Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java?rev=1886971&r1=1886970&r2=1886971&view=diff ============================================================================== --- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java (original) +++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java Sat Feb 27 05:43:47 2021 @@ -42,6 +42,7 @@ public class BoundsTest { Dimensions dim0 = new Dimensions(0, 0); Dimensions dim1 = new Dimensions(1, 1); + Dimensions dim5 = new Dimensions(5); Point p10 = new Point(10, 10); Bounds bnd10 = new Bounds(p10, dim1); Bounds bnd10a = new Bounds(dim1); @@ -62,6 +63,9 @@ public class BoundsTest { Bounds bnd6 = Bounds.decode("2, 3; 4, 5"); Bounds bnd6a = new Bounds(2, 3, 4, 5); + Bounds bnd7 = bnd6a.enlarge(2); + Bounds bnd7a = bnd6a.enlarge(1, 3); + assertEquals(Bounds.EMPTY, bnd0); assertNotEquals(bndMinus1, bnd0); assertNotEquals(bnd0, bnd1); @@ -72,15 +76,15 @@ public class BoundsTest { assertEquals(bnd2, bnd3); assertEquals(bnd3, bnd3a); - assertEquals(bndMinus1.getSize(), dim0); - assertEquals(bnd0.getSize(), dim0); - assertEquals(bnd1.getSize(), dim1); + assertEquals(dim0, bndMinus1.getSize()); + assertEquals(dim0, bnd0.getSize()); + assertEquals(dim1, bnd1.getSize()); assertFalse(bnd1.contains(bnd0)); assertFalse(bndMinus1.intersects(bnd0)); assertFalse(bnd0.intersects(bnd1)); - assertEquals(bnd0.intersect(bnd1), new Bounds(1, 1, -1, -1)); + assertEquals(new Bounds(1, 1, -1, -1), bnd0.intersect(bnd1)); assertTrue(bnd5a.intersects(bnd5b)); assertTrue(bnd0.union(bnd1).equals(new Bounds(0, 0, 2, 2))); @@ -90,7 +94,10 @@ public class BoundsTest { assertEquals(bndN, bndAll); assertEquals(bnd6, bnd6a); - assertEquals(bnd6a.toString(), "Bounds [2,3;4x5]"); + assertEquals("Bounds [2,3;4x5]", bnd6a.toString()); + + assertEquals("Bounds [0,1;8x9]", bnd7.toString()); + assertEquals("Bounds [1,0;6x11]", bnd7a.toString()); } }