简体   繁体   English

Java游戏图像

[英]Java Game Images

I am making a game in Java and I have a class which loads images like so 我正在用Java制作一个游戏,我有一个像这样加载图像的类

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. 我的问题是,当我的程序运行时,CPU使用率很高,我认为这是因为必须在每次重绘时加载这些图像而且我应该提到,图像的大小可能与它们的组件大小不同所以需要一些扩展需求要完成。

Should I be using a BufferedImage for this? 我应该使用BufferedImage吗? Or may someone explain to me what I may be doing wrong, or what the problem is? 或者有人可以向我解释我可能做错了什么,或者问题是什么?

I think your high CPU must be coming from somewhere else. 我认为你的高CPU必须来自其他地方。 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. 程序启动时可能会有很高的CPU加载它们一次,但我没有看到任何理由它会再加载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM