1
9 package version29;
10
11 import java.awt.Graphics;
12 import java.awt.GraphicsConfiguration;
13 import java.awt.GraphicsEnvironment;
14 import java.awt.Image;
15 import java.awt.Transparency;
16 import java.awt.image.BufferedImage;
17 import java.awt.image.ImageObserver;
18 import java.net.URL;
19 import javax.imageio.ImageIO;
20
21 public class SpriteCache extends ResourceCache implements ImageObserver{
22
23 protected Object loadResource(URL url) {
24 try {
25 return ImageIO.read(url);
26 } catch (Exception e) {
27 System.out.println("No se pudo cargar la imagen de "+url);
28 System.out.println("El error fue : "+e.getClass().getName()+" "+e.getMessage());
29 System.exit(0);
30 return null;
31 }
32 }
33
34 public BufferedImage createCompatible(int width, int height, int transparency) {
35 GraphicsConfiguration gc =
36 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
37 BufferedImage compatible = gc.createCompatibleImage(width,height,transparency);
38 return compatible;
39 }
40
41 public BufferedImage getSprite(String name) {
42 BufferedImage loaded = (BufferedImage)getResource(name);
43 BufferedImage compatible = createCompatible(loaded.getWidth(),loaded.getHeight(),Transparency.BITMASK);
44 Graphics g = compatible.getGraphics();
45 g.drawImage(loaded,0,0,this);
46 return compatible;
47 }
48
49 public boolean imageUpdate(Image img, int infoflags,int x, int y, int w, int h) {
50 return (infoflags & (ALLBITS|ABORT)) == 0;
51 }
52 }
53