How can I load Computer Directory images in JAVAFX

孤者浪人 提交于 2019-11-29 06:58:02

It is not clear exactly what you are trying to do. If you are talking about JavaFX 2.0, the following code works. If you are loading a lot of images and need to conserve memory, you only have to create enough ImageView's for the number you want to display at one time. Then as you page through the images, you can swap out the Image object contained in the ImageView.

public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);

    File file = new File("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    Image image = new Image(file.toURI().toString());
    ImageView iv = new ImageView(image);

    root.getChildren().add(iv);
    primaryStage.setScene(scene);
    primaryStage.show();
}

its simple: open a image with a browser and copy the whole url of the pic and paste it as a parameter of the Image object. DO NOT REMOVE "file///:" because this makes the image loadable. you will get your other logic from there. happy coding e.g

Image image = new Image("file:///C:/Users/Nigel/Desktop/my_image.png");
ImageView imgview = new ImageView(image);

sorry if my answer came a little bit late but that was my approach and it works 100% wherever you are putting your file 1.assign your image to File 2.parse the File to URI 3.assign the uri.toString() to the image

ex code:

File imageFile = new File("path/to/image/outside/your/jar/awesomeless.jpg");
String fileLocation = imageFile.toURI().toString();
Image fxImage = new Image(fileLocation);    

or you can simply put it all together like this:

Image fxImage = new Image(new File("path/.../awesomemore.jpg").toURI().toString());

and please if anyone knows a better approach let us know!

Another solution is to pass the InputStream into the Image class constructor; and is working...

public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    StackPane root = new StackPane();
    Scene scene = new Scene(root, 300, 250);

    java.io.FileInputStream fis = new FileInputStream("/System/Library/CoreServices/loginwindow.app/Contents/Resources/LogOut.png");
    ImageView iv = new ImageView(new Image(fis));

    root.getChildren().add(iv);
    primaryStage.setScene(scene);
    primaryStage.show();
}

None of the previous answers worked for me. However, this one, which I saw a while back but can't find an original link from, works flawlessly.

@Override
public void start(Stage primaryStage) throws Exception {
    //create a pane to hold the image views
    Pane pane = new HBox(10);
    pane.setPadding(new Insets(5,5,5,5));

    //create the image to be used!
    Image image = new Image("/Content/vortex.jpg");

    //set some custom properties and add an image
    ImageView imageView = new ImageView(image);
    imageView.setFitHeight(100);
    imageView.setFitWidth(100);
    pane.getChildren().add(imageView);

    //add the second image view with this image and no custom properties
    pane.getChildren().add(new ImageView(image));

    ImageView imageView2 = new ImageView(image);
    imageView2.setRotate(45);
    pane.getChildren().add(imageView2);

    Scene scene = new Scene(pane, 200, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

The key is that when you create the image, START the path with a '/' (ie: "/Content/vortex.jpg"). Note that in this setup, the root folder is the src folder in most IDEs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!