Problème affichage image fichier .jar éxécutable

Résolu
Ikky33 -  
ikky33 Messages postés 43 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour, j'ai fini de créer un petit projet réalisé en une soirée qui est un pierre feuille ciseaux graphique en utilisant javafx, cependant quand j'exporte le projet dans mon bureau et que je le lance, l'image qui correspond au fond écran du jeux ne s'affiche pas, j'ai regardé plusieurs forum sur le même type de problème mais je ne comprends pas trés bien, pouvez vous m'aider svp merci, voici une partie de mon code dans laquelle je vous montre comment je créer l'image :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
Image g1 = new Image("file:fond.jpg");
ImageView iVp = new ImageView(g1);
iVp.setLayoutX(0);
iVp.setFitWidth(850);
iVp.setFitHeight(650);

root.getChildren().addAll(iVp, quit, niv1, niv2, niv3, pierre, feuille, ciseaux, retour, p, g, perdu,
pierre1, feuille1, ciseaux1, pierre2, feuille2, ciseaux2, c, c1, c2);

primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
primaryStage.setTitle("Pierre_Feuille_Ciseaux by ikky33");






Configuration: Windows / Chrome 76.0.3809.132
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

Selon la JavaDoc de javafx.scene.image.Image, on a plusieurs manières d'utiliser le paramètre
String url
:

import javafx.scene.image.Image;

// load an image in background, displaying a placeholder while it's loading
// (assuming there's an ImageView node somewhere displaying this image)
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png", true);

// load an image and resize it to 100x150 without preserving its original
// aspect ratio
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

// load an image and resize it to width of 100 while preserving its
// original aspect ratio, using faster filtering method
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

// load an image and resize it only in one dimension, to the height of 100 and
// the original width, without preserving original aspect ratio
// The image is located in the current working directory
Image image4 = new Image("file:flower.png", 0, 100, false, false);

Dans ton code, avec
new Image("file:fond.jpg");
tu utilises la 4è méthode, celle du répertoire courant, il faudrait plutôt utiliser la 1è ou 2è méthode (en s'assurant que l'image soit bien dans le jar).
0
Ikky33
 
Bonjour, merci de m'avoir répondu, j'ai donc fais ceci mais un message d'erreur s'affiche dans la console :


// Image g1 = new Image("file:fond.jpg");
Image image1 = new Image("fond.jpg", true);
ImageView iVp = new ImageView(image1);
iVp.setLayoutX(0);
iVp.setFitWidth(850);
iVp.setFitHeight(650);
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020 > Ikky33
 
Quelle erreur ?

De plus, ici tu n'es ni dans le 1er cas (tu devrais commencer par /) ni dans le 2è cas (avec des / intermédiaires)
0
Ikky33
 
J'ai mis un / mais un message d'erreur s'affiche :

Image image1 = new Image("/fond.jpg", true);
ImageView iVp = new ImageView(image1);
iVp.setLayoutX(0);
iVp.setFitWidth(850);
iVp.setFitHeight(650);


code d'erreur :

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:636)
 at application.Main.start(Main.java:31)
 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
 at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
 at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
 at java.security.AccessController.doPrivileged(Native Method)
 at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
 at javafx.scene.image.Image.validateUrl(Image.java:1110)
 ... 11 more
0
ikky33 Messages postés 43 Date d'inscription   Statut Membre Dernière intervention  
 
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020 > ikky33 Messages postés 43 Date d'inscription   Statut Membre Dernière intervention  
 
Est-ce que ton image est dans le Jar ?

Vu ton code, tu peux la mettre à côté de Main.class dans src/application puis faire
new Image("application/fond.jpg", true);
0