Created
December 5, 2013 15:22
-
-
Save jandk/7807363 to your computer and use it in GitHub Desktop.
IconManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cmosis.iconlib; | |
import java.awt.Graphics; | |
import java.awt.image.BufferedImage; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.imageio.ImageIO; | |
import javax.swing.Icon; | |
import javax.swing.ImageIcon; | |
public class IconManager | |
{ | |
private static IconManager instance; | |
public static IconManager instance() | |
{ | |
if (instance == null) | |
instance = new IconManager(); | |
return instance; | |
} | |
private final Map<String, List<String>> iconNames = new HashMap<>(); | |
private final Map<String, BufferedImage> iconSheets = new HashMap<>(); | |
private final Map<String, BufferedImage> cache = new HashMap<>(); | |
private IconManager() | |
{ | |
} | |
public BufferedImage get(final String iconName) | |
{ | |
final IconLocation location = IconLocation.fromString(iconName); | |
if (cache.containsKey(location.toString())) | |
return cache.get(location.toString()); | |
try | |
{ | |
final List<String> names = getIconSheetNames(location.getSetName()); | |
final int index = names.indexOf(location.getIconName()); | |
if (index < 0) | |
throw new IllegalStateException("Icon does not exist: " + location.toString()); | |
final BufferedImage image = getIconFromSheet(location, index); | |
cache.put(location.toString(), image); | |
return image; | |
} | |
catch (IOException ex) | |
{ | |
throw new IllegalStateException("Icon does not exist" + location.toString(), ex); | |
} | |
} | |
public Icon getIcon(final String iconName) | |
{ | |
return new ImageIcon(get(iconName)); | |
} | |
private BufferedImage getIconFromSheet(final IconLocation location, final int index) throws IOException | |
{ | |
final BufferedImage source = getIconSheetImage(location.getSetName()); | |
final int tileWidth = source.getWidth() / 16; | |
final int offsetX = (index % tileWidth) * 16; | |
final int offsetY = (index / tileWidth) * 16; | |
final BufferedImage image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); | |
final Graphics g = image.createGraphics(); | |
g.drawImage(source, 0, 0, 16, 16, offsetX, offsetY, offsetX + 16, offsetY + 16, null); | |
g.dispose(); | |
return image; | |
} | |
private BufferedImage getIconSheetImage(final String sheetName) throws IOException | |
{ | |
if (iconSheets.containsKey(sheetName)) | |
return iconSheets.get(sheetName); | |
final InputStream stream = getClass().getResourceAsStream(sheetName + ".png"); | |
final BufferedImage image = ImageIO.read(stream); | |
iconSheets.put(sheetName, image); | |
return image; | |
} | |
private List<String> getIconSheetNames(final String sheetName) throws IOException | |
{ | |
if (iconNames.containsKey(sheetName)) | |
return iconNames.get(sheetName); | |
final InputStream stream = getClass().getResourceAsStream(sheetName + ".txt"); | |
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); | |
String line; | |
List<String> names = new ArrayList<>(); | |
while ((line = reader.readLine()) != null) | |
names.add(line); | |
iconNames.put(sheetName, names); | |
return names; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment