Loading an image with ImageIcon for a static context/class

Hi, really simple, here's what I want to do:
I have "public class Global" with static variables inside, then I want to load an image with ImageIcon but the image doesn't load, see the code:
import javax.swing.ImageIcon;
 
public class Global {
    public static int test = 5;
 
    public static ImageIcon[] images = new ImageIcon[Main.NOMBRE_ELEMENTS];
    static
    {
        images[Main.MONSTRE1] = new ImageIcon("/Interface/monstre1.jpg");
        images[Main.MONSTRE2] = new ImageIcon("/Interface/monstre2.jpg");
        images[Main.HERO] = new ImageIcon("/Interface/hero.jpg");
        images[Main.GRASS] = new ImageIcon("/Interface/grass.jpg");
    }
}
 
// Later in the code, if I write:
System.out.println( test );
System.out.println( Global.images[0].getWidth() );
// , then I get '-1' since the image did not load, but the variable 'test' shows up correctly

Open in new window

Dimitri04Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Actually i get so used to people using ImageIcon to load images here when the icon isn't required, i thought that's what you might be doing. Let's stick with your original code:

>> images[Main.MONSTRE1] = new ImageIcon("/Interface/monstre1.jpg");

That will try to load the image from the root of your file system in /Interface, which i suspect is not what you intend. If Interface is at your package root, try instead
images[Main.MONSTRE1] = new ImageIcon(Global.class.getResource("/Interface/monstre1.jpg"));

Open in new window

0
 
CEHJCommented:
I would use ImageIO
images[Main.MONSTRE1] = ImageIO.read(new File("/Interface/monstre1.jpg"));

Open in new window

0
 
Dimitri04Author Commented:
I tried it, but it did not work, see code for what I tried:
// Says Inconvertible types:
images[Main.MONSTRE1] = (ImageIcon) (ImageIO.read(new File("/Interface/monstre1.jpg"));
 
// Says Incompatible types
images[Main.MONSTRE1] = ImageIO.read( new File("/Interface/monstre1.jpg") );
 
// Says unreported exception
images[Main.MONSTRE1] = new ImageIcon( ImageIO.read( new File("/Interface/monstre1.jpg") ) );

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
You need to change

>>public static ImageIcon[] images = new ImageIcon[Main.NOMBRE_ELEMENTS];

to
public static Image[] images = new Image[Main.NOMBRE_ELEMENTS];

Open in new window

0
 
Dimitri04Author Commented:
Well, I can't I must stick with ImageIcon because later in the project I am using:
lbl.setIcon( images[Main.MONSTRE1] );

Open in new window

0
 
CEHJCommented:
You can then do
  	
 
lbl.setIcon(new ImageIcon(images[Main.MONSTRE1]));

Open in new window

0
 
objectsCommented:
I don't see any need to complicate things by using ImageIO. where are the images located?

0
 
Dimitri04Author Commented:
I just can't make it work CEHJ, ImageIO doesn't seem to work well for what I wat to do.

The images are located in the source folder, so all I need is import them. It is working well in a non-static context, but as soon as I am in a static context, it's not working, i.e. images not showing, getWidth() = -1
0
 
Dimitri04Author Commented:
Thanks!! exactly what I needed
0
 
CEHJCommented:
:-)
0
All Courses

From novice to tech pro — start learning today.