JavaFX Tutorial - JavaFX Image ImageView








JavaFX can display standard image file formats on the scene graph.

  • Load images with javafx.scene.image.Image from local file system or a remote webserver.
  • Display images using the javafx.scene.image.ImageView node.

Loading Images

To load standard image file formats JavaFX provides the javafx.scene.image.Image API. The Image class has many convenient constructors that facilitate different loading strategies, as shown in the following list:

Image(java.io.InputStream  inputStream)
Image(java.io.InputStream is, double   requestedWidth,  double requestedHeight,  boolean preserveRatio,  boolean smooth)
Image(java.lang.String url)
Image(java.lang.String url, boolean  backgroundLoading)
Image(java.lang.String url, double   requestedWidth,  double requestedHeight,  boolean preserveRatio,  boolean smooth)
Image(java.lang.String url, double   requestedWidth,  double requestedHeight,  boolean preserveRatio,  boolean smooth,   boolean backgroundLoading)

The following table describes each parameter.

ParameterData Type / Description
inputStreamjava.io.InputStream
An input stream such as a file or network.
urlString
An image's URL location.
backgroundLoading
boolean
Loads the image in the background off the JavaFX application thread.
requestWidthdouble
Specifies an image's bounding box width.
requestedHeightdouble
Specifies an image's bounding box height.
preserveRatioboolean
Used to keep an image's aspect ratio within the bounding box.
smoothboolean
True to render smoother but slower; otherwise to render in lower quality but faster.

The following code loads two images, one from local disk and the other from the web.

import java.io.File;
import java.net.MalformedURLException;
/*w ww.  j av a  2  s  .  com*/
import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    try {
      File file = new File("C:/Users/abc/myphoto.jpg");
      String localUrl = file.toURI().toURL().toString();
      // don't load in the background
      Image localImage = new Image(localUrl, false);

      String remoteUrl = "http://java2s.com/style/demo/Firefox.png";
      // load in the background
      Image remoteImage = new Image(remoteUrl, true);

      System.out.println(localUrl);
      System.out.println(remoteUrl);

    } catch (MalformedURLException ex) {
      // error
    }
  }

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




Viewing Images

The ImageView object is a JavaFX Node object which can display images. It can have effects, perform transforms, and scale images.

When an ImageView node is applying special effects such as blurring on an image, the image's pixel data is copied, calculated and displayed onto the ImageView node.

The following code shows how to create an ImageView object.

Image  image  = new Image(url, true); 
ImageView imageView = new ImageView(image);

Full source code

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/*from w ww. j  av  a2s. c om*/
public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 330, Color.WHITE);
        
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);
        
        final ImageView imv = new ImageView();
        final Image image2 = new Image(Main.class.getResourceAsStream("button.png"));
        imv.setImage(image2);

        final HBox pictureRegion = new HBox();
        
        pictureRegion.getChildren().add(imv);
        gridpane.add(pictureRegion, 1, 1);
        
        
        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}




Rotate ImageView

The following code shows how to Rotate ImageView.

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.VBox;
import javafx.stage.Stage;
 //from   w  w  w. j a  v  a2s. c  om
public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTML");
        stage.setWidth(500);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());
        VBox root = new VBox();    

        final ImageView selectedImage = new ImageView();   
        Image image1 = new Image(Main.class.getResourceAsStream("a.jpg"));
        selectedImage.setImage(image1);
        
        selectedImage.setRotate(90);
        
        root.getChildren().addAll(selectedImage);
        
        scene.setRoot(root);
 
        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

setFitWidth for ImageView

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
//from  ww  w.j a  v  a  2 s  .co m
public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 330, Color.WHITE);
        
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);
        
        final ImageView imv = new ImageView();
        imv.setFitWidth(100);
        final Image image2 = new Image(Main.class.getResourceAsStream("button.png"));
        imv.setImage(image2);

        final HBox pictureRegion = new HBox();
        
        pictureRegion.getChildren().add(imv);
        gridpane.add(pictureRegion, 1, 1);
        
        
        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

setPreserveRatio for ImageView

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
//w  ww.j  av a  2 s . c o  m
public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 330, Color.WHITE);
        
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);
        
        final ImageView imv = new ImageView();
        imv.setPreserveRatio(true);
        final Image image2 = new Image(Main.class.getResourceAsStream("button.png"));
        imv.setImage(image2);

        final HBox pictureRegion = new HBox();
        
        pictureRegion.getChildren().add(imv);
        gridpane.add(pictureRegion, 1, 1);
        
        
        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

JavaFX Image Zoom

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage;
/*from w  ww  .ja  v  a 2  s.  c o m*/
public class Main extends Application {

  @Override
  public void start(Stage stage) throws Exception {
    ImageView imageView = new ImageView();
    ScrollPane scrollPane = new ScrollPane();
    DoubleProperty zoomProperty = new SimpleDoubleProperty(200);

    zoomProperty.addListener(new InvalidationListener() {
      @Override
      public void invalidated(Observable arg0) {
        imageView.setFitWidth(zoomProperty.get() * 2);
        imageView.setFitHeight(zoomProperty.get() * 3);
      }
    });
    scrollPane.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
      @Override
      public void handle(ScrollEvent event) {
        if (event.getDeltaY() > 0) {
          zoomProperty.set(zoomProperty.get() * 1.2);
        } else if (event.getDeltaY() < 0) {
          zoomProperty.set(zoomProperty.get() / 1.1);
        }
      }
    });
    imageView.setImage(new Image("http://yourImageURL"));
    imageView.preserveRatioProperty().set(true);
    scrollPane.setContent(imageView);
    stage.setScene(new Scene(scrollPane, 400, 300));
    stage.show();
  }

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