Map does not save values put into it

danpelican

New Member
I'm creating a game and storing my images in a \[code\]Map\[/code\] which also has a \[code\]TreeMap\[/code\] inside it. I am trying to make my \[code\]getImage\[/code\] method to check the map and see if an Image's specific key is stored. If it is, load the image early, if not load the image from the resource package and store the key into the Map so that next time the \[code\]getImage\[/code\] method is called it will load that image early because the key is already in the Map. The problem is that the Map will not save the key after it's put into it. I am specifically calling the same image so that I should receive a console output of \[code\]Image Loaded.\[/code\] when it references the images key to the Map. Here is the class with the console output of the tests I set to show it's not saving. \[code\] 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; } }\[/code\]Here is the console output when run:\[code\] {} 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}\[/code\]Is there a way I can have it save the values and reference the Map correctly?
 
Back
Top