/* * Copyright (c) 2011, 2012, 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 * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javafx.scene.layout; /** * The BorderImageSlices defines how the image associated with a BorderImage should * be sliced and stretched over the Region. This is typically used for 9-slice * rendering, where an image is sliced into 9 sections and stretched so as to * scale up to the bounds of the Region. *
* Because the BorderImageSlices is immutable, it can safely be used in any * cache, and can safely be reused among multiple Regions. */ public final class BorderImageSlices { /** * An empty BorderImageSlices. Any BorderImage which uses this BorderImageSlices will not be drawn, * since no edges are defined and {@code filled} by default is {@code false} */ public static final BorderImageSlices EMPTY = new BorderImageSlices(0, 0, 0, 0, false, false, false, false, false); /** * The non-negative distance from the top of the image which defines the position for the top slice. * This might be interpreted as an absolute value, or as a percentage, as defined by * the {@code topAsPercentage} property. */ final double top; public final double getTop() { return top; } /** * The non-negative distance from the right of the image which defines the position for the right slice. * This might be interpreted as an absolute value, or as a percentage, as defined by * the {@code rightAsPercentage} property. */ final double right; public final double getRight() { return right; } /** * The non-negative distance from the bottom of the image which defines the position for the bottom slice. * This might be interpreted as an absolute value, or as a percentage, as defined by * the {@code bottomAsPercentage} property. */ final double bottom; public final double getBottom() { return bottom; } /** * The non-negative distance from the left of the image which defines the position for the left slice. * This might be interpreted as an absolute value, or as a percentage, as defined by * the {@code leftAsPercentage} property. */ final double left; public final double getLeft() { return left; } /** * Specifies whether the {@code top} property should be interpreted as a percentage ({@code true}) * of the image height or not ({@code false}). */ final boolean topAsPercentage; public final boolean isTopAsPercentage() { return topAsPercentage; } /** * Specifies whether the {@code right} property should be interpreted as a percentage ({@code true}) * of the image width or not ({@code false}). */ final boolean rightAsPercentage; public final boolean isRightAsPercentage() { return rightAsPercentage; } /** * Specifies whether the {@code bottom} property should be interpreted as a percentage ({@code true}) * of the image height or not ({@code false}). */ final boolean bottomAsPercentage; public final boolean isBottomAsPercentage() { return bottomAsPercentage; } /** * Specifies whether the {@code left} property should be interpreted as a percentage ({@code true}) * of the image width or not ({@code false}). */ final boolean leftAsPercentage; public final boolean isLeftAsPercentage() { return leftAsPercentage; } /** * Specifies whether or not the center patch (as defined by the left, right, top, and bottom slices) * should be drawn. */ final boolean filled; public final boolean isFilled() { return filled; } /** * A cached hash code for faster secondary usage. It is expected * that BorderImageSlices will be pulled from a cache in many cases. */ private int hash; /** * Creates a new BorderImageSlices, where the top, right, bottom, and left are * specified. None of these values are percentages, and the {@code filled} * property is set to true. * * @param top A positive number of pixels from the top * @param right A positive number of pixels in from the right * @param bottom A positive number of pixels up from the bottom * @param left A positive number of pixels in from the left */ public BorderImageSlices(double top, double right, double bottom, double left) { if (top < 0 || right < 0 || bottom < 0 || left < 0) { throw new IllegalArgumentException("All numbers must be >= 0"); } this.top = top; this.right = right; this.bottom = bottom; this.left = left; this.topAsPercentage = false; this.rightAsPercentage = false; this.bottomAsPercentage = false; this.leftAsPercentage = false; this.filled = true; } /** * Creates a new BorderImageSlices. The values for {@code top}, {@code right}, {@code bottom}, and * {@code left} must all be non-negative. If any value with an associated "asPercentage" flag set * to true is greater than 1 then it is clamped to the respective edge. * * @param top A positive number of pixels from the top * @param right A positive number of pixels in from the right * @param bottom A positive number of pixels up from the bottom * @param left A positive number of pixels in from the left * @param topAsPercentage Whether the top should be treated as a percentage * @param rightAsPercentage Whether the right should be treated as a percentage * @param bottomAsPercentage Whether the bottom should be treated as a percentage * @param leftAsPercentage Whether the left should be treated as a percentage * @param filled Whether the center section of the image should be used to fill. */ public BorderImageSlices( double top, double right, double bottom, double left, boolean topAsPercentage, boolean rightAsPercentage, boolean bottomAsPercentage, boolean leftAsPercentage, boolean filled) { if (top < 0 || right < 0 || bottom < 0 || left < 0) { throw new IllegalArgumentException("All numbers must be >= 0"); } this.top = top > 1 && topAsPercentage ? 1 : top; this.right = right > 1 && rightAsPercentage ? 1 : right; this.bottom = bottom > 1 && bottomAsPercentage ? 1 : bottom; this.left = left > 1 && leftAsPercentage ? 1 : left; this.topAsPercentage = topAsPercentage; this.rightAsPercentage = rightAsPercentage; this.bottomAsPercentage = bottomAsPercentage; this.leftAsPercentage = leftAsPercentage; this.filled = filled; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (hashCode() != o.hashCode()) return false; BorderImageSlices that = (BorderImageSlices) o; if (Double.compare(that.bottom, bottom) != 0) return false; if (bottomAsPercentage != that.bottomAsPercentage) return false; if (filled != that.filled) return false; if (Double.compare(that.left, left) != 0) return false; if (leftAsPercentage != that.leftAsPercentage) return false; if (Double.compare(that.right, right) != 0) return false; if (rightAsPercentage != that.rightAsPercentage) return false; if (Double.compare(that.top, top) != 0) return false; if (topAsPercentage != that.topAsPercentage) return false; return true; } @Override public int hashCode() { if (hash == 0) { int result; long temp; temp = top != +0.0d ? Double.doubleToLongBits(top) : 0L; result = (int) (temp ^ (temp >>> 32)); temp = right != +0.0d ? Double.doubleToLongBits(right) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); temp = bottom != +0.0d ? Double.doubleToLongBits(bottom) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); temp = left != +0.0d ? Double.doubleToLongBits(left) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); result = 31 * result + (topAsPercentage ? 1 : 0); result = 31 * result + (rightAsPercentage ? 1 : 0); result = 31 * result + (bottomAsPercentage ? 1 : 0); result = 31 * result + (leftAsPercentage ? 1 : 0); result = 31 * result + (filled ? 1 : 0); hash = result; } return hash; } }