📄 resources.java
字号:
package cn.pandaoen.game.minesweeper.res;
import org.apache.log4j.Logger;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* This class is conform to Singleton Pattern.
* The singleton instance manage the resources.
* It support caching.
*
* @author pan
*/
public final class Resources {
/**
* The single instance of this class
*/
public static final Resources res = new Resources();
private Map<String, Image> imageCache = new HashMap<String, Image>();
private Logger logger = Logger.getLogger(Resources.class);
private ResourceBundle resources;
private Locale[] locales = { Locale.ENGLISH, Locale.CHINESE, Locale.GERMAN };
private static final String PREFIX = "cn/pandaoen/game/minesweeper/res/"; //$NON-NLS-1$
public Image getImage(String imageName) {
Image image = imageCache.get(imageName);
if (image != null)
return image;
InputStream is = null;
try {
is = getResourceAsStream("images/" + imageName); //$NON-NLS-1$
image = new Image(Display.getDefault(), is);
imageCache.put(imageName, image);
} catch (Exception e) {
logger.warn(e.toString() + " " + imageName); //$NON-NLS-1$
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
logger.warn(e.toString());
}
}
return image;
}
public String getString(String key) {
if (resources == null) {
try {
resources = ResourceBundle.getBundle(PREFIX + "minesweeper"); //$NON-NLS-1$
} catch (Exception e) {
logger.error(e);
}
}
try {
return resources.getString(key);
} catch (MissingResourceException e) {
logger.warn("String for " + key + "not found"); //$NON-NLS-1$//$NON-NLS-2$
return key;
}
}
public InputStream getResourceAsStream(String filename) {
return Resources.class.getResourceAsStream(filename);
}
public URL getResourceAsURL(String filename) {
return Resources.class.getResource(filename);
}
public void dispose() {
logger.trace("Release images"); //$NON-NLS-1$
for (Image image : imageCache.values()) {
image.dispose();
}
imageCache = null;
}
public Locale[] getLocales() {
return locales;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -