I looked into the HashMap class and found a way to load images with it so that no image is loaded twice. Thanks:)
Here’s my implementation for those interested.
/*
* SpriteBank class
* Desc: Handles the loading of images/sprites.
* Other objects use this class to access/load their images
* so that no image is loaded twice and that all images are handled
* neatly and seperately.
*
* NOTE: Is a Singleton object! ( Can be instantiated once, and only once ).
*/
import java.util.*; // for List and HashMap
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class SpriteBank {
// HashMap - used to store the imagesuniquely as to prevent
// loading the same image twice.
//private HashMap<String,BufferedImage> //HashMap<K,V>
// hm = new HashMap<String,BufferedImage>();
private HashMap<String,BufferedImage> //HashMap<K,V>
hm = new HashMap<String,BufferedImage>();
////
private String path = "gfx/";
//Singleton code -+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-
// Used to holds a reference to ourself (Singleton design)
private static SpriteBank ref = null;
// private Constructor ( Singleton method )
private SpriteBank() {
/* No code required here
* - we just want to make sure that no one can instantiate this class
* without our (itselfs) permission
*/
}
public static synchronized
SpriteBank start() {
// Start/Instantiate the SpriteBank class
if (ref == null) {
// Make sure this class is only instantiated once
// and that it afterwards only references itself
ref = new SpriteBank();
}
// return ref which now points to the instance of SpriteBank we just made
return ref;
}
@Override
public Object clone()
throws CloneNotSupportedException {
// Override the generic clone method ( inherited from Object )
// to prevent cloning ( so there can't be more SpriteBanks than 1
// , not even clones )
throw new CloneNotSupportedException();
// If you try to clone() you'll get tazered with an exception! :P
}
//Singleton code end -+-+-+-+-+-+-+-+-+-+-++-+-+-+-+-+-+-+-+-+-+-+-
// normal code
private synchronized BufferedImage loadSprite(String sprName) {
// Attempts to load sprite sprName
BufferedImage spr = null;
try {
spr = ImageIO.read(new File(path+sprName));
} catch (IOException e) {
// Error occured
return null;
}
return spr;
}
private boolean sprExists(String sprName) {
// checks if a sprite has already been added/loaded
// into the hash map
boolean b = hm.containsKey(sprName);
return b;
}
public synchronized BufferedImage getSprite(String sprName) {
// used by other objects to load/get their image
// that represents their sprite name.
if(sprExists(sprName)) {
// Sprite already loaded.
return hm.get(sprName);
} else {
// Sprite hasn't been loaded yet.
BufferedImage spr;
spr = loadSprite(sprName);
if(spr == null) {
// An error occured, couldn't load sprite.
return null;
} // else move on
/* transform the loaded sprite into an image
* with the top left pixel color as transparent */
spr =
imageToBufferedImage( makeColorTransparent(spr) );
// save the loaded sprite (and now transparent) into the hashmap
hm.put(sprName, spr);
// and then return the sprite
return spr;
}
}
// Transforms a BufferedImage with a transparent color
// into an Image ( use imagetoBufferedImage(Image imgage) method
// to turn it back into a BufferedImage.
private Image
makeColorTransparent(BufferedImage img) {
final Color c = new Color(img.getRGB(0,0)); /* The color to turn transparent
* is in the top left corner
* of the image */
ImageFilter filter = new RGBImageFilter() {
// the color to turn into transparent (opaque)
int markerRGB = c.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if( (rgb | 0xFF000000) == markerRGB) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
} else {
//do nothing
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(img.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
// Turn an Image into a BufferedImage
private BufferedImage imageToBufferedImage(Image image) {
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
/* Source code
http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png
*/
}
public synchronized int size() {
// return the number of images loaded into memory
return hm.size();
}
}