Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HashMap不保存条目_Java_Hashmap_Textures - Fatal编程技术网

Java HashMap不保存条目

Java HashMap不保存条目,java,hashmap,textures,Java,Hashmap,Textures,我的问题是,我试图制作一个资源加载器,将过去的纹理保存到一个HashMap中,并将它们的位置作为键。当它加载包外的图像时,它可以完美地工作,但当它试图加载内部图像时,它只是无法保存 这是我的资源加载器代码 public Map<String,BufferedImage> loads = new HashMap<String,BufferedImage>(); public BufferedImage loadImage(String imagePath){ B

我的问题是,我试图制作一个资源加载器,将过去的纹理保存到一个HashMap中,并将它们的位置作为键。当它加载包外的图像时,它可以完美地工作,但当它试图加载内部图像时,它只是无法保存

这是我的资源加载器代码

public Map<String,BufferedImage> loads = new HashMap<String,BufferedImage>();

public BufferedImage loadImage(String imagePath){

    BufferedImage temp = new BufferedImage(9, 16, BufferedImage.TYPE_INT_RGB);

    String location = imagePath.replaceAll("[.]", "/");
    location += ".png";

    //internal
    if(location.startsWith("CLASS_")){
        if(loads.get(location) != null){
            System.out.println("OLD");
            return loads.get(location);
        }else{
            location = location.replaceAll("CLASS_", "");
            try{
                temp = ImageIO.read(this.getClass().getClassLoader().getResource("net/minegeek360/platformer/assets/"+location));
                loads.put(location, temp);
            }catch(Exception e){System.err.println("CANT LOAD IMAGE");}
            System.out.println("NEW | "+temp);
        }
    //external
    }else{
        try{
            if(loads.get(location) != null){
                //System.out.println("LOADED ORIGIONAL IMAGE");
                return loads.get(location);
            }else{
                temp = ImageIO.read(new File("assets/textures/"+location));
                //System.out.println("LOADED NEW IMAGE");
            }
        }catch(Exception e){ e.printStackTrace(); }
        loads.put(location, temp);
    }

    return temp;
}
publicmap load=newhashmap();
公共缓冲区图像加载图像(字符串图像路径){
BuffereImage temp=新的BuffereImage(9,16,BuffereImage.TYPE_INT_RGB);
字符串位置=imagePath.replaceAll(“[.]”,“/”);
位置+=“.png”;
//内部的
if(location.startsWith(“CLASS_”)){
if(loads.get(location)!=null){
System.out.println(“旧”);
返回负载。获取(位置);
}否则{
location=location.replaceAll(“CLASS_uu)”;
试一试{
temp=ImageIO.read(this.getClass().getClassLoader().getResource(“net/minegeek360/platformer/assets/”+location));
负载。放置(位置、温度);
}catch(异常e){System.err.println(“无法加载图像”);}
系统输出println(“新的|“+温度);
}
//外部的
}否则{
试一试{
if(loads.get(location)!=null){
//System.out.println(“加载的原始图像”);
返回负载。获取(位置);
}否则{
temp=ImageIO.read(新文件(“资产/纹理/”+位置));
//System.out.println(“加载的新图像”);
}
}catch(异常e){e.printStackTrace();}
负载。放置(位置、温度);
}
返回温度;
}

正如我所说的,外部负载工作得很好,问题出在内部负载上。它正确加载了所有图像,因此我知道BuffereImage不是问题所在,这就是为什么我认为是HashMaps的问题。

当数据放入映射时,前缀“CLASS_389;”将从用作键的位置删除

但是,当从地图查询数据时,前缀仍然存在

您能为内部部件试用此代码并提供控制台的输出吗

if(location.startsWith("CLASS_")){
    location = location.replaceFirst("CLASS_", "");
    if(loads.get(location) != null){
        System.out.println("OLD");
        return loads.get(location);
    } else {
        try{
            temp = ImageIO.read(this.getClass().getClassLoader().getResource("net/minegeek360/platformer/assets/"+location));
            System.out.println("Loading image, current size: " + loads.size());
            loads.put(location, temp);
            System.out.println("Image loaded,  new size:     " + loads.size());
        }catch(Exception e){System.err.println("CANT LOAD IMAGE");}
        System.out.println("NEW | "+temp);
    }
}

负载从未得到任何添加到它!即使在我的代码中,它什么也不做

怀疑

检查内部加载的图像,如下所示:

if(location.startsWith("CLASS_")){
    if(loads.get(location) != null){
如果找不到,则按如下方式加载:

        location = location.replaceAll("CLASS_", "");
        try{
            temp = ImageIO.read(this.getClass().getClassLoader().getResource("net/minegeek360/platformer/assets/"+location));
            loads.put(location, temp);

您当然会存储图像,但对以前加载的内部图像的测试将永远不会成功,因为您存储图像时使用的键(所有外观均已删除)与您稍后尝试使用的键不同。

问题是,我有同一加载程序的多个实例。所以我只让我的文件只访问一个实例。

你得到了一个异常?调试器在你将()内部实例放入映射时显示了什么?加载永远不会得到任何添加到它的内容!即使在我的代码中,它什么也不做!你试过从地图上打印出所有对吗?当我使用System.out.println(“SAVED IMAGE |”+loads.get(location));在代码末尾,它返回图像。当我在代码开头使用它时,它返回null!!不是这样的。我已经用很多不同的方法对它进行了测试,但它没有将它添加到地图中。我现在已经更改了代码。加载位置时,它执行System.out.println(“加载位置|”+位置);当它保存时,它会执行System.out.println(“NEW |”+location);结果是:新的| CLASS|u纹理/tiles/blocks/grass.png加载位置| CLASS|u纹理/tiles/blocks/grass.png新的| CLASS|u纹理/tiles/blocks/grass.png加载位置| CLASS|u纹理/tiles/blocks/grass.png