1

I have 30 ImageIcons that I have declared in a ImageIcon array. I have declared them all using a FOR loop. THe only problem is that when I try to add the ImageIcon to a JLabel and display the JPanel on the screen, it doesn't work.Here is my code:

package screens;

import javax.swing.*;

import java.awt.*;

public class gameScreen extends JPanel {
        private static final long serialVersionUID = 1L;
        // -------------VARIABLES---------------//
        Image wallpaper  = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/wallpaper.jpg"));


        ImageIcon[] teamsImg = new ImageIcon[30];
        public gameScreen() {

        for(int i = 0;i>30;i++){
            teamsImg[i] = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
        getClass().getResource("images/img.png")));


         }

         JLabel label = new JLabel(teamsImg[27]);
         add(label);

    }

// -------------PAINT FUNCTION----------//
public void paintComponent(Graphics g) {
     g.drawImage(wallpaper,0,0,null);



}

}
3
  • Please, please, please, always call super.paintXxx, the paint methods do a lot of important work that you should not neglect unless you a have a very, Very, VERY good reason to. ps I doubt that's your problem, but that just annoys me :P Commented Oct 4, 2012 at 0:36
  • What exactly do you mean by 'it doesn't work' - do you get an error, or is it just that nothing is visible? Do the images exist? Are they actually loaded? Why are you you loading the same image 30 times, and then using it only once? Is the 'gameScreen' instance a non-zero size? Oh, and class names should start with capital letters. Commented Oct 4, 2012 at 0:41
  • nothing is visible. and the images aren't loaded. i checked using a if stament Commented Oct 4, 2012 at 0:49

1 Answer 1

3

I would take a really long, hard look at this line...

for (int i = 0; i > 30; i++) {

As Adam Savage would say, "well, there's your problem".

Personally, I would use ImageIO over Toolkit.getImage, unless you have a really good reason not to (like you need the background loading capabilities of Toolkit.getImage due to slow network connection). ImageIO will give you access to a much wider range of possible image formats, thanks to it's plugin architecture as well as guarantee that when it returns, the image will fully loaded.

Something like...

try {
    wallpaper = ImageIO.read(getClass().getResource("images/wallpaper.jpg"));

    for (int i = 0; i < 30; i++) {

        teamsImg[i] = ImageIO.read(getClass().getResource("images/img.png"));

    }
} catch (IOException iOException) {
    System.err.println("Bad things happened");
    iOException.printStackTrace();
}

This will require to use Image instead of ImageIcon to store the images, but it's a little sacrifice for the benefit it brings you.

Also, you do know, you're loading the same image over and over again??? You could just load it once and set the reference to each element in the array, it's effectively the same thing...(as I understand it, the image API is using a cached version to make repeated loads quicker)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Look at the line in your code, then look at the sample code I provided using ImageIO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.