Java Utililty Methods ImageIcon

List of utility methods to do ImageIcon

Description

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

Method

ImageIconcreateMonoColoredImageIcon(final Paint paint, final int width, final int height)
Creates a rectangular mono colored image.
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
final Graphics2D g = image.createGraphics();
g.setPaint(paint);
final int lineHeight = 4;
g.fill3DRect(0, height / 2 - lineHeight / 2, width, lineHeight, false);
g.dispose();
return new ImageIcon(image);
ImageIconcreateNonCachedImageIcon(File file)
ImageIcon's constructors use MediaTracker to load an image, which is cached.
try {
    return new ImageIcon(ImageIO.read(file));
} catch (IOException e) {
    e.printStackTrace();
    return null;
ImageIconcreateOverlayIcon(Icon baseIcon, ImageIcon overlayIcon)
create Overlay Icon
BufferedImage result = new BufferedImage(baseIcon.getIconWidth(), baseIcon.getIconHeight(),
        BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = result.createGraphics();
baseIcon.paintIcon(null, g2, 0, 0);
Image overlay = overlayIcon.getImage();
int w = 12;
int h = 12;
int x = baseIcon.getIconWidth() - w;
...
ImageIconcreateScaledImageIcon(byte[] albumArtBytes)
create Scaled Image Icon
if (albumArtBytes == null) {
    return null;
Image image = new ImageIcon(albumArtBytes).getImage();
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.getGraphics();
graphics.drawImage(image, 0, 0, null);
...
ImageIconCreateSizedImageIconScaledSmooth(URL filePath, int width, int height)
Scales the provided image to the new height/width
ImageIcon newImageIcon = new ImageIcon(filePath);
Image image = newImageIcon.getImage();
image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
newImageIcon = new ImageIcon(image);
return newImageIcon;
FilecreateTempImage(ImageIcon icon, File oldFile)
create Temp Image
String extension = icon.getDescription();
extension = extension.substring(extension.lastIndexOf(".") + 1);
try {
    File ret = File.createTempFile("cdb", "." + extension);
    ret.deleteOnExit();
    BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bi.createGraphics();
...
voiddisplayImage(final ImageIcon ii)
Display a given image in a frame (for debuging purpose).
final JFrame jf = new JFrame();
jf.add(new JLabel(ii));
jf.pack();
jf.setVisible(true);
voiddrawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)
draw Image Border
int left = ins.left;
int right = ins.right;
int top = ins.top;
int bottom = ins.bottom;
int x = rect.x;
int y = rect.y;
int w = rect.width;
int h = rect.height;
...
voiddrawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)
Draws a border based on an image.
int left = ins.left;
int right = ins.right;
int top = ins.top;
int bottom = ins.bottom;
int x = rect.x;
int y = rect.y;
int w = rect.width;
int h = rect.height;
...
ImageIconfitIcon(ImageIcon icon, int containerWidth, int containerHeight)
fit Icon
if (icon.getIconWidth() > containerWidth) {
    double rate = icon.getIconWidth() / (double) containerWidth;
    int height = (int) (icon.getIconHeight() / rate);
    int width = (int) (icon.getIconWidth() / rate);
    icon = new ImageIcon(getScaledImage(icon, width, height));
} else if (icon.getIconHeight() > containerHeight) {
    double rate = icon.getIconHeight() / (double) containerHeight;
    int height = (int) (icon.getIconHeight() / rate);
...