1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.javafx.scene.image;
  27 
  28 import javafx.geometry.Rectangle2D;
  29 import javafx.scene.image.Image;
  30 import javafx.scene.image.ImageView;
  31 
  32 public final class ImageViewConfig {
  33     private final Image image;
  34     private final float x;
  35     private final float y;
  36     private final Rectangle2D viewport;
  37     private final float fitWidth;
  38     private final float fitHeight;
  39     private final boolean preserveRatio;
  40 
  41     private ImageViewConfig(final Image image,
  42                             final float x,
  43                             final float y,
  44                             final Rectangle2D viewport,
  45                             final float fitWidth,
  46                             final float fitHeight,
  47                             final boolean preserveRatio) {
  48         this.image = image;
  49         this.x = x;
  50         this.y = y;
  51         this.viewport = viewport;
  52         this.fitWidth = fitWidth;
  53         this.fitHeight = fitHeight;
  54         this.preserveRatio = preserveRatio;
  55     }
  56 
  57     public void applyTo(final ImageView imageView) {
  58         imageView.setImage(image);
  59         imageView.setX(x);
  60         imageView.setY(y);
  61         imageView.setViewport(viewport);
  62         imageView.setFitWidth(fitWidth);
  63         imageView.setFitHeight(fitHeight);
  64         imageView.setPreserveRatio(preserveRatio);
  65     }
  66 
  67     public static ImageViewConfig config(final Image image,
  68                                          final float x,
  69                                          final float y) {
  70         return new ImageViewConfig(image, x, y, null, 0, 0, false);
  71     }
  72 
  73     public static ImageViewConfig config(final Image image,
  74                                          final float x,
  75                                          final float y,
  76                                          final float fitWidth,
  77                                          final float fitHeight,
  78                                          final boolean preserveRatio) {
  79         return new ImageViewConfig(image, x, y, null, fitWidth, fitHeight,
  80                                    preserveRatio);
  81     }
  82 
  83     public static ImageViewConfig config(final Image image,
  84                                          final float x,
  85                                          final float y,
  86                                          final float vpX,
  87                                          final float vpY,
  88                                          final float vpWidth,
  89                                          final float vpHeight,
  90                                          final float fitWidth,
  91                                          final float fitHeight,
  92                                          final boolean preserveRatio) {
  93         return new ImageViewConfig(image, x, y,
  94                                    new Rectangle2D(vpX, vpY, vpWidth, vpHeight),
  95                                    fitWidth, fitHeight, preserveRatio);
  96     }
  97 }