将 JComponent 中的绘图保存到 Tiff 文件中

发布于 2024-08-07 02:39:56 字数 573 浏览 5 评论 0原文

如何将JComponent中的绘图保存为tiff格式?我只知道如何保存整个Java文件,但不知道如何保存特定的JComponent。帮助我:-(

编辑: 谢谢大家,现在我可以将我的绘图保存为 Jpeg 格式了。

但是我只想保存其中一个组件? c.paintAll(bufferedImage.getGraphics()); 似乎保存了整个组件。但是,我只想保存此组件 c.add(new PaintSurface(), BorderLayout.CENTER); 而不使用 panel.add(saveBtn); 我该怎么做?谢谢。

Container c = getContentPane();
c.setLayout(new BorderLayout());      
Panel panel = new Panel();
panel.add(saveBtn);
c.add("South", panel);
c.setBackground(Color.WHITE);
c.add(new PaintSurface(), BorderLayout.CENTER);

How to save drawing in JComponent into tiff format? I only know how to save the whole Java file but I dont know how to save specific Jcomponent. Help me :-(

EDITED:
Thanks guys, now I am able to save my drawing to Jpeg.

However I just wanted to save one of the component? The c.paintAll(bufferedImage.getGraphics()); seem to save the whole component. However, I just want to save this component c.add(new PaintSurface(), BorderLayout.CENTER); without panel.add(saveBtn); How can I do that? Thanks.

Container c = getContentPane();
c.setLayout(new BorderLayout());      
Panel panel = new Panel();
panel.add(saveBtn);
c.add("South", panel);
c.setBackground(Color.WHITE);
c.add(new PaintSurface(), BorderLayout.CENTER);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥︴琐丶欲为 2024-08-14 02:39:56

这基本上与 broschb 的 解决方案相同,仅使用正确的方法语法并实际调用适当的 JAI 例程。

public void saveComponentAsTiff(Component c, String filename, boolean subcomp) throws IOException {
    saveComponentTiff(c, "tiff", filename, subcomp);
}

public void saveComponent(Component c, String format, String filename, boolean subcomp) throws IOException {
    // Create a renderable image with the same width and height as the component
    BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);

    if(subcomp) {
        // Render the component and all its sub components
        c.paintAll(image.getGraphics());
    }
    else {
        // Render the component and ignoring its sub components
        c.paint(image.getGraphics());
    }

    // Save the image out to file
    ImageIO.write(image, format, new File(filename));
}

可以在此处找到各种函数的文档:

如果您想以 tiff 以外的格式保存,可以使用 ImageIO.getWriterFormatNames() 获取当前所有图像输出格式的列表由 JRE 加载。

更新:如果您对绘制子组件不感兴趣,您可以用 Component.paint(...) 替换对 Component.paintAll(...) 的调用。我更改了示例代码以反映这一点。将 subcomp 设置为 true 并渲染子组件并将其设置为 false 将忽略它们。

This is essentially identical to broschb's solution only using correct syntax and actually calling the appropriate JAI routines.

public void saveComponentAsTiff(Component c, String filename, boolean subcomp) throws IOException {
    saveComponentTiff(c, "tiff", filename, subcomp);
}

public void saveComponent(Component c, String format, String filename, boolean subcomp) throws IOException {
    // Create a renderable image with the same width and height as the component
    BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);

    if(subcomp) {
        // Render the component and all its sub components
        c.paintAll(image.getGraphics());
    }
    else {
        // Render the component and ignoring its sub components
        c.paint(image.getGraphics());
    }

    // Save the image out to file
    ImageIO.write(image, format, new File(filename));
}

Documentation for the various functions can be found here:

If you want to save in a format other than tiff you can use ImageIO.getWriterFormatNames() to obtain a list of all image output formats currently loaded by the JRE.

UPDATE: If you are not interested in painting sub components you can substitute the call to Component.paintAll(...) with Component.paint(...). I have altered the example code to reflect this. Setting subcomp to true with render the subcompnents and setting it to false will ignore them.

苏别ゝ 2024-08-14 02:39:56

ScreenImage 类允许您保存任何组件的图像。

The ScreenImage class allows you to save an image of any component.

贩梦商人 2024-08-14 02:39:56

您可以通过创建面板大小的缓冲图像来获取组件或包含绘图的面板的缓冲图像。然后,您可以将面板内容绘制到缓冲图像上。然后,您可以使用 JAI(Java Advanced Imaging)库将缓冲图像保存为 tiff。您必须在此处查看该文档。

JComponent component;  //this is your already created component
BufferedImage image = new BufferedImage(component.getWidth(),
                                        component.getHeight(),
                                        Bufferedimage.TYPERGB)

Graphics g = image.getGraphics;
component.paintComponent(g);

语法可能略有偏差,我不知道,但这是总体思路。然后,您可以使用 JAI 并将缓冲图像转换为 TIFF。

you can get a Buffered Image of the component, or the panel that contains the drawing by creating a buffered image the size of the panel. You can then paint the panels contents onto the buffered image. You can then use JAI(Java Advanced Imaging) library to save the buffered image as a tiff. You'll have to check the docs on that here.

JComponent component;  //this is your already created component
BufferedImage image = new BufferedImage(component.getWidth(),
                                        component.getHeight(),
                                        Bufferedimage.TYPERGB)

Graphics g = image.getGraphics;
component.paintComponent(g);

The syntax may be slightly off, i'm not at an idea, but that is the general idea. You can then use JAI and convert the buffered image to a TIFF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文