Java ImageIcon Create toImageIcon(File file, int width, int height)

Here you can find the source of toImageIcon(File file, int width, int height)

Description

to Image Icon

License

Apache License

Declaration

public static ImageIcon toImageIcon(File file, int width, int height) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    public static ImageIcon toImageIcon(File file, int width, int height) throws IOException {
        ImageIcon lImageIcon = null;

        BufferedImage lBufferedImage = ImageIO.read(file);
        if (lBufferedImage != null) {
            BufferedImage lBufferedImageResized = resizeImage(lBufferedImage, width, height);
            lImageIcon = new ImageIcon(lBufferedImageResized);
        }//from w  w w.  j a  va  2  s.c o  m

        return lImageIcon;
    }

    private static BufferedImage resizeImage(BufferedImage image, int width, int height) {
        BufferedImage lBufferedImage = new BufferedImage(width, height, BufferedImage.SCALE_SMOOTH);
        Graphics2D lGraphics2D = (Graphics2D) lBufferedImage.createGraphics();
        lGraphics2D.addRenderingHints(
                new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        lGraphics2D.drawImage(image, 0, 0, width, height, null);
        lGraphics2D.dispose();
        return lBufferedImage;
    }
}

Related

  1. createImageIcon(URL url, String description, Integer w, Integer h)
  2. createImageIconJLabel(ClassLoader classloader, String path, String description, String text)
  3. createImageIcons(java.awt.Image... images)
  4. getIconImage(Icon icon)
  5. getIconImage(String path)
  6. toImageIcon(Icon icon)
  7. toImageIcon(URL resource, int width, int height)