gpt4 book ai didi

java - 使用 setIcon 重叠图像

转载 作者:行者123 更新时间:2023-11-29 03:39:52 25 4
gpt4 key购买 nike

我制作了一个 JLabel,我在其中显示我的图像,如下所示:

BufferedImage myimage;
imageLabel.setIcon(new ImageIcon(myimage));

是否可以使用 setIcon 命令绘制图像并在其上绘制较小的图像(图标)?我该怎么做?

例如:

BufferedImage myimage1;
BufferedImage myLittleIcon;
imageLabel.setIcon(new ImageIcon(myimage1));
imageLabel.setIcon(new ImageIcon(myLittleIcon));

上面只是画了小图标。

最佳答案

调用 setIcon 会覆盖图标。但是,您可以尝试这样的操作:

// Assumed that these are non-null
BufferedImage bigIcon, smallIcon;

// Create a new image.
BufferedImage finalIcon = new BufferedImage(
bigIcon.getWidth(), bigIcon.getHeight(),
BufferedImage.TYPE_INT_ARGB)); // start transparent

// Get the graphics object. This is like the canvas you draw on.
Graphics g = finalIcon.getGraphics();

// Now we draw the images.
g.drawImage(bigIcon, 0, 0, null); // start at (0, 0)
g.drawImage(smallIcon, 10, 10, null); // start at (10, 10)

// Once we're done drawing on the Graphics object, we should
// call dispose() on it to free up memory.
g.dispose();

// Finally, convert to ImageIcon and apply.
imageLabel.setIcon(new ImageIcon(finalIcon));

这会创建一个新图像,绘制大图标,然后绘制小图标。

你也可以画其他东西,比如outlining a rectanglefilling an oval .

要获得更高级的图形功能,请尝试转换为 Graphics2D目的。

关于java - 使用 setIcon 重叠图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666514/

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