JavaFX Image create from image file path

Description

JavaFX Image create from image file path

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Main extends Application {
  @Override/*from  w  ww. j  a  v a 2s. c o  m*/
  public void start(Stage primaryStage) {
    GridPane pane = new GridPane();
    pane.setAlignment(Pos.CENTER);
    String imagePath = "icon.png";
    
    Image image = new Image(imagePath);
    
    pane.add(new ImageView(image),0,0);
    Scene scene = new Scene(pane, 150, 150);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

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



PreviousNext

Related