简体   繁体   English

如何在JLabel上绘图?

[英]How to draw on a 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. 我想使用2D Java API在已经有图像的JLabel上绘制,然后保存生成的编辑图片。

I can't find any tutorials on this specific subject, does anyone have any code or references that show how to do it? 我找不到关于这个特定主题的任何教程,是否有人有任何代码或参考资料显示如何做到这一点?

override the paintComponent method of the JLabel . 覆盖JLabelpaintComponent方法。 It should first call super.paintComponent , so you get whatever the JLabel contains, then add your own drawing code after that. 它应该首先调用super.paintComponent ,这样你就可以获得JLabel包含的任何内容,然后在此之后添加自己的绘图代码。 Should look somewhat like this: 应该看起来像这样:

public void paintComponent(Graphics g){
    super.paintComponent(g)
    g.drawWhatever ...
}

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. 一种方法是将现有图像和绘图渲染为BufferedImage ,如此示例中所示,其覆盖徽标上的文本。 Once the image is complete, use ImageIO.write() to save it in the desired format. 图像完成后,使用ImageIO.write()将其保存为所需的格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM