I have a JavaFX BlueJ project. Following is the simple directory structure
As you can see that I have a Gui.java
and map.png
files in same directory. Following is my code
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Dragon Castle");
Game game = new Game();
TextArea logArea = new TextArea();
VBox vbox = new VBox(10);
// Create maps
Canvas canvas = new Canvas(770, 630);
Image map = new Image("map.png"); // Here it is giving error
GraphicsContext gc = canvas.getGraphicsContext2D();
// Insert controls
VBox controlsVbox = new VBox(5);
controlsVbox.setAlignment(Pos.CENTER);
HBox buttonHboxR1 = new HBox(5);
buttonHboxR1.setAlignment(Pos.CENTER); .... Other code
Error:
Exception in Application start method
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$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$7/22058848.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1099)
at javafx.scene.image.Image.<init>(Image.java:608)
at src.Gui.start(Gui.java:46)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$57/5668924.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$53/812813.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$55/19277439.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$54/26973244.run(Unknown Source)
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$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$43/21354624.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1091)
... 16 more
Image not found. I tried same code in eclipse, its working. Also I put map.png
in resources folder but its still not working in BlueJ.
From the
Image
documentation:So, since you pass a simple path, the
Image
constructor will attempt to resolve that relative to the classpath. Looking at the stack trace, yourGui
class appears to be in a package calledsrc
(really???) and your image is in the same package. Hence the path to the image should besrc/map.png
.A better way to retrieve the image from the same package as the current class is to get the URL from the class object as a resource: