Nate
Nate

Reputation: 35

Java issue loading image

for some reason i cannot load certain images with my java program. just above this snippet of code i have another image referral that works fine.

for(int x = 1; x<=7; x++){
        if(additionals[x] != 0){
            rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+x+".GIF")));
            count++;
        }
    }

images are saved in a folder named IMAGES and are called image1.gif, image2.gif etc if you need the rest of my code just ask

Upvotes: 0

Views: 139

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

Are you sure that this:

    if (additionals[0] != 0){
        rightPanel.add(new JLabel(new ImageIcon("IMAGES/ram"+additionals[0]+"gb.gif")));
        count++;
    }
    for(int x = 1; x<=7; x++){
        if (additionals[x] != 0){
            rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+x+".gif")));
            count++;
        }
    }

shouldn't really be this?

    if (additionals[0] != 0){
        rightPanel.add(new JLabel(new ImageIcon("IMAGES/ram"+additionals[0]+"gb.gif")));
        count++;
    }
    for(int x = 1; x<=7; x++){
        if (additionals[x] != 0){
            rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+ additionals[x]+".gif")));
            count++;
        }
    }

?

It would make your code appear more symmetric. Otherwise, do printlns before the offending line with the String that you want to use to make the ImageIcon to be sure that it's correct.

For example:

    for(int x = 1; x<=7; x++){
        if (additionals[x] != 0){
            String imagePath = "IMAGES/image"+x+".gif";
            System.out.println("imagePath = " + imagePath);
            rightPanel.add(new JLabel(new ImageIcon(imagePath)));
            count++;
        }
    }

And then compare the Strings that are output with the file names and paths. Even better is to create a new File and output its full path before trying to use it to create a new ImageIcon.

caveat: code has not been tested.

Upvotes: 1

Sergey Grinev
Sergey Grinev

Reputation: 34478

  1. is it windows? GIF != gif otherwise
  2. does it work if you remove condition (additionals[x] != 0)?
  3. is rightPanel big enough for all images?

Upvotes: 2

Related Questions