I'm working on a simple "Space Invaders" game using JavaFx, and while my game works fine without any exceptions or errors when I run the project inside.netbeans, it keeps giving me this error when I run the.jar file through CMD and I can't seem to find a solution.
//this is an example of how i define my images/imagepattern
Image il=new Image("file:ship.png");
setFill(new ImagePattern(il));
// this is the error
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Image must be non-null
at com.sun.prism.paint.ImagePattern.<init>(ImagePattern.java:44)
at com.sun.javafx.tk.quantum.QuantumToolkit.createImagePatternPaint(QuantumToolkit.java:905)
at com.sun.javafx.tk.Toolkit.getPaint(Toolkit.java:657)
at javafx.scene.paint.ImagePattern.acc_getPlatformPaint(ImagePattern.java:291)
at javafx.scene.paint.Paint$1.getPlatformPaint(Paint.java:51)
at javafx.scene.shape.Shape.updatePGShape(Shape.java:916)
at javafx.scene.shape.Shape.impl_updatePeer(Shape.java:965)
at javafx.scene.shape.Rectangle.impl_updatePeer(Rectangle.java:541)
at javafx.scene.Node.impl_syncPeer(Node.java:503)
at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2304)
at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2313)
at javafx.scene.Scene$ScenePulseListener.synchronizeSceneNodes(Scene.java:2280)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2419)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$408(QuantumToolkit.java:319)
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$152(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
EDIT: after some debugging i have found that the.jar looks for the images in C:\Users\Admin instead of within the project itself, how can i change that?
I believe that the image should be in the same directory as your java project or declare a package of icons inside your project. eg: /icons/monimage.png
in the source directory otherwise your access path will tighten
Image il=new Image("/icon/monimage.png");
setFill(new ImagePattern(il));
public static void SetImage(ImageView imgV, String patch) throws FileNotFoundException {
File file = new File(patch);
Image image = new Image(file.toURI().toString(), imgV.getFitHeight(), imgV.getFitWidth(), true, true);
imgV.setImage(image);
imgV.setPreserveRatio(true);
}
You can use this function to display an image directly in an ImagaView
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.