Class ImageView

java.lang.Object
javafx.scene.Node
javafx.scene.image.ImageView
All Implemented Interfaces:
Styleable, EventTarget

@DefaultProperty("image") public class ImageView extends Node
The ImageView is a Node used for painting images loaded with Image class.

This class allows resizing the displayed image (with or without preserving the original aspect ratio) and specifying a viewport into the source image for restricting the pixels displayed by this ImageView.

Example code for displaying images


 import javafx.application.Application;
 import javafx.geometry.Rectangle2D;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.layout.HBox;
 import javafx.scene.paint.Color;
 import javafx.stage.Stage;

 public class HelloImageView extends Application {

     @Override public void start(Stage stage) {
         // load the image
         Image image = new Image("flower.png");

         // simple displays ImageView the image as is
         ImageView iv1 = new ImageView();
         iv1.setImage(image);

         // resizes the image to have width of 100 while preserving the ratio and using
         // higher quality filtering method; this ImageView is also cached to
         // improve performance
         ImageView iv2 = new ImageView();
         iv2.setImage(image);
         iv2.setFitWidth(100);
         iv2.setPreserveRatio(true);
         iv2.setSmooth(true);
         iv2.setCache(true);

         // defines a viewport into the source image (achieving a "zoom" effect) and
         // displays it rotated
         ImageView iv3 = new ImageView();
         iv3.setImage(image);
         Rectangle2D viewportRect = new Rectangle2D(40, 35, 110, 110);
         iv3.setViewport(viewportRect);
         iv3.setRotate(90);

         Group root = new Group();
         Scene scene = new Scene(root);
         scene.setFill(Color.BLACK);
         HBox box = new HBox();
         box.getChildren().add(iv1);
         box.getChildren().add(iv2);
         box.getChildren().add(iv3);
         root.getChildren().add(box);

         stage.setTitle("ImageView");
         stage.setWidth(415);
         stage.setHeight(200);
         stage.setScene(scene);
         stage.sizeToScene();
         stage.show();
     }

     public static void main(String[] args) {
         Application.launch(args);
     }
 }
 

The code above produces the following:

A visual rendering of the ImageView example

Since:
JavaFX 2.0
  • Property Details

    • image

      public final ObjectProperty<Image> imageProperty
      The Image to be painted by this ImageView.
      Default value:
      null
      See Also:
    • x

      public final DoubleProperty xProperty
      The current x coordinate of the ImageView origin.
      Default value:
      0
      See Also:
    • y

      public final DoubleProperty yProperty
      The current y coordinate of the ImageView origin.
      Default value:
      0
      See Also:
    • fitWidth

      public final DoubleProperty fitWidthProperty
      The width of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic width of the image will be used as the fitWidth.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
      See Also:
    • fitHeight

      public final DoubleProperty fitHeightProperty
      The height of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic height of the image will be used as the fitHeight.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
      See Also:
    • preserveRatio

      public final BooleanProperty preserveRatioProperty
      Indicates whether to preserve the aspect ratio of the source image when scaling to fit the image within the fitting bounding box.

      If set to true, it affects the dimensions of this ImageView in the following way

      • If only fitWidth is set, height is scaled to preserve ratio
      • If only fitHeight is set, width is scaled to preserve ratio
      • If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
      If unset or set to false, it affects the dimensions of this ImageView in the following way
      • If only fitWidth is set, image's view width is scaled to match and height is unchanged;
      • If only fitHeight is set, image's view height is scaled to match and height is unchanged;
      • If both are set, the image view is scaled to match both.
      Note that the dimensions of this node as reported by the node's bounds will be equal to the size of the scaled image and is guaranteed to be contained within fitWidth x fitHeight bonding box.
      Default value:
      false
      See Also:
    • smooth

      public final BooleanProperty smoothProperty
      Indicates whether to use a better quality filtering algorithm or a faster one when transforming or scaling the source image to fit within the bounding box provided by fitWidth and fitHeight.

      If set to true a better quality filtering will be used, if set to false a faster but lesser quality filtering will be used.

      The default value depends on platform configuration.

      Default value:
      platform-dependent
      See Also:
    • viewport

      public final ObjectProperty<Rectangle2D> viewportProperty
      The rectangular viewport into the image. The viewport is specified in the coordinates of the image, prior to scaling or any other transformations.

      If viewport is null, the entire image is displayed. If viewport is non-null, only the portion of the image which falls within the viewport will be displayed. If the image does not fully cover the viewport then any remaining area of the viewport will be empty.

      Default value:
      null
      See Also:
  • Field Details

    • SMOOTH_DEFAULT

      public static final boolean SMOOTH_DEFAULT
      Platform-dependent default value of the smooth property.
  • Constructor Details

    • ImageView

      public ImageView()
      Allocates a new ImageView object.
    • ImageView

      public ImageView(String url)
      Allocates a new ImageView object with image loaded from the specified URL.

      The new ImageView(url) has the same effect as new ImageView(new Image(url)).

      Parameters:
      url - the string representing the URL from which to load the image
      Throws:
      NullPointerException - if URL is null
      IllegalArgumentException - if URL is invalid or unsupported
      Since:
      JavaFX 2.1
    • ImageView

      public ImageView(Image image)
      Allocates a new ImageView object using the given image.
      Parameters:
      image - Image that this ImageView uses
  • Method Details

    • setImage

      public final void setImage(Image value)
      Sets the value of the property image.
      Property description:
      The Image to be painted by this ImageView.
      Default value:
      null
    • getImage

      public final Image getImage()
      Gets the value of the property image.
      Property description:
      The Image to be painted by this ImageView.
      Default value:
      null
    • imageProperty

      public final ObjectProperty<Image> imageProperty()
      The Image to be painted by this ImageView.
      Default value:
      null
      See Also:
    • setX

      public final void setX(double value)
      Sets the value of the property x.
      Property description:
      The current x coordinate of the ImageView origin.
      Default value:
      0
    • getX

      public final double getX()
      Gets the value of the property x.
      Property description:
      The current x coordinate of the ImageView origin.
      Default value:
      0
    • xProperty

      public final DoubleProperty xProperty()
      The current x coordinate of the ImageView origin.
      Default value:
      0
      See Also:
    • setY

      public final void setY(double value)
      Sets the value of the property y.
      Property description:
      The current y coordinate of the ImageView origin.
      Default value:
      0
    • getY

      public final double getY()
      Gets the value of the property y.
      Property description:
      The current y coordinate of the ImageView origin.
      Default value:
      0
    • yProperty

      public final DoubleProperty yProperty()
      The current y coordinate of the ImageView origin.
      Default value:
      0
      See Also:
    • setFitWidth

      public final void setFitWidth(double value)
      Sets the value of the property fitWidth.
      Property description:
      The width of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic width of the image will be used as the fitWidth.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
    • getFitWidth

      public final double getFitWidth()
      Gets the value of the property fitWidth.
      Property description:
      The width of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic width of the image will be used as the fitWidth.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
    • fitWidthProperty

      public final DoubleProperty fitWidthProperty()
      The width of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic width of the image will be used as the fitWidth.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
      See Also:
    • setFitHeight

      public final void setFitHeight(double value)
      Sets the value of the property fitHeight.
      Property description:
      The height of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic height of the image will be used as the fitHeight.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
    • getFitHeight

      public final double getFitHeight()
      Gets the value of the property fitHeight.
      Property description:
      The height of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic height of the image will be used as the fitHeight.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
    • fitHeightProperty

      public final DoubleProperty fitHeightProperty()
      The height of the bounding box within which the source image is resized as necessary to fit. If set to a value <= 0, then the intrinsic height of the image will be used as the fitHeight.

      See preserveRatio for information on interaction between image view's fitWidth, fitHeight and preserveRatio attributes.

      Default value:
      0
      See Also:
    • setPreserveRatio

      public final void setPreserveRatio(boolean value)
      Sets the value of the property preserveRatio.
      Property description:
      Indicates whether to preserve the aspect ratio of the source image when scaling to fit the image within the fitting bounding box.

      If set to true, it affects the dimensions of this ImageView in the following way

      • If only fitWidth is set, height is scaled to preserve ratio
      • If only fitHeight is set, width is scaled to preserve ratio
      • If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
      If unset or set to false, it affects the dimensions of this ImageView in the following way
      • If only fitWidth is set, image's view width is scaled to match and height is unchanged;
      • If only fitHeight is set, image's view height is scaled to match and height is unchanged;
      • If both are set, the image view is scaled to match both.
      Note that the dimensions of this node as reported by the node's bounds will be equal to the size of the scaled image and is guaranteed to be contained within fitWidth x fitHeight bonding box.
      Default value:
      false
    • isPreserveRatio

      public final boolean isPreserveRatio()
      Gets the value of the property preserveRatio.
      Property description:
      Indicates whether to preserve the aspect ratio of the source image when scaling to fit the image within the fitting bounding box.

      If set to true, it affects the dimensions of this ImageView in the following way

      • If only fitWidth is set, height is scaled to preserve ratio
      • If only fitHeight is set, width is scaled to preserve ratio
      • If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
      If unset or set to false, it affects the dimensions of this ImageView in the following way
      • If only fitWidth is set, image's view width is scaled to match and height is unchanged;
      • If only fitHeight is set, image's view height is scaled to match and height is unchanged;
      • If both are set, the image view is scaled to match both.
      Note that the dimensions of this node as reported by the node's bounds will be equal to the size of the scaled image and is guaranteed to be contained within fitWidth x fitHeight bonding box.
      Default value:
      false
    • preserveRatioProperty

      public final BooleanProperty preserveRatioProperty()
      Indicates whether to preserve the aspect ratio of the source image when scaling to fit the image within the fitting bounding box.

      If set to true, it affects the dimensions of this ImageView in the following way

      • If only fitWidth is set, height is scaled to preserve ratio
      • If only fitHeight is set, width is scaled to preserve ratio
      • If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
      If unset or set to false, it affects the dimensions of this ImageView in the following way
      • If only fitWidth is set, image's view width is scaled to match and height is unchanged;
      • If only fitHeight is set, image's view height is scaled to match and height is unchanged;
      • If both are set, the image view is scaled to match both.
      Note that the dimensions of this node as reported by the node's bounds will be equal to the size of the scaled image and is guaranteed to be contained within fitWidth x fitHeight bonding box.
      Default value:
      false
      See Also:
    • setSmooth

      public final void setSmooth(boolean value)
      Sets the value of the property smooth.
      Property description:
      Indicates whether to use a better quality filtering algorithm or a faster one when transforming or scaling the source image to fit within the bounding box provided by fitWidth and fitHeight.

      If set to true a better quality filtering will be used, if set to false a faster but lesser quality filtering will be used.

      The default value depends on platform configuration.

      Default value:
      platform-dependent
    • isSmooth

      public final boolean isSmooth()
      Gets the value of the property smooth.
      Property description:
      Indicates whether to use a better quality filtering algorithm or a faster one when transforming or scaling the source image to fit within the bounding box provided by fitWidth and fitHeight.

      If set to true a better quality filtering will be used, if set to false a faster but lesser quality filtering will be used.

      The default value depends on platform configuration.

      Default value:
      platform-dependent
    • smoothProperty

      public final BooleanProperty smoothProperty()
      Indicates whether to use a better quality filtering algorithm or a faster one when transforming or scaling the source image to fit within the bounding box provided by fitWidth and fitHeight.

      If set to true a better quality filtering will be used, if set to false a faster but lesser quality filtering will be used.

      The default value depends on platform configuration.

      Default value:
      platform-dependent
      See Also:
    • setViewport

      public final void setViewport(Rectangle2D value)
      Sets the value of the property viewport.
      Property description:
      The rectangular viewport into the image. The viewport is specified in the coordinates of the image, prior to scaling or any other transformations.

      If viewport is null, the entire image is displayed. If viewport is non-null, only the portion of the image which falls within the viewport will be displayed. If the image does not fully cover the viewport then any remaining area of the viewport will be empty.

      Default value:
      null
    • getViewport

      public final Rectangle2D getViewport()
      Gets the value of the property viewport.
      Property description:
      The rectangular viewport into the image. The viewport is specified in the coordinates of the image, prior to scaling or any other transformations.

      If viewport is null, the entire image is displayed. If viewport is non-null, only the portion of the image which falls within the viewport will be displayed. If the image does not fully cover the viewport then any remaining area of the viewport will be empty.

      Default value:
      null
    • viewportProperty

      public final ObjectProperty<Rectangle2D> viewportProperty()
      The rectangular viewport into the image. The viewport is specified in the coordinates of the image, prior to scaling or any other transformations.

      If viewport is null, the entire image is displayed. If viewport is non-null, only the portion of the image which falls within the viewport will be displayed. If the image does not fully cover the viewport then any remaining area of the viewport will be empty.

      Default value:
      null
      See Also:
    • getClassCssMetaData

      public static List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
      Gets the CssMetaData associated with this class, which may include the CssMetaData of its superclasses.
      Returns:
      the CssMetaData
      Since:
      JavaFX 8.0
    • getCssMetaData

      public List<CssMetaData<? extends Styleable,?>> getCssMetaData()
      This method should delegate to Node.getClassCssMetaData() so that a Node's CssMetaData can be accessed without the need for reflection.
      Specified by:
      getCssMetaData in interface Styleable
      Overrides:
      getCssMetaData in class Node
      Returns:
      the CssMetaData associated with this class, which may include the CssMetaData of its super classes.
      Since:
      JavaFX 8.0