0

I recently started learning JavaFX and my class was given the following assignment:

Write a program that displays four images in a grid pane.

Here is my code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class Ch_14_Ex_1 extends Application {
    @Override //Override the start method in the Application class
    public void start(Stage primaryStage) {
    //Create a pane to hold the image views
    GridPane pane = new GridPane();

    //Place nodes in the pane
    pane.add(new ImageView(new Image("image/ukFlag1.gif")), 0, 0);
    pane.add(new ImageView(new Image("image/canadaFlag1.gif")), 1, 0);
    pane.add(new ImageView(new Image("image/chineseFlag1.gif")), 0, 1);
    pane.add(new ImageView(new Image("image/usaFlag1.gif")), 1, 1);

    //Create a scene and place it in the stage
    Scene scene = new Scene(pane);
    primaryStage.setTitle("Ch_14_Ex_1"); //Set the stage title
    primaryStage.setScene(scene); //Place the scene in the stage
    primaryStage.show(); //Display the stage
  }
}

The program compiles fine but whenever I try to run the program I get the following error and i'm not sure how to fix it.

Exception in Application start method
Exception in thread "JavaFX BlueJ Helper" java.lang.RuntimeException: 
Exception in Application start method
at  com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 

at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found

at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:620)
at Ch_14_Ex_1.start(Ch_14_Ex_1.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 11 more
gogov
  • 9
  • 5
  • 1
    make sure images are available to javafx code. See next questions for details: https://stackoverflow.com/questions/10575410/where-does-javafx-scene-image-imageflower-png-look-for-flower-png/10575646#10575646 and https://stackoverflow.com/questions/16099427/cannot-load-image-in-javafx – Sergey Grinev Aug 27 '18 at 15:28
  • Make sure the images in the right directory src->image . – Mazen Embaby Aug 27 '18 at 15:57

1 Answers1

-1

You can set the resources folder in your project in case you wanna wrap it in your jar file and load them like so
new Image(getClass.getResourceAsStream("image/yourImage.gif"))
OR
If those images are not in your resources you can load them like so
new Image("file:image/yourimage.gif")

Ahmed Emad
  • 674
  • 6
  • 19
  • 1
    I wouldn't recommend doing this. This way you rely on the working directory to be the directory containing the `image` directory. You cannot guarantee this and it usually is better to include the resources in the classpath/jar which is the place where `Image` is looking for the image, if the URL is relative. – fabian Aug 27 '18 at 15:51
  • 1
    he didn't say whether these images is a resources images r if it's a gallery app so these images are off the app resources – Ahmed Emad Aug 27 '18 at 16:38
  • If you want to use files as source, it would probably be better to [use the `File` class to get the `URL`](https://stackoverflow.com/a/6098521/2991525) – fabian Aug 27 '18 at 17:41
  • I think it's the same as the second solution – Ahmed Emad Aug 27 '18 at 18:15