Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
TileImages.java
Go to the documentation of this file.
1 // This file is part of MicropolisJ.
2 // Copyright (C) 2013 Jason Long
3 // Portions Copyright (C) 1989-2007 Electronic Arts Inc.
4 //
5 // MicropolisJ is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GPLv3, with additional terms.
7 // See the README file, included in this distribution, for details.
8 
9 package micropolisj.gui;
10 
11 import java.awt.*;
12 import java.awt.image.*;
13 import java.net.URL;
14 import java.util.*;
15 import javax.swing.*;
16 
17 import micropolisj.engine.*;
18 import static micropolisj.engine.TileConstants.*;
19 
20 public class TileImages {
21  final int TILE_WIDTH;
22  final int TILE_HEIGHT;
23  Image[] images;
24  Map<SpriteKind, Map<Integer, Image>> spriteImages;
25 
26  private TileImages(int size) {
27  this.TILE_WIDTH = size;
28  this.TILE_HEIGHT = size;
29 
30  if(size != 16) {
31  this.images = loadTileImages("/tiles_" + size + "x" + size + ".png", size);
32  }
33  else {
34  this.images = loadTileImages("/tiles.png", 16);
35  }
36 
38  }
39 
40  static Map<Integer, TileImages> savedInstances = new HashMap<Integer, TileImages>();
41 
42  public static TileImages getInstance(int size) {
43  if(!savedInstances.containsKey(size)) {
44  savedInstances.put(size, new TileImages(size));
45  }
46  return savedInstances.get(size);
47  }
48 
49  public Image getTileImage(int cell) {
50  int tile = (cell & LOMASK) % images.length;
51  return images[tile];
52  }
53 
54  private Image[] loadTileImages(String resourceName, int srcSize) {
55  URL iconUrl = TileImages.class.getResource(resourceName);
56  Image refImage = new ImageIcon(iconUrl).getImage();
57 
58  GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
59  GraphicsDevice dev = env.getDefaultScreenDevice();
60  GraphicsConfiguration conf = dev.getDefaultConfiguration();
61 
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);
67 
68  images[i] = bi;
69  }
70  return images;
71  }
72 
73  public Image getSpriteImage(SpriteKind kind, int frameNumber) {
74  return spriteImages.get(kind).get(frameNumber);
75  }
76 
77  private void loadSpriteImages() {
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);
83  if(img != null) {
84  imgs.put(i, img);
85  }
86  }
87  spriteImages.put(kind, imgs);
88  }
89  }
90 
91  Image loadSpriteImage(SpriteKind kind, int frameNo) {
92  String resourceName = "/obj" + kind.objectId + "-" + frameNo;
93 
94  // first, try to load specific size image
95  URL iconUrl = TileImages.class.getResource(resourceName + "_" + TILE_WIDTH + "x" + TILE_HEIGHT + ".png");
96  if(iconUrl != null) {
97  return new ImageIcon(iconUrl).getImage();
98  }
99 
100  iconUrl = TileImages.class.getResource(resourceName + ".png");
101  if(iconUrl == null)
102  return null;
103 
104  if(TILE_WIDTH == 16 && TILE_HEIGHT == 16) {
105  return new ImageIcon(iconUrl).getImage();
106  }
107 
108  // scale the image ourselves
109  ImageIcon ii = new ImageIcon(iconUrl);
110  int destWidth = ii.getIconWidth() * TILE_WIDTH / 16;
111  int destHeight = ii.getIconHeight() * TILE_HEIGHT / 16;
112 
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();
118 
119  gr.drawImage(ii.getImage(), 0, 0, destWidth, destHeight, 0, 0, ii.getIconWidth(), ii.getIconHeight(), null);
120  return bi;
121  }
122 
123 }