Java Utililty Methods ImageIcon Load

List of utility methods to do ImageIcon Load

Description

The list of methods to do ImageIcon Load are organized into topic(s).

Method

ImageIcongetImageIcon(String path, String description)
Returns an ImageIcon, or null if the path was invalid.
if (path == null || path.trim().length() == 0) {
    throw new IllegalArgumentException("Invalid file path: " + path);
URL imgURL = ClassLoader.getSystemResource(path);
if (imgURL != null) {
    return new ImageIcon(imgURL, description);
throw new IllegalArgumentException("Couldn't find file: " + path);
...
ImageIcongetImageIcon(String res)
Retrieves an ImageIcon from the specified resource
try {
    return new ImageIcon(ClassLoader.getSystemResource(res));
} catch (Exception e) {
    e.printStackTrace();
return null;
ImageIcongetImageIcon(String resource)
get Image Icon
URL imageURL = ClassLoader.getSystemResource(resource);
ImageIcon image = new ImageIcon(imageURL);
return image;
ImageIcongetImageIcon(URL u, int w)
get Image Icon
BufferedImage img = null;
BufferedImage tmp = ImageIO.read(u);
ImageIcon icon = null;
if (tmp != null) {
    float h = (float) tmp.getHeight() * (float) w / (float) tmp.getWidth();
    img = scaleImage(w, (int) h, tmp, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    icon = new ImageIcon((Image) img);
return icon;
ImageIcongetImageIcon(URL url)
get Image Icon
ImageIcon icon = (url == null) ? null : (new ImageIcon(url));
return icon;
ImagegetScaledInstance(Image img, int targetWidth, int targetHeight)
Redimensionne une image.
final int type = BufferedImage.TYPE_INT_ARGB;
Image ret = img;
final int width = ret.getWidth(null);
final int height = ret.getHeight(null);
if (width != targetWidth && height != targetHeight) {
    final BufferedImage scratchImage = new BufferedImage(targetWidth, targetHeight, type);
    final Graphics2D g2 = scratchImage.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
...
ImageIconloadImageIcon(byte bytes[], String descr)
load Image Icon
ImageIcon image = new ImageIcon(bytes, descr);
return image;
ImageIconloadImageIcon(Class clas, String iconPath)
Loads a icon from either the file system or from a jar.
ImageIcon icon = null;
URL url = clas.getResource(iconPath);
if (url != null) {
    icon = new ImageIcon(url);
return icon;
ImageIconloadImageIcon(String imageName)
Loads an image from a given path and returns it as an ImageIcon.
return new ImageIcon(loadImage(imageName));
ImageIconloadImageIcon(String url)
load Image Icon
byte[] data = getResourceAsBytes(url);
ImageIcon icon = new ImageIcon(data);
return icon;