将 JPanel 导出为矢量图形

发布于 2024-07-18 17:15:57 字数 192 浏览 8 评论 0原文

我想将 JPanel 中的图像导出到矢量图形文件,以便可以以高于屏幕的分辨率对其进行编辑和打印。 本质上,我希望使用目标 Graphics 来调用其 paint() 函数,该目标 Graphics 将绘图命令保存到矢量图形文件中。

有什么好的、简单的方法可以做到这一点? 推荐哪些库? 哪种矢量格式最好,为什么?

I would like to export the image in my JPanel to a vector graphics file so it can be edited and printed at a higher-than-screen resolution. Essentially I want its paint() function to be called with a destination Graphics that saves the drawing commands into a vector graphic file.

What is a good, simple way to do this? What libraries are recommended? Which vector format would be best, and why?

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

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

发布评论

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

评论(7

枯叶蝶 2024-07-25 17:15:57

查看Java EPS Graphics2D 包

许多 Java 程序使用 Graphics2D 在屏幕上绘制内容,虽然将输出保存为 png 或 jpeg 文件很容易,但将其导出为 EPS 以包含在文档或纸张中有点困难。

这个包使整个过程变得非常简单,因为您可以像使用 Graphics2D 对象一样使用 EpsGraphics2D 对象。 唯一的区别是,所有实现的方法都会创建 EPS 输出,这意味着您绘制的图表可以调整大小,而不会导致您在调整基于像素的图像(例如 JEPG 和 PNG 文件)大小时可能看到的任何锯齿状边缘。

Have a look at The Java EPS Graphics2D package.

Many Java programs use Graphics2D to draw stuff on the screen, and while it is easy to save the output as a png or jpeg file, it is a little harder to export it as an EPS for including in a document or paper.

This package makes the whole process extremely easy, because you can use the EpsGraphics2D object as if it's a Graphics2D object. The only difference is that all of the implemented methods create EPS output, which means the diagrams you draw can be resized without leading to any of the jagged edges you may see when resizing pixel-based images, such as JEPG and PNG files.

泼猴你往哪里跑 2024-07-25 17:15:57

Apache Batik 将允许您绘制 Graphics2D 对象的专门实现,然后导出为可缩放矢量图形(.svg) 文件。 然后,您可以使用支持 SVG 的浏览器查看/处理/打印它(Firefox 将原生处理它,ISTR、IE 和其他浏览器可以使用插件)。

请参阅 SVGGraphics2D 对象(过程记录此处

Apache Batik will let you paint to a specialised implementation of a Graphics2D object and then export as an scalable vector graphics (.svg) file. You can then view/process/print it using an SVG-enabled browser (Firefox will handle it nativly, ISTR, IE and others can use plugins).

See the SVGGraphics2D object (process documented here)

-残月青衣踏尘吟 2024-07-25 17:15:57

The Java EPS mentioned by Pierre looks good, but if it isn't you might also like to look at FreeHEP Vector Graphics. Written to allow Java reuse in the High Energy Physics field it includes a vector graphics package, done through an implementation of Graphics2D. We've used it to export EPS very successfull for a number of years.

红ご颜醉 2024-07-25 17:15:57

我可以推荐 VectorGraphics2D 库 (LGPL)。 虽然它不支持 Graphics2D 的所有功能,但我在我的项目中成功使用了它。 它提供了各种矢量文件格式的 java.awt.Graphics2D 实现。 它只是将所有绘画操作导出到 EPS、SVG 或 PDF 文件。

I can recommend the VectorGraphics2D library (LGPL). Although it does not support all the features of Graphics2D, I used it successfully for my project. It provides implementations of java.awt.Graphics2D for various vector file formats. It simply exports all paint operations to EPS, SVG, or PDF files.

小嗲 2024-07-25 17:15:57

为具有相同要求的人提供的其他库:

这两个 GPLv3 均通过在 JFreeChart奥森图表

Additional libraries for people with the same requirement:

Both of these GPLv3 and well tested via extensive usage in JFreeChart and Orson Charts.

耀眼的星火 2024-07-25 17:15:57

FreeHEP 似乎运行得很好,尽管它似乎不再被维护,而且它的错误和论坛页面也消失了。 只需几行,您就可以得到一个弹出对话框,可以将任何组件保存为各种可扩展和常规图像格式。 我们有一些具有挑战性的图像,使用 Alpha 通道、旋转文本、由曲线界定的区域,并且它们保存得很好,使用 VectorGraphics2D 效果更好。

到目前为止,我看到的唯一问题是 jpeg 保存,我的所有图像都显示为黑色。 鉴于 png 以及所有矢量模式都可以工作,这对我们来说并不是很重要,但我确信这对某些人来说会是一个问题。

我必须添加这么多代码才能在所有这些模式下保存:

public static void showImage(Component comp) 
{
    try
    {
        ExportDialog export = new ExportDialog();
        export.showExportDialog( null, "Export view as ...", comp, "export" );
        System.err.println("Image save complete");

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

还必须添加一堆库 jar。

FreeHEP seems to work quite well although it does not appear to be maintained anymore and its bug and forum pages are gone. With just a handful of lines you get a popup dialog that can save any component to a variety of scalable and regular image formats. We have some challenging images, using alpha channel, rotated text, areas bounded by curves, and they saved perfectly, much better with with VectorGraphics2D.

The only problem I've seen so far is in jpeg save, which comes out black for all my images. This isn't very important for us given that png works, plus all the vector modes, but I'm sure it would be an issue for some.

I had to add exactly this much code to save in all these modes:

public static void showImage(Component comp) 
{
    try
    {
        ExportDialog export = new ExportDialog();
        export.showExportDialog( null, "Export view as ...", comp, "export" );
        System.err.println("Image save complete");

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

There are a bunch of library jars that must be added as well.

〃温暖了心ぐ 2024-07-25 17:15:57

这基本上是不可能直接实现的,因为低级 java api 以栅格(像素)工作,并且从不以矢量格式存储。
(检查 java.awt.Graphics 的 API 就明白我的意思了)。

有一些通用程序可以将光栅转换为矢量格式,这是我在快速搜索中找到的:
http://autotrace.sourceforge.net/index.html

所以,使用这样的程序你可以将您的问题分成两个较小的问题:

  1. 将您的 JPanel 转换为位图或文件 ( http://www.jguru.com/faq/view.jsp?EID=242020)
  2. 对文件运行自动跟踪。

this is basically not possible directly, as the low level java api works in terms of raster (pixels) and is never stored in vector format.
(Check the API of java.awt.Graphics to see what I mean).

there are some general purpose program that convert raster to vector formats, this is one I found on a quick search:
http://autotrace.sourceforge.net/index.html

so, using such a program you can divide your problem into two smaller problems:

  1. convert your JPanel into a bitmap or file (http://www.jguru.com/faq/view.jsp?EID=242020)
  2. run autotrace on the file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文