Java 图像图标+;ImageIcon=ImageIcon
我有两个ImageIcon,我想创建第三个ImageIcon,它在nr1上绘制nr2。Java 图像图标+;ImageIcon=ImageIcon,java,graphics,Java,Graphics,我有两个ImageIcon,我想创建第三个ImageIcon,它在nr1上绘制nr2。 我最好怎么做?下面的代码从两个ImageIcon中获取一个图像,并创建一个新的ImageIcon 第二个图像图标的图像绘制在第一个图像的顶部,然后生成的图像用于制作新的图像图标: Image img1 = imageIcon1.getImage(); Image img2 = imageIcon2.getImage(); BufferedImage resultImage = new BufferedIma
我最好怎么做?下面的代码从两个
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);
编辑
(修复了一些错误,增加了透明度支持。)
为了允许透明度,构造函数中的图像类型可以使用
BuffereImage.TYPE_INT_ARGB
,而不是没有alpha通道的BuffereImage.TYPE_INT_RGB
。以下代码从两个图像图标
中获取一个图像
,并创建一个新的图像图标
第二个图像图标
的图像绘制在第一个图像的顶部,然后生成的图像用于制作新的图像图标
:
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);
编辑
(修复了一些错误,增加了透明度支持。)
为了允许透明度,构造函数中的图像类型可以使用
buffereImage.TYPE_INT_ARGB
,而不是没有alpha通道的buffereImage.TYPE_INT_RGB
。非常接近,新的图像似乎有一个黑色的背景,而不是一个透明的改变了类型为类型_4BYTE_ABGR和照顾阿尔法。很多感谢非常接近,新的图像似乎有一个黑色的背景,而不是一个透明的改变了类型为类型_4BYTE_ABGR,这照顾到了阿尔法。非常感谢