gpt4 book ai didi

java - 绘制两个重叠的图像

转载 作者:行者123 更新时间:2023-12-01 15:01:36 26 4
gpt4 key购买 nike

我正在尝试绘制 2 个图像,一个在另一个之上。第一个图像是一个箭头(在最终图像中应显示为标题)。第一个图像(箭头)为 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 图像,仅绘制第二个图像。

感谢任何帮助。

谢谢!

最佳答案

看起来这里的问题是您最后绘制了 32x32 背景图像,这意味着它将打印在其他图像的顶部,使得 24x24 图像看起来好像根本没有绘制过。

如果交换这两行,您应该会看到两个图像。来自:

g.drawImage(baseImage, 0, 0, null);
g.drawImage(backgroundImage, 0, 0, null);

至:

g.drawImage(backgroundImage, 0, 0, null);
g.drawImage(baseImage, 0, 0, null);


但是,这将在左上角绘制 24x24 图像,而您说过您希望将其绘制在右下角。这可以通过一些基本的减法来完成:

g.drawImage(baseImage, w - baseImage.getWidth(), h - baseImage.getHeight(), null);

关于java - 绘制两个重叠的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13564869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com