1

我做了一个JLabel像这样显示我的图像的地方:

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

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

例如:

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

上面只画了小图标。

4

1 回答 1

4

调用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));

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

您还可以绘制其他内容,例如画出矩形轮廓填充椭圆形

对于更高级的图形功能,请尝试转换为Graphics2D对象。

于 2012-12-02T05:05:28.573 回答