4

I'm pretty new to the JNI, and I stumbled across this while looking into the JNI:

Not only can native code interface with Java, it can also draw on a Java Canvas, which is possible with the Java AWT Native Interface

Is there a specific reason why this functionality was made specific/is available? Does it improve processing time on Windows system? Please elaborate why and when one would use such a feature

Loktar
  • 34,764
  • 7
  • 90
  • 104
Vince
  • 14,470
  • 7
  • 39
  • 84

2 Answers2

5

Native performance rendering in Java... games would be the first thing to come to mind but really drawing anything that would be computationally intensive or that requires a high frame-rate would benefit.

Official Java Explanation:

The ability to draw directly into a Java Canvas from a native code library is extremely useful for developers planning to migrate a legacy software system to Java, especially one that includes a high-performance rendering engine. It makes it much easier to migrate in stages, leaving performance-sensitive rendering code alone, while other less-sensitive portions of code are converted to Java. The result can be a modern Java-centric application, providing the benefit of portability and development efficiency, but one that does not sacrifice an investment in performance of a key piece of native code.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47
0

The most common use of AWT's JNI painting is 3D drawing with OpenGL or DirectX D3D. Some java 3D APIs, like Java3D and initial releases of JOGL, used Canvas.java as drawing surface. It can also be used for 2D native rendering, but Java drawings features are just enough for this task.

Alex Byrth
  • 1,328
  • 18
  • 23
  • Java3D 1.7.0 will provide other options, its Canvas3D extends directly java.awt.Canvas. JOGL uses JAWT under the hood except with NEWT, only its heavyweight AWT GLCanvas still extends java.awt.Canvas. The Java AWT Native Interface is used to provide a good interoperability with AWT in the native code but only a Java binding (like JOGL) should directly use it, doing it in a scenegraph API is a bad idea, the hard work is already done in JOGL and when an API depends too strongly on AWT, making it work with AWT-free JREs is difficult. I'm not convinced by the official explanation. – gouessej Sep 29 '16 at 10:50
  • I think java.awt.Canvas is too compromised with AWT's thread for rendering and event processing. That's why it requires lock/unlock for JNI drawing. Sounds nice the idea to have JAVA3D with it's own and exclusive canvas. The drawback is no be able to include it within a traditional 2D GUI, unless use a offscreen renderer. – Alex Byrth Sep 30 '16 at 18:15
  • Actually, it's possible to interact with AWT and use NEWT at the same time by using NewtCanvasAWT, you get the best of the both world without offscreen rendering :) – gouessej Oct 03 '16 at 08:11