lundi 30 mars 2015

java - Image fits and works with jar, but not when using a String[] array to store the image names

Edit: It was helpful to load the images only once in the default constructor, everything works much faster now. The problem, however, has changed. I can't open the jar file anymore, and if I launch it from the console using java -jar BounceTheSphinx.jar I get this



Exception in thread ''main'' java.lang.IllegalArgumentException: input == null!:
at javax.imageio.ImageIO.read<Unknown Source>
at BounceBack.PanneauJeu.<init>(PanneauJeu.java:55)
at BounceBack.FenetreJeu.<init>(FenetreJeu.java:21)
at BounceBack.MainBounceBack.main(MainBounceBack.java:11)


Line 55 from PanneauJeu.java is fondArray[j] = ImageIO.read(this.getClass().getResource(imageArray[j])); I looked on other posts, but I can't solve my problem with the solutions proposed. The thing is, I really use the same technique to load and display those images, those images exist, everything works in eclipse, yet the fondArray one always causes the problem, not the fondPerdu


I edited the code for you to see


So I wrote in the comments ''WORKS'' and ''DOESN'T WORK'' so you can see where my problem is.



public class PanneauJeu extends JPanel
{
private int i = 0;//color counter
private int j = 0;//imageArray counter
private int k = 0;//imagePerdu counter

private String[] imageArray = {"/resources/Sphinx.png", "/resources/Sphinx2.png ", "/resources/Sphinx3.png", "/resources/Sphinx4.png", "/resources/Sphinx5.png", "/resources/Sphinx6.png", "/resources/Sphinx7.png", "/resources/Sphinx8.png"};//score

private String[] imagePerdu = {"/resources/Lose5.png", "/resources/Lose6.png", "/resources/Lose7.png", "/resources/Lose8.png", "/resources/Lose9.png", "/resources/Lose10.png", "/resources/Lose11.png", "/resources/Lose12.png", "/resources/Lose13.png"};//, "Lose10.png", "Loose11.png", "Loose12.png"};
private Image fond;
private Image fondArray[] = new Image[imageArray.length];
private Image fondPerdu[] = new Image[imagePerdu.length];

public PanneauJeu()//default constructor
{
for(int j = 0; j < imageArray.length; j++)
{
//DOESN'T WORK
try
{
fondArray[j] = ImageIO.read(this.getClass().getResource(imageArray[j]));
}catch(IOException e){e.printStackTrace();}
}

for(int k = 0; k < imagePerdu.length; k++)
{
//WORKS
try
{
fondPerdu[k] = ImageIO.read(this.getClass().getResource(imagePerdu[k]));
}catch(IOException e){e.printStackTrace();}
}
}


Can anyone tell me what could possibly be wrong? Remember, everything works just fine in Eclipse.


Thank you everyone for your help


Aucun commentaire:

Enregistrer un commentaire