Java – interoperability between graphics2d and graphicscontext
I'm working with a combination looking for a graphics renderer in Java At present, I am trying to determine the target Java awt. Whether graphics2d is forward compatible That is, can other libraries, such as JavaFX displays, render from graphics2d instances? java. awt. Graphics2d and JavaFX scene. canvas. Is there interoperability between graphicscontext?
Or, if there is no direct between graphics2d and graphicscontext, is there a way to display graphics2d rendering in JavaFX applications, similar to swing displaying graphics2d rendering in panel?
Edit:
If it were not for this problem, I would be quite new to the Java ecosystem For more context, most of the things I found in Internet search are examples / tutorials, which explain how to complete the work in AWT in JavaFX (see example article). This can't answer my question, but it will lead me to guess that there is no interoperability there However, I hope someone who is more familiar with the ecosystem can answer this question directly
Solution
There is no built-in interoperability between AWT graphics2d and JavaFX graphicscontext. They are completely independent APIs for completely independent UI toolkits
proposal
Are there any requirements to modify or insert existing swing applications?
Yes = > code to Java awt. Graphics interface and (embedded in JavaFX) wrap your AWT rendered graphics in swingnode, or use the bridge defined below
No = > the code points directly to the JavaFX graphic context or JavaFX scene diagram
Show swing (and AWT) in JavaFX
To display swing in JavaFX, you can use the swingnode. In Java 8 early access release
Show JavaFX in swing
To display JavaFX in swing, you can use jfxpanel Put the JavaFX canvas into jfxpanel For more information, see the JavaFX for swing developers tutorial
Connecting AWT and JavaFX graphics
You can implement the bridge pattern, develop the abstract interface, and delegate it to the configured graphics implementation My linked wiki page provides a good example of how to do this I think the implementation of such a bridge is quite straightforward For example, you can implement Java awt. Graphics and map API calls to JavaFX graphicscontext operations Once your bridge is completed, you only need to write the bridge interface, and the bridge can convert your API calls into thread safe AWT or JavaFX method calls according to the implementation you choose
Update Date: May 20, 2014
David Gilbert (founder of J freechart) created a bridge The project is fxgraphics2d:
Fxgraphics2d home page and GitHub location
Thread suggestion
If you want to mix JavaFX and swing code, pay attention to how to manage threads Both toolkits are single threaded. Both toolkits run their processing on their own threads, so JavaFX code must run on JavaFX threads, and swing code must run on swing threads
Consider JavaFX scenegraph
JavaFX includes a scene graph that can render 2D shapes Consider using scene diagrams instead of drawing canvases directly
Future possibilities
Future versions of JavaFX are likely to include content like OpenGL node, which can be rendered directly to the OpenGL buffer The API used to draw such a node may be significantly different from the JavaFX canvas API (for example, it will use something like jogl)
Comments about the sample article you linked
The article you linked in the question is related to JavaFX 1 x. In general, completely ignore JavaFX 1 All old articles related to x, because it is completely outdated, and any information in these articles may surprise you
Articles related to JavaFX 2 are relevant, and the best source is official Oracle JavaFX 2 document