combine Images - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

combine Images

Demo Code

/**/*from  w  ww. j a  v  a2s . com*/
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Main{
    private static final Logger log = Logger.getLogger(className);
    private static BufferedImage combineImages(BufferedImage i1,
            BufferedImage i2) {

        if (i1 == null || i2 == null) {
            log.warning("null parameter");
            return null;
        }

        int width = i1.getWidth();
        int height = i1.getHeight();

        if (width != i2.getWidth() || height != i2.getHeight()) {
            log.warning("image dimensions not equal");
            return null;
        }

        BufferedImage i3 = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = i3.createGraphics();
        g2d.drawImage(i1, null, 0, 0);
        g2d.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 1.0F));
        g2d.drawImage(i2, null, 0, 0);
        g2d.dispose();

        return i3;
    }
}

Related Tutorials