的Graphics2D和GraphicsContext之间的互操作性 [英] Interoperability between Graphics2D and GraphicsContext

查看:235
本文介绍了的Graphics2D和GraphicsContext之间的互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与正在瞄准Java中的图形渲染器一组工作。此刻,我想弄清楚是否针对 <一个href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html\">java.awt.Graphics2D会前向兼容。也就是说,像JavaFX的显示器可以在其他库中的的Graphics2D 实例呈现?有没有之间的 <一个互操作性href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html\">java.awt.Graphics2D javafx.scene.canvas.GraphicsContext?

I am working with a group which is looking to target a graphics renderer in Java. At the moment I am trying to figure out whether targeting java.awt.Graphics2D will be forward compatible. Namely, can other libraries like JavaFX display renders from a Graphics2D instance? Is there interoperability between java.awt.Graphics2D and javafx.scene.canvas.GraphicsContext?

或者,如果没有之间的的Graphics2D GraphicsContext 直接,是有显示的的Graphics2D 的任何方式呈现在JavaFX应用程序类似于说Swing中显示的Graphics2D在面板呈现?

Or, if not between Graphics2D and GraphicsContext directly, is there any means of displaying a Graphics2D render in a JavaFX application analogous to say Swing displaying Graphics2D renders in Panels?

如果从这个问题不是很明显,我是相当新的Java生态系统。欲了解更多背景下,我发现在互联网搜索最多的事就是例子/教程解释如何在事情做AWT可以JavaFX中进行(见的条),不回答我的问题,但它使我猜想,有没有互操作性。不过,我希望有人比较熟悉的生态系统可以直接回答这个问题。

If not obvious from the question, I'm fairly new to the Java ecosystem. For more context, most things that I'm finding in internet searches are examples/tutorials explaining how things done in AWT can be done in JavaFX (see Example Article), which doesn't answer my question, but does lead me to conjecture that there is no interoperability. However, I'm hoping someone more familiar with the ecosystem can answer the question outright.

推荐答案

有没有内置的互操作性AWT之间的的Graphics2D 和一个JavaFX <一个href=\"http://docs.oracle.com/javafx/2/api/javafx/scene/canvas/GraphicsContext.html\">GraphicsContext,他们是完全独立的UI工具包完全独立的API。

There is no built-in interoperability between an awt Graphics2D and a JavaFX GraphicsContext, they are completely separate APIs for completely separate UI toolkits.

建议

是否有修改或插入到现有的Swing应用程序的要求?

Is there a requirement to modify or plug into an existing Swing application?

是=> code到 java.awt.Graphics 接口(JavaFX中嵌入时)包住AWT渲染的图形在SwingNode,或使用的桥梁定义如下:

Yes => Code to the java.awt.Graphics interface and (when embedding in JavaFX) wrap your awt rendered graphics in a SwingNode, or use a bridge as defined below.

没有=> code直接向一个JavaFX图形上下文或JavaFX的场景图。

No => Code direct to a JavaFX graphics context or the JavaFX scene graph.

在JavaFX中的显示摆幅(和AWT)

要在JavaFX中显示的Swing,您可以使用 SwingNode ,这是在的Java 8提供早期访问发布

To display Swing in JavaFX, you can use a SwingNode, which is available in a Java 8 early access release.

在Swing中显示的JavaFX

要在Swing中显示JavaFX的,您可以使用 JFXPanel 。请将您的JavaFX的画布到JFXPanel。请参阅对JavaFX的Swing开发教程以获取更多信息。

To display JavaFX in Swing, you can use a JFXPanel. Place your JavaFX canvas into the JFXPanel. See the JavaFX for Swing Developers tutorial for more information.

弥合AWT和JavaFX图形

您可以实现一个的模式来开发一个抽象的接口,然后委托给配置的图形执行。我联系wiki页面提供了如何这可能是做了一个很好的例子。我觉得这样一个桥梁的实现将是非常直接的。例如,你可以实现 java.awt.Graphics 并映射API调用的JavaFX GraphicsContext操作。一旦你的桥是完整的,你只要code桥接口和桥会将您的API调用线程安全的AWT或JavaFX的方法调用适当取决于您选择的实现。

You could implement a bridge pattern to develop an abstract interface and then delegate to a configured Graphics implementation. The wiki page I linked provides a very good example of how this may be done. I think the implementation of such a bridge would be quite straightforward. For instance, you could implement java.awt.Graphics and map the api calls to JavaFX GraphicsContext operations. Once your bridge is complete, you just code to the bridge interface and the bridge translates your api calls to thread-safe invocations of awt or javafx methods as appropriate depending on your chosen implementation.

更新:2014年5月20日

大卫·吉尔伯特(的JFreeChart 创建者)创建了一个桥梁。该项目是FXGraphics2D:

David Gilbert (JFreeChart creator) created a bridge. The project is FXGraphics2D:

FXGraphics2D是一个自由的实现Graphics2D的API的那
  针对JavaFX的画布。在code已被开发利用
  奥森图表
   JFreeChart的,但会为使用Graphics2D的API的任何code一般是有用的。

FXGraphics2D is a free implementation of the Graphics2D API that targets the JavaFX Canvas. The code has been developed for the use of Orson Charts and JFreeChart, but will be generally useful for any code that uses the Graphics2D API.

FXGraphics2D需要JDK 1.8.0或更高版本,并根据许可
  一(三级子句)BSD风格的许可条款。

FXGraphics2D requires JDK 1.8.0 or later and is licensed under the terms of a (three clause) BSD-style license.

FXGraphics2D主页 github上的位置

线程忠告

小心你如何管理线程,如果你是混合的JavaFX和Swing code。这两个工具包是单线程的工具包都在自己的线程上运行他们的处理,所以JavaFX的code必须继续下去了JavaFX线程和Swing code必须在Swing线程上运行。

Be careful of how you manage threads if you are mixing JavaFX and Swing Code. Both toolkits are single threaded and both toolkits run their processing on their own thread, so JavaFX code must go on the JavaFX thread and Swing code must run on the Swing thread.

考虑的JavaFX场景图

的JavaFX包括渲染的场景图能够: //docs.oracle.com/javafx/2/api/javafx/scene/shape/package-summary.html\">2D图形。考虑使用场景图,而不是直接绘制画布。

JavaFX includes a scene graph capable of rendering 2D shapes. Consider using the scene graph instead of a direct draw canvas.

未来Possiblities

有可能的JavaFX的未来版本可能包括像一个 OpenGLNode ,允许你直接渲染到一个OpenGL缓冲区。对于这样一个节点上绘图的API可能比JavaFX的画布API显著不同的(例如,它会使用像 JOGL

It is likely that a future version of JavaFX might include something like an OpenGLNode, allowing you to render directly to an OpenGL buffer. An API for drawing on such a node would likely be significantly different than the JavaFX canvas API (e.g. it would use something like jogl).

您联系example文章的说明

您在你的问题链接文章涉及到的JavaFX 1.x的一般情况下,完全不顾相关的JavaFX 1.x的所有旧的文章,因为它是完全过时,在这种物品的任何信息可以极大地迷惑你。

The article you link in your question relates to JavaFX 1.x. In general, completely disregard all old articles related to JavaFX 1.x as it is totally obsolete and any information in such articles may confuse you greatly.

有关的JavaFX 2+文章是相关的,对他们最好的来源是 Oracle官方的JavaFX 2机制的文档

Articles related to JavaFX 2+ are relevant and the best source for them is the official Oracle JavaFX 2 documention.

这篇关于的Graphics2D和GraphicsContext之间的互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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