/* * 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; import javafx.scene.image.Image; /** * Defines properties describing how to render an image as the background to * some {@link Region}. A BackgroundImage must have an Image specified (it cannot be * null). The {@link #getRepeatX() repeatX} and {@link #getRepeatY() repeatY} * properties define how the image is to be repeated in each direction. The * {@link #getPosition() position} property defines how to position the image on the * Region while the {@link #getSize() size} property defines the size of the image * on the Region. For example, the {@code size} might be defined with * {@link javafx.scene.layout.BackgroundSize#isCover() cover = true}, meaning the image * should be stretched to cover the entire rendering surface of the Region. *

* Because the BackgroundImage is immutable, it can safely be used in any * cache, and can safely be reused among multiple Regions. */ public final class BackgroundImage { /** * The image to be used. This will never be null. If this * image fails to load, then the entire BackgroundImage will * be skipped at rendering time. */ final Image image; public final Image getImage() { return image; } /** * Indicates in what manner (if at all) the background image * is to be repeated along the x-axis of the region. This * will never be null. */ final BackgroundRepeat repeatX; public final BackgroundRepeat getRepeatX() { return repeatX; } /** * Indicates in what manner (if at all) the background image * is to be repeated along the y-axis of the region. This will * never be null. */ final BackgroundRepeat repeatY; public final BackgroundRepeat getRepeatY() { return repeatY; } /** * The position of this BackgroundImage relative to the Region. Note that any * position outside the background area of the region will be clipped. */ final BackgroundPosition position; public final BackgroundPosition getPosition() { return position; } /** * The size of this image relative to the Region. */ final BackgroundSize size; public final BackgroundSize getSize() { return size; } /** * A cached hash code for faster secondary usage. It is expected * that BackgroundImage will be pulled from a cache in many cases. */ private int hash = 0; /** * Creates a new BackgroundImage. The {@code image} must be specified. * * @param image The image to use. This cannot be null. * @param repeatX The BackgroundRepeat for the x axis. If null, this value defaults to REPEAT. * @param repeatY The BackgroundRepeat for the y axis. If null, this value defaults to REPEAT. * @param position The BackgroundPosition to use. If null, defaults to BackgroundPosition.DEFAULT. * @param size The BackgroundSize. If null, defaults to BackgroundSize.DEFAULT. */ public BackgroundImage(Image image, BackgroundRepeat repeatX, BackgroundRepeat repeatY, BackgroundPosition position, BackgroundSize size) { if (image == null) throw new NullPointerException("Image cannot be null"); this.image = image; this.repeatX = repeatX == null ? BackgroundRepeat.REPEAT : repeatX; this.repeatY = repeatY == null ? BackgroundRepeat.REPEAT : repeatY; this.position = position == null ? BackgroundPosition.DEFAULT : position; this.size = size == null ? BackgroundSize.DEFAULT : size; } /** * @inheritDoc */ @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; // Because the hash is cached, this can be very fast if (hashCode() != o.hashCode()) return false; BackgroundImage that = (BackgroundImage) o; if (!image.equals(that.image)) return false; if (!position.equals(that.position)) return false; if (repeatX != that.repeatX) return false; if (repeatY != that.repeatY) return false; if (!size.equals(that.size)) return false; return true; } /** * @inheritDoc */ @Override public int hashCode() { if (hash == 0) { // Please note: I have to compute into a temporary result variable // before making the final assignment, or the hash code could // come out wrong in a multi-threaded scenario. int result = image.hashCode(); result = 31 * result + repeatX.hashCode(); result = 31 * result + repeatY.hashCode(); result = 31 * result + position.hashCode(); result = 31 * result + size.hashCode(); hash = result; } return hash; } }