Java ImageIcon Merge mergeIcons(ImageIcon bottom, ImageIcon top)

Here you can find the source of mergeIcons(ImageIcon bottom, ImageIcon top)

Description

Merge the two given icons in one, drawing bottom under top .

License

Open Source License

Parameter

Parameter Description
bottom The Icon drawn at the bottom
top The Icon drawn at the top

Return

THe merged Icon.

Declaration

public static ImageIcon mergeIcons(ImageIcon bottom, ImageIcon top) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    /**/*from w  w w. j  a  va  2 s.c o m*/
     * Merge the two given icons in one, drawing {@code bottom} under {@code top}.
     * @param bottom The Icon drawn at the bottom
     * @param top The Icon drawn at the top
     * @return THe merged Icon.
     */
    public static ImageIcon mergeIcons(ImageIcon bottom, ImageIcon top) {
        if (bottom == null) {
            return top;
        }
        if (top == null) {
            return bottom;
        }
        BufferedImage image = new BufferedImage(Math.max(bottom.getIconWidth(), top.getIconWidth()),
                Math.max(bottom.getIconHeight(), top.getIconHeight()), BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.drawImage(bottom.getImage(), 0, 0, null);
        g.drawImage(top.getImage(), 0, 0, null);
        return new ImageIcon(image);
    }
}