user2407444
user2407444

Reputation: 33

Java Game Images

I am making a game in Java and I have a class which loads images like so

class ImageLoader {
     public static ImageIcon image_1 = new ImageIcon(this.class.getResource(file_name_of_image);
     public static ImageIcon image_2 = new ImageIcon(this.class.getResource(file_name_of_image);
     public static ImageIcon image_3 = new ImageIcon(this.class.getResource(file_name_of_image);
     public static ImageIcon image_4 = new ImageIcon(this.class.getResource(file_name_of_image);

    public Image getImage1() {
        return image_1.getImage();
    }
}

I call them like

// paint method
g.draw(ImageLoader.getImage1()...);

or

setIcon(ImageLoader.image_1);

My problem is that when my program runs, there is a high CPU usage, which I assume is from having to load this images on every repaint and also I should mention, the images may not be the same size as their components so some scaling needs to be done.

Should I be using a BufferedImage for this? Or may someone explain to me what I may be doing wrong, or what the problem is?

Upvotes: 1

Views: 1002

Answers (1)

arcy
arcy

Reputation: 13103

I think your high CPU must be coming from somewhere else. Once these static variables are loaded, they are not loaded a second time, regardless of repaint. There could be high cpu at the start of your program while it loads them once, but I don't see any reason it would load them a second time.

Upvotes: 4

Related Questions