JavaFX 2.2中的SVG图像 [英] SVG Image in JavaFX 2.2

查看:329
本文介绍了JavaFX 2.2中的SVG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX 2.2的新手,到目前为止,我找不到在JavaFX 2.2应用程序中显示SVG图像的方法。我看了看Batik,但它没有为我做这个技巧,因为它转换为 BufferedImages 而不是 javafx.ImageView



有没有办法在JavaFX应用程序中显示SVG图像?或者你至少可以从JavaFX导出SVG图像?函数 Node.snapshot()以任何方式帮助吗?

解决方案

< blockquote>

有没有办法在JavaFX应用程序中显示SVG图像?


以下是一些选项:


  1. 创建 WebView 将svg图像加载到WebView的WebEngine中。

  2. e(fx)clipse 项目包括 svg to fxml converter (现在链接死了:()。

  3. 这个 NetBeans插件也转发erts svg to fxml(link now dead :()。

  4. 您可以使用基于awt的库,如蜡染 svgsalamander 并将生成的BufferedImage转换为JavaFX图像。

  5. JavaFX 2.2本身包括对一些最小的支持 SVGPath 字符串(不是完整的SVG规范)。

  6. 您可以编写一个转换器,使用JavaFX Canvas呈现svg GraphicsContext 命令。

  7. FranzXaver 提供了一个SVGLoader,其用法在以下答案中得到了证明:加载SVG文件JavaFX上的按钮




或者你至少可以从javafx导出SVG图像吗?


此功能听起来像是一个JavaFX SceneGraph到svg转换器。虽然理论上可行,但我还没有意识到有人已经创建了这样的工具。


函数Node.snapshot()是否有帮助以任何方式?


Node.snapshot()无法帮助导出svg图像作为svg的JavaFX场景图是基于矢量的格式,节点快照是位映射格式。


我看了看Batik,但是它没有为我做这个技巧,因为它转换为BufferedImages而不是javafx.ImageView。


你可以轻松地在awt之间转换使用 SwingFXUtils 的BufferedImages和javafx图像。


就性能而言nce - 你会推荐哪种方式?


对于复杂的svg,而不是从fxml渲染,你可能会获得更好的性能渲染到位图图像或画布图形上下文。这是因为你的场景图最终会更简单,节点也少得多 - 这意味着javafx运行时的工作量要少得多。对于简单的svgs(生成<1000个节点),它可能无关紧要。


将SVG转换为FXML创建javafx.scene.shape.SVGPath的实例?


部分。完整的 SVG规范涵盖了比基本形状更多的基础。此外,SVG可以执行渐变,效果,动画等。因此,如果源SVG图像包含那些附加项,那么它们也需要转换为FXML(尽管它们可能是最好的,SVG的某些方面,例如脚本很难转换为FXML)。



我的猜测是SVG规范的大小和复杂性是JavaFX核心API当前不包含对完整SVG图像系统的直接支持的原因之一而且,仅提供有限的类似功能,例如SVGPath。与其他理想项目相比,在JavaFX核心API中本地实现SVG的成本相对较低,而且成本相对较低。


如果是不会,#2(和#3)和#5会不一样?


没有。 #2和#3中引用的NetBeans和Eclipse工具比#5中提到的SVGPath工具更具功能性,因为这些工具将SVG规范的其他方面转换为FXML等效项(例如渐变,效果和变换)。 p>

I am new to JavaFX 2.2 and as of now I couldn't find a way to display SVG Images in my JavaFX 2.2 application. I took a look at Batik, but it didn't do the trick for me as it converts to BufferedImages and not to javafx.ImageView.

Is there any way to display an SVG image in an JavaFX application? Or can you at least export an SVG image from JavaFX? Does the function Node.snapshot() help there in any way?

解决方案

Is there any way to display an SVG image in an JavaFX application?

Here are some options:

  1. Create a WebView and load the svg image into the WebView's WebEngine.
  2. The e(fx)clipse project includes an svg to fxml converter (link now dead :().
  3. This NetBeans plugin also converts svg to fxml (link now dead :().
  4. You can use an awt based library like Batik or svgsalamander and convert the resultant BufferedImage to a JavaFX Image.
  5. JavaFX 2.2 natively includes support for some minimal SVGPath strings (not the full SVG spec).
  6. You could write a convertor which renders the svg using JavaFX Canvas GraphicsContext commands.
  7. FranzXaver provides an SVGLoader, whose usage is demonstrated in the answer to: Load SVG file in a Button on JavaFX

Or can you at least export an SVG image from javafx?

This feature sounds like a JavaFX SceneGraph to svg converter. While it is theoretically possible, I am not aware that anybody has created such a tool yet.

Does the function Node.snapshot() help there in any way?

Node.snapshot() will not help with exporting an svg image from a JavaFX scene graph as svg is a vector based format and a node snapshot is a bit mapped format.

I took a look at Batik, but it didn't do the trick for me as it converts to BufferedImages and not to javafx.ImageView.

You can easily convert between awt BufferedImages and javafx images using SwingFXUtils.

In terms of performance - which way would you recommend?

For complex svg's, rather than rendering from fxml, you will probably get better performance rendering to a bitmapped image or canvas graphics context. This is because your scene graph will end up a lot simpler, with far less nodes - which means that the javafx runtime will have a lot less work to do. For simple svgs (< 1000 nodes generated), it probably won't matter too much.

Is converting an SVG to an FXML about creating instances of javafx.scene.shape.SVGPath?

Partially. The full SVG specification covers much more ground than the basic shapes. In addition gradients, effects, animations, etc can be performed by an SVG. So if the source SVG image includes those additional items, then they also need to be converted to FXML (as best they can be, there will be some aspects of SVG such as scripting which would be hard to translate to FXML).

My guess is that the size and complexity of the SVG specification is one of the reasons why no direct support for the full SVG image system is currently included in the JavaFX core API and, instead, only limited similar functionality such as SVGPath is offered. Implementing SVG natively in the JavaFX core API would have been high cost for relatively little reward when compared to other desirable items.

If that is the case, wouldn't #2 (and #3) and #5 be just the same?

No. The NetBeans and Eclipse tools referenced in #2 and #3 were more functional than just the SVGPath facilities mentioned in #5, as those tools converted additional aspects of the the SVG specification to FXML equivalents (e.g. gradients, effects and transforms).

这篇关于JavaFX 2.2中的SVG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆