Hi,
I have two ImageIcons and I want to create a third ImageIcon which has nr 2 drawn upon nr 1. How would I best do that?
Hi,
I have two ImageIcons and I want to create a third ImageIcon which has nr 2 drawn upon nr 1. How would I best do that?
The following code takes an Image
from two ImageIcon
s and creates a new ImageIcon
.
The image from the second ImageIcon
is drawn on top of the image from the first, then the resulting image is used to make a new ImageIcon
:
Image img1 = imageIcon1.getImage();
Image img2 = imageIcon2.getImage();
BufferedImage resultImage = new BufferedImage(
img1.getWidth(null), img1.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resultImage.createGraphics();
g.drawImage(img1, 0, 0, null);
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon resultImageIcon = new ImageIcon(resultImage);
Edit (Fixed some errors, added transparency support.)
For allowing transparency, the BufferedImage.TYPE_INT_ARGB
can be used for the image type in the constructor, rather than BufferedImage.TYPE_INT_RGB
which does not have an alpha channel.