📄 imageloader.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.common.resources;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
/** Loads images to create icons.
* @author Federico Granata
* Date: 4-giu-2003
* Time: 17.14.13
* @author Bertoli Marco
* added caching support (11-lug-2005)
*/
public class ImageLoader {
// Used to cache already used icons (this speeds up a lot)
protected static HashMap iconcache = new HashMap();
/** Loads the image from this directory, please put all images in the class
* package.
*
* @param imageName string containing the image name
* @return the icon of the image
*
* Modified by Bertoli Marco to support caching
*/
public static ImageIcon loadImage(String imageName) {
String internalName = imageName + ".gif";
if (!iconcache.containsKey(internalName)) {
java.net.URL imageURL = ImageLoader.class.getResource(internalName);
if (imageURL == null) {
return null;
} else {
ImageIcon tmp = new ImageIcon(imageURL);
iconcache.put(internalName, tmp);
return tmp;
}
}
else
return (ImageIcon)iconcache.get(internalName);
}
/** Loads the image from this directory, please put all images in the class
* package.
*
* @param imageName string containing the image name
* @param size specific size for the image to be returned
* @return the icon of the image
*
*/
public static ImageIcon loadImage(String imageName, Dimension size){
ImageIcon im = ImageLoader.loadImage(imageName);
if(im != null) {
Image scaled = im.getImage();
scaled = scaled.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH);
return new ImageIcon(scaled);
}else return im;
}
/** Loads the image from this directory, please put all images in the class
* package.
*
* @param imageName string containing the image name
* @return the image
*/
public static Image loadImageAwt(String imageName) {
String internalName = imageName + ".gif";
Image image = Toolkit.getDefaultToolkit().getImage(ImageLoader.class.getResource(internalName));
if (image == null) {
return null;
} else {
return image;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -