dimanche 29 mars 2015

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

I made a game that works just fine in Eclipse, but not as a jar. I know many people have trouble putting their images (*.png) in the jar file, but that is NOT my case. When I start the game, the background image is there, so I have no problems about that.


The problem is that at some point, I show many images after another, so that it makes like a little movie, and this works just fine in Eclipse. But when I export it as a jar, everything works until the animation, where the whole game, except the music, freezes.


Which is weird, because I use fond = ImageIO.read(this.getClass().getResource(imageString)); both for the image and the animation, except I use an array for the animation, like this: fond = ImageIO.read(this.getClass().getResource(imageArray[j]));


So I wrote in the comments ''here are the arrays'', ''this works fine!'' and ''this doesn't'' (3x) so you can see where my problem is (use Ctrl+F to spare your time!)



package BounceBack;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class PanneauJeu extends JPanel
{
//déclaration des variables d'instances
private int posXrectangle = 500/2 - 80/2;
private int posYrectangle = 500;
//utilisé pour la balle, mais aussi pour définir les sommets du triangle d'animation de victoire
private int posAovale = 500/2 - 40/2;
private int posBovale = 30 - 40/2;
private boolean score = false;
private boolean perdu = false;
private boolean victoire = false;
private int i = 0;//compteur pour les couleurs
private int j = 0;//compteur pour les images score
private int k = 0;//compteur pour les images perdu (loose)
private int increment = 0;//compteur pour la victoire
private int upperBound = 15, lowerBound = 0;
private int p = 0;//coordonnées axe y de l'image
private Random random = new Random();

//here are the arrays

private String imageString = "/resources/Sphinx.png";
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/Loose5.png", "/resources/Loose6.png", "/resources/Loose7.png", "/resources/Loose8.png", "/resources/Loose9.png", "/resources/Loose10.png", "/resources/Loose11.png", "/resources/Loose12.png", "/resources/Loose13.png"};//, "Loose10.png", "Loose11.png", "Loose12.png"};
private Image fond;

//constructeur par défaut

//constructeur avec paramètres

//accesseurs
public int getPosXrectangle()
{
return this.posXrectangle;
}

public int getPosYrectangle()
{
return this.posYrectangle;
}

public int getPosAovale()
{
return this.posAovale;
}

public int getPosBovale()
{
return this.posBovale;
}

public int getIncrement()
{
return this.increment;
}

//mutateurs
public void setPosXrectangle(int posXrectangle)
{
this.posXrectangle = posXrectangle;
}

public void setPosAovale(int posAovale)
{
this.posAovale = posAovale;
}

public void setPosBovale(int posBovale)
{
this.posBovale = posBovale;
}

public void setScore(boolean score)
{
this.score = score;
}

public void setPerdu(boolean perdu)
{
this.perdu = perdu;
}

public void setVictoire(boolean victoire)
{
this.victoire = victoire;
}


//méthodes

public void paintComponent (Graphics g)
{
final Color[] color = {Color.blue, Color.yellow, Color.pink, Color.orange, Color.cyan, Color.MAGENTA, Color.red};//tableau de 7 couleurs

if(!score && !victoire)
{
try
{
fond = ImageIO.read(this.getClass().getResource(imageString));//so this works fine!
}catch(IOException e){e.printStackTrace();}
g.drawImage(fond, 0, 0, 500, 700, this);
g.setColor(Color.RED);
}

if(score && !victoire)
{
try
{
fond = ImageIO.read(this.getClass().getResource(imageArray[j]));//but this doesn't!
}catch(IOException e){e.printStackTrace();}
g.setColor(color[i]);
p = random.nextInt(10);
g.drawImage(fond, 0, p, 500, 700, this);

i++;
j++;
if(i == 6) i = 0;
if(j == 7) j = 0;
}

if(perdu)
{
g.setColor(Color.black);
try
{
if(k == 8) k = 0;
fond = ImageIO.read(this.getClass().getResource(imagePerdu[k]));//but this doesn't!
g.drawImage(fond, 0, 0, 500, 700, this);
k++;
}catch(IOException e){e.printStackTrace();}
}

if(victoire)
{
if(increment < 600)
{
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
try
{
fond = ImageIO.read(this.getClass().getResource(imageArray[j]));//but this doesn't!
}catch(IOException e){e.printStackTrace();}
g.setColor(color[i]);

p = random.nextInt(upperBound - lowerBound) + lowerBound;//(upperbound-lowerbound) + lowerbound;
g.drawImage(fond, 0, p, 500, 700, this);

increment = increment + 3;
i++;
j++;
upperBound = upperBound + 3;
lowerBound = lowerBound + 3;
if(i == 6) i = 0;
if(j == 7) j = 0;
if(increment > 580) victoire = false;
}
}

g.fillOval(posAovale, posBovale, 40, 40);
g.setColor(Color.black);
g.drawOval(posAovale, posBovale, 40, 40);
g.setColor(Color.yellow);
g.fillRect(posXrectangle, posYrectangle, 80, 30);
g.setColor(Color.black);
g.drawRect(posXrectangle, posYrectangle, 80, 30);
}


//classes additionnelles



}//fin classe PanneauJeuContenu


Can anyone tell me why it doesn't work as a jar with the String array defining the images, but yet it does, using the same technique but with a single image? Remember, everything works just fine in Eclipse, so it is no compilation problem.


Thank you everyone for your help


Aucun commentaire:

Enregistrer un commentaire