java绘制两个重叠图像
我试着画两张图片,一张在另一张上面。第一个图像是一个箭头(在最后一个图像中应该显示为标题)。第一个图像(箭头)为32x32像素,第二个图像为24x24像素
理想情况下,我希望在第一张图片的顶部绘制第二张图片,从第一张图片的右下角开始
目前我正在使用这样的代码
// load source images
BufferedImage baseImage = ImageIO.read(new File(baseImg.getFileLocation()));
BufferedImage backgroundImage = ImageIO.read(new File(backgroundImg.getFileLocation()));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(baseImage.getWidth(), backgroundImage.getWidth());
int h = Math.max(baseImage.getHeight(), backgroundImage.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(baseImage, 0, 0, null);
g.drawImage(backgroundImage, 0, 0, null);
int index = baseImg.getFileLocation().lastIndexOf(".png");
String newFileName = baseImg.getFileLocation().substring(0, index);
// Save as new image
ImageIO.write(combined, "PNG", new File(newFileName + "_combined.png"));
但这对我来说不太管用,因为最终的结果是一个32x32的图像,第二个图像只被绘制
感谢您的帮助
谢谢
# 1 楼答案
看起来这里的问题是,你最后绘制的是32x32背景图像,这意味着它将被打印在另一幅图像的顶部,使其看起来好像24x24图像根本就没有绘制过
如果将这两条线互换,应该可以看到两幅图像。发件人:
致:
然而,这将在左上角绘制24x24图像,而您说希望它位于右下角。这可以通过一些基本的减法来实现: