Tuesday, June 14, 2016

32. Image

An image of Hillary Clinton is used. It is downloaded from Wikipedia article. It is also possible to put the url in the Image constructor.


The downloaded image is put in the src directory. We have to load the image using the Image class, passing the path to the constructor. Next, we create an ImageView node to actually view the image.


package ex32;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
 
public class Ex32 extends Application {
 
    public static void main(String args[]) {
        launch();
    }
        
    @Override
    public void start(Stage primaryStage) {
        
        primaryStage.setTitle( "Example 32. Image" );
        
        Group root = new Group();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        
        Image image = new Image("HillaryClinton.png");
        ImageView imageView = new ImageView(image);
        
        root.getChildren().add(imageView);
        primaryStage.show();
    }
}

This is the output:


No comments:

Post a Comment