如何在 JLabel 上绘图?
我想使用 2D Java API 在已有图像的 JLabel 上绘图,然后保存编辑后的图片。
我找不到关于这个特定主题的任何教程,有人有任何代码或参考资料来说明如何做到这一点吗?
I want to use the 2D Java API to draw on a JLabel that already has an image on it and then save the resulting edited picture.
I can't find any tutorials on this specific subject, does anyone have any code or references that show how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重写
JLabel
的paintComponent
方法。它应该首先调用super.paintComponent
,这样您就可以获得JLabel
包含的任何内容,然后添加您自己的绘图代码。应该看起来有点像这样:override the
paintComponent
method of theJLabel
. It should first callsuper.paintComponent
, so you get whatever theJLabel
contains, then add your own drawing code after that. Should look somewhat like this:一种方法是将现有图像渲染为
BufferedImage
,如这个示例所示,将文本覆盖在 标识。图像完成后,使用ImageIO.write()
将其保存为所需的格式。One approach would be to render the existing image and drawing into a
BufferedImage
, as shown in this example that overlays text on a logo. Once the image is complete, useImageIO.write()
to save it in the desired format.