我有两个ImageIcons,我想创建第三个在nr 1上绘制nr 2的ImageIcon.我最好怎么做?
以下代码Image从两个ImageIcons中获取并创建一个新的ImageIcon.
第二个ImageIcon图像从第一个图像的顶部绘制,然后生成的图像用于创建一个新图像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);
Run Code Online (Sandbox Code Playgroud)
编辑 (修正了一些错误,增加了透明度支持.)
为了允许透明度,BufferedImage.TYPE_INT_ARGB可以将其用于构造函数中的图像类型,而不是BufferedImage.TYPE_INT_RGB没有alpha通道.