简体   繁体   中英

Simplest way to add an image in Javafx?

I don't understand is how to add a simple image. I imported everything and followed what they said on this page:

http://www.java2s.com/Code/Java/JavaFX/LoadajpgimagewithImageanduseImageViewtodisplay.htm

Javafx code

import javafx.application.Application;
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;

public class test 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(test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));

        selectedImage.setImage(image1);

        root.getChildren().addAll(selectedImage);

        scene.setRoot(root);

        stage.setScene(scene);
        stage.show();
    }

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

Error

    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Input stream must not be null
    at javafx.scene.image.Image.validateInputStream(Image.java:1110)
    at javafx.scene.image.Image.<init>(Image.java:694)
    at prototype.test.start(test.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)

Class.getResourceAsStream tries to load an element in the classpath. It's not meant to load files unless they are included in the classpath. To load a file outside of the classpath, use a FileInputStream instead:

Image image1 = new Image(new FileInputStream("C:\\Users\\user\\Desktop\\x.jpg"));

Or use the Image(String) constructor and pass the URL of the file:

Image image1 = new Image(new File("C:\\Users\\user\\Desktop\\x.jpg").toURI().toURL().toExternalForm());

As fabian mentioned, Class.getResourceAsStream(...) loads an InputStream from the classpath, (meaning, from the jar file itself). To load a file from your computer, you can simply use the file protocol and give the path to the Image constructor, like so:

Image image1 = new Image("file:///C:/Users/user/Desktop/x.jpg");

The reason that you were getting:

Caused by: java.lang.NullPointerException: Input stream must not be null

in your error is because Class.getResourceAsStream(...) simply returns null when it can't find the resource that you tell it to look for. The Image constructor was receiving null and then throwing an exception.

Your problem seems to be this line:

Image image1 = new Image(test.class.getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));

Because you get a NullPointerException: java.lang.NullPointerException: Input stream must not be null .

See:

Try adding image to GridPane

ImageView img1 = new ImageView(new 
Image(getClass().getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg")));
    GridPane.setConstraints(img1, 0, 4);

I think a good way to fix this is dragging your image to the project folder. That way you can search for the name alone

Image image1 = new Image(test.class.getResourceAsStream("x.jpg"));

if this does'nt work add File:

Your problem is at line getResourceAsStream("C:\\\\Users\\\\user\\\\Desktop\\\\x.jpg")); Replace "\\\\" to "/", your problem is solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM