The list of methods to do ImageIcon Create are organized into topic(s).
ImageIcon
createImageIcon(Class clazz, String path) Returns an ImageIcon, or throws a RuntimeException if the path was invalid.
URL imageUrl = clazz.getResource(path);
if (imageUrl != null) {
return new ImageIcon(imageUrl);
throw new RuntimeException("Couldn't find file: " + path);
ImageIcon
createImageIcon(Class clazz, String pathToImage, int sizeX, int sizeY) create Image Icon
ImageIcon ret = null;
if (pathToImage != null) {
try {
InputStream is = clazz.getResourceAsStream(pathToImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
...
ImageIcon
createImageIcon(File f, String description) Load BufferedImage form file, scale it and converts to icon.
try {
BufferedImage i = ImageIO.read(f);
return new ImageIcon(i.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
} catch (Exception ex) {
return null;
ImageIcon
createImageIcon(final Icon icon) Creates an ImageIcon from any Icon.
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.createGraphics();
icon.paintIcon(new JLabel(), g, 0, 0);
g.dispose();
return new ImageIcon(bufferedImage);
ImageIcon
createImageIcon(final String src) create Image Icon
if (!new File(src).exists()) {
return new ImageIcon();
} else {
try {
return new ImageIcon(ImageIO.read(new File(src)));
} catch (final Throwable e) {
return new ImageIcon();
ImageIcon
createImageIcon(String iconFile) Returns an ImageIcon, or null if the iconFile is invalid.
if (iconFile != null) {
return new ImageIcon(iconFile);
} else {
System.err.println("Couldn't find file: " + iconFile);
return null;