使用 Cairo 渲染到 SWING 表面(例如 JPanel)

发布于 2024-12-13 20:15:39 字数 400 浏览 4 评论 0原文

我目前正在开展一个项目,我需要在墨卡托投影的世界地图上绘制卫星的预测足迹,并可能进行缩放/裁剪等。完成地图。

我认为开罗将是一个很好的图书馆用于此目的。有可用的 Java 绑定。然而,我就是找不到一种方法让它渲染到 SWING GUI 上(例如 JPanel 的表面)。我考虑过渲染到字节缓冲区并使用 Java2D 将其逐像素绘制出来,但我找不到任何 API 调用来使 Cairo 渲染到缓冲区中(这很奇怪,因为这是我最基本的功能之一) d 希望得到这样一个库的支持)。

我有什么办法可以实现这个目标吗?我知道有 Java2D,但它相当基础。为此,我非常希望有一个更强大、更广泛、经过充分测试、高质量、免费 (LGPL) 的图形库。如果我能以某种方式让它与 SWING 一起工作,那么 Cairo 将是一个完美的选择。

非常感谢您的建议。

I'm currently working on a project where I need to plot the predicted footprint of a satellite on a mercator-projected world map with possible scaling/cropping/etc. done to the map.

I thought Cairo would be a good library to use for this purpose. There are Java-bindings available for it. However, I just can't find a way to make it render onto a SWING GUI (e. g. onto the surface of a JPanel). I thought about rendering into a byte buffer and plotting it out pixel by pixel using Java2D, but I can't find any API call to make Cairo render into a buffer (which is weird, as this is one of the most fundamental functionalities I'd expect to get supported by such a library).

Any way I can achieve this? I know there is Java2D, but it is fairly basic. I'd really appreciate a more powerful, widespread, well-tested, high-quality, free (LGPL) graphics library for this purpose. Cairo would be such a perfect fit, if I could get it to work with SWING somehow.

Thank you very much for your proposals.

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

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

发布评论

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

评论(2

旧人九事 2024-12-20 20:15:39

Cairo 的基本原则之一是任何非抽象图像上下文都绑定到受支持的后端之一。
我从未尝试过 Java 绑定,但它们很可能是一个薄层,不提供新的表面类型 - 您应该使用“图像表面”类型。

在库的 C 文档中,有一个“cairo_image_surface_get_data()”调用
(此处:http://cairgraphics.org/manual/cairo-Image-Surfaces.html< /a> ),它提供对缓冲区的一次访问权限。
也许由于实际使用其内容所需的低级内存访问,绑定并未将其公开给 Java。如果这是真的,那么我建议您采用以下解决方法:

(1)将结果渲染到开罗图像表面,(2)使用表面内容写入临时文件,(3)读取并显示临时文件Java 2D API。

One of the fundamentals in Cairo is that any non-abstract image context is bound to one of the supported back ends.
I've never tried the Java bindings, but it is likely they are a thin layer, not providing a a new surface type - you should use the "Image Surface" type.

On the C documentation for the Library, there is a "cairo_image_surface_get_data()" call
(here: http://cairographics.org/manual/cairo-Image-Surfaces.html ) which gives one acess to the buffer.
Maybe the bindings didn't expose this to Java due to the low level memory access required to actually use its contents. If that is true, then I propose you the following work-around:

(1)Render your results to a Cairo Image Surface, (2) Write a temporary file with the surface contents,(3) Read and display the temporary file with the Java 2D API.

み青杉依旧 2024-12-20 20:15:39

这是一个示例
我在 http://java-gnome.sourceforge.net 上找到了这个示例
它创建一个 gtk 窗口,实际上是一个 gtk DrawingArea 小部件,onDraw() 事件使用 cairo。
我在linux上编译并运行了它,效果很好。

然而java-gnome似乎只有linux二进制文件。也许有人可以制作一个 Windows 二进制文件,但需要一些工作。

它是一个gtk窗口,所以与swing无关。
如果 gtk(java-gnome) 能够满足您的需求,也许您不需要 swing。

如果必须使用 swing,则可以使用 cairo 渲染内存中的图像,然后通过重写 PaintComponent() 方法等方法将其显示给 JComponent。我不知道表演。

Here is a example.
I found this examples on http://java-gnome.sourceforge.net
It create a gtk window, and actually a gtk DrawingArea widget, the onDraw() event uses cairo.
I compiled and run it on linux, it works good.

however java-gnome seems only have linux binary. Maybe somebody could make a windows binary, but need some work.

It is a gtk window, so have nothing to do with swing.
Maybe you don't need swing if gtk(java-gnome) can fit your needs.

If you must use swing, you can use cairo to render to a image in memory, then show it to JComponent by somthing like overriding paintComponent() method. I don't know the performance.

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