9 package micropolisj.gui;
12 import java.awt.image.*;
17 import micropolisj.engine.*;
18 import static micropolisj.engine.TileConstants.*;
22 final int TILE_HEIGHT;
24 Map<SpriteKind, Map<Integer, Image>> spriteImages;
27 this.TILE_WIDTH = size;
28 this.TILE_HEIGHT = size;
31 this.images =
loadTileImages(
"/tiles_" + size +
"x" + size +
".png", size);
40 static Map<Integer, TileImages> savedInstances =
new HashMap<Integer, TileImages>();
43 if(!savedInstances.containsKey(size)) {
44 savedInstances.put(size,
new TileImages(size));
46 return savedInstances.get(size);
50 int tile = (cell & LOMASK) % images.length;
55 URL iconUrl =
TileImages.class.getResource(resourceName);
56 Image refImage =
new ImageIcon(iconUrl).getImage();
58 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
59 GraphicsDevice dev = env.getDefaultScreenDevice();
60 GraphicsConfiguration conf = dev.getDefaultConfiguration();
62 Image[] images =
new Image[refImage.getHeight(null) / srcSize];
63 for(
int i = 0; i < images.length; i++) {
64 BufferedImage bi = conf.createCompatibleImage(TILE_WIDTH, TILE_HEIGHT, Transparency.OPAQUE);
65 Graphics2D gr = bi.createGraphics();
66 gr.drawImage(refImage, 0, 0, TILE_WIDTH, TILE_HEIGHT, 0, i * srcSize, 0 + srcSize, i * srcSize + srcSize, null);
74 return spriteImages.get(kind).get(frameNumber);
78 spriteImages =
new EnumMap<SpriteKind, Map<Integer, Image>>(SpriteKind.class);
79 for(SpriteKind kind : SpriteKind.values()) {
80 HashMap<Integer, Image> imgs =
new HashMap<Integer, Image>();
81 for(
int i = 0; i < kind.numFrames; i++) {
82 Image img = loadSpriteImage(kind, i);
87 spriteImages.put(kind, imgs);
91 Image loadSpriteImage(SpriteKind kind,
int frameNo) {
92 String resourceName =
"/obj" + kind.objectId +
"-" + frameNo;
95 URL iconUrl =
TileImages.class.getResource(resourceName +
"_" + TILE_WIDTH +
"x" + TILE_HEIGHT +
".png");
97 return new ImageIcon(iconUrl).getImage();
100 iconUrl =
TileImages.class.getResource(resourceName +
".png");
104 if(TILE_WIDTH == 16 && TILE_HEIGHT == 16) {
105 return new ImageIcon(iconUrl).getImage();
109 ImageIcon ii =
new ImageIcon(iconUrl);
110 int destWidth = ii.getIconWidth() * TILE_WIDTH / 16;
111 int destHeight = ii.getIconHeight() * TILE_HEIGHT / 16;
113 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
114 GraphicsDevice dev = env.getDefaultScreenDevice();
115 GraphicsConfiguration conf = dev.getDefaultConfiguration();
116 BufferedImage bi = conf.createCompatibleImage(destWidth, destHeight, Transparency.TRANSLUCENT);
117 Graphics2D gr = bi.createGraphics();
119 gr.drawImage(ii.getImage(), 0, 0, destWidth, destHeight, 0, 0, ii.getIconWidth(), ii.getIconHeight(), null);