/**ImageLoader.java - handles the loading of images. * This class is only owned by ImageManager */ package graphicsManager; import java.awt.image.BufferedImage; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Graphics2D; import java.io.IOException; import javax.imageio.*; /** * @author Sam * */ public class ImageLoader { //private static String imgDir = "Images/"; private GraphicsConfiguration gc; public ImageLoader() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); } // ------------------- Image Loading ------------------ /** loads an image using ImageIO. * Translates the image into a managed image before returning. * @param filename the name of the image to be loaded * @return the loaded (managed) image */ public SpriteImage loadImageFromFile(String filename) { try { BufferedImage buffImage = ImageIO.read( getClass().getResource(filename) ); //not a managed image //retrieve information about the transparency int transparency = buffImage.getColorModel().getTransparency(); //create a managed buffered image BufferedImage copy = gc.createCompatibleImage( buffImage.getWidth(), buffImage.getHeight(), transparency ); // create a graphics context Graphics2D g2d = copy.createGraphics(); // g2d.setComposite(AlphaComposite.Src); // copy image g2d.drawImage(buffImage,0,0,null); g2d.dispose(); return new SpriteImage(copy, 0, 0); } catch(IOException e) { System.out.println("Load Image error for " + filename + ":\n" + e); return null; } } public SpriteImage loadImageFromFile(String filename, int x, int y) { SpriteImage sprite = loadImageFromFile(filename); sprite.setLocation(x, y); return sprite; } }