0

我正在创建一个游戏并将我的图像存储在Map其中也有一个TreeMap里面。我正在尝试使用我的getImage方法来检查地图并查看是否存储了图像的特定密钥。如果是,则提前加载图像,如果不从资源包中加载图像并将密钥存储到 Map 中,以便下次getImage调用该方法时,它将提前加载该图像,因为密钥已经在 Map 中。

问题是 Map 在放入后不会保存密钥。我专门调用了同一个图像,以便我应该收到一个控制台输出,Image Loaded.说明它何时将图像键引用到 Map。这是我设置的测试的控制台输出的类,以显示它没有保存。

    package game;

    import java.awt.Image;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.TreeMap;
    import javax.imageio.ImageIO;

    public class ImageLoader {

        public Image loadedImage;
        private Map<String, Image> images;

        public ImageLoader(){
            TreeMap<String, Image> map  = new TreeMap<String, Image>();
            images = map;

        }

        public Image getImage(String s){
            System.out.println(images);
            try
            {

                if(images.containsKey(s)){
                    System.out.println("Loaded image.");
                    return images.get(s);
                }
                else
                {
                    System.out.println("Not loaded.");
                    ClassLoader myLoader = this.getClass().getClassLoader();
                    InputStream imageStream = myLoader.getResourceAsStream(s);
                    loadedImage = ImageIO.read(imageStream);
                    images.put(s, loadedImage);
                    System.out.println(images);
                    return images.get(s);
                }
            }
            catch (IOException e)
            {
                System.out.println ("Could not load: " + e);
            }
            return null;
        }


    }

这是运行时的控制台输出:

    {}
    Not loaded.
    {resources/Asteroid.jpg=BufferedImage@1632847: type = 5 ColorModel: #pixelBits = 24         numComponents = 3 color space = java.awt.color.ICC_ColorSpace@e95a56 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 600 height = 600 #numDataElements 3 dataOff[0] = 2}
    {}
    Not loaded.
    {resources/Asteroid.jpg=BufferedImage@1fc6e42: type = 5 ColorModel: #pixelBits = 24         numComponents = 3 color space = java.awt.color.ICC_ColorSpace@e95a56 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 600 height = 600 #numDataElements 3 dataOff[0] = 2}
    {}
    Not loaded.
    {resources/Asteroid.jpg=BufferedImage@8ee016: type = 5 ColorModel: #pixelBits = 24         numComponents = 3 color space = java.awt.color.ICC_ColorSpace@e95a56 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 600 height = 600 #numDataElements 3 dataOff[0] = 2}
    {}
    Not loaded.
    {resources/Alien-Ship.png=BufferedImage@1fe1feb: type = 6 ColorModel: #pixelBits = 32         numComponents = 4 color space = java.awt.color.ICC_ColorSpace@e95a56 transparency = 3 has alpha = true isAlphaPre = false ByteInterleavedRaster: width = 45 height = 45 #numDataElements 4 dataOff[0] = 3}

有没有办法让它保存值并正确引用地图?

4

1 回答 1

0

我怀疑你ImageLoader每次都会得到一个新的。

于 2013-04-05T21:12:22.780 回答