2015-01-08 13 views
0

내 문제는 과거의 텍스처를 HashMap의 위치로 키로 저장하는 리소스 로더를 만드는 중입니다. 패키지 외부의 이미지를로드 할 때 완벽하게 작동하지만 내부 이미지를로드하려고하면 저장되지 않습니다. 내가 외부 로딩이 완벽하게 문제 자사의 단지 내부로드를 작동 말했듯이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; 
} 

내 코드입니다. BufferedImages가 문제가 아니라는 것을 알기 때문에 모든 이미지를 올바르게로드합니다. 그래서 그 이미지가 HashMaps라고 생각합니다.

+0

예외가 있습니까? –

+0

번호 그냥 이상한 – Minegeek360

+0

디버거는 내부에있는 것을 맵에 넣는 지점에 무엇을 보여줄까요? –

답변

1

지도에 데이터를 입력하면 접두어 "CLASS_"이 (가) 키로 사용되는 위치에서 제거됩니다.

그러나지도에서 데이터를 쿼리하는 경우 접두사가 계속 존재합니다.

이 코드를 내부 부품으로 시험 해보고 콘솔에서 출력 해 주시겠습니까?

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); 
    } 
} 
+0

그게 아니야. 여러 가지 방법으로 테스트 해봤는데지도에 추가하지 않았습니다. – Minegeek360

0

하중 아무것도가 추가 결코 극복! 비록 그것이 내 코드에서, 그것은 아무것도하지 않습니다!

의심의 여지가 있습니다.

당신과 같이 내부적으로로드 된 이미지를 확인 :

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); 

당신은 확실히 이미지를 저장하지만, 이전에로드 된 내부 이미지에 대한 테스트는 나중에 다른 키를 사용하여 저장하기 때문에 성공하지 못합니다 (모든 모양이 "CLASS_" 인 상태로 제거됨).

+0

나는 코드를 변경했다. 위치를로드 할 때 System.out.println ("loading location |"+ location); 저장하면 System.out.println ("NEW |"+ location); 그 결과는 다음과 같습니다 : NEW | CLASS_textures/tiles/blocks/grass.png 로케이션 위치 | CLASS_textures/tiles/blocks/grass.png 신규 | CLASS_textures/tiles/blocks/grass.png 로케이션 위치 | CLASS_textures/tiles/blocks/grass.png – Minegeek360

0

문제는 동일한 로더의 인스턴스가 여러 개있는 것이 었습니다. 그래서 방금 내 파일에 하나의 인스턴스 만 액세스하도록 만들었습니다.