Java Swings/AWT

AWT stands for Abstract Windowing Toolkit. Prior to Swing, AWT was used to develop GUI and rich client interface, but AWT had one major problem. AWT was platform dependent, which means a program written in AWT behaves differently in different platforms. Hence it defeats WORA (Write Once, Run Anywhere) purpose which is the key Java philosophy.

Swing on the other hand is purely (100%) written in Java. A swing application developed on one platform behaves the same on any other platform in which Java is installed. Hence today almost all Java programmers prefer Swing over AWT for GUI development.

Java AWT Native Interface is an interface for the Java programming language that enables rendering libraries compiled to native code to draw directly to a Java Abstract Window Toolkit (AWT) object drawing surface.

The Java Native Interface (JNI) enabled developers to add platform-dependent functionality to Java applications. The JNI enables developers to add time-critical operations like mathematical calculations and 3D rendering. Previously, native 3D rendering was a problem because the native code didn't have access to the graphic context. The AWT Native Interface is designed to give developers access to an AWT  for direct drawing by native code. In fact, the Java 3D API extension to the standard Java SE JDK relies heavily on the AWT Native Interface to render 3D objects in Java. The AWT Native Interface is very similar to the JNI, and, the steps are, in fact, the same as those of the JNI.

The AWT Native Interface was added to the Java platform with the Java Platform, Standard Edition J2SE 1.3 ("Kestrel") version.

Create the Java application
Type in this in a .java file named JavaSideCanvas and compile:

The  method will be simply invoked when the AWT event dispatching thread repaints the screen.

Create the C++ header file
Create the C++ header file as usual for JNI.

The header file looks like this now:

Implement the C++ native code
Type this in a file named "NativeSideCanvas.cpp" and compile into a library.

(Microsoft) Don't forget to link this with "jawt.lib" and "gdi32.lib". These libraries are needed because the code draws a rectangle using routines from these libraries.

Microsoft C++:

Run the example
Run the file as usual for JNI.

It's interesting to note that the AWT Native Interface requires the "jawt.dll" (or "jawt.so") to run with the application, so the easiest way to do that is copying the "jawt.dll" (should be in the .../jre/bin file path of the JDK's installation path.)

You should see a window with a rectangle drawn in it.

Congratulations! You have made your first AWT Native Application!

Native painting
As you can see, you can paint as if it is a native application. In Windows, the JVM will pass a HWND and other window information to your native application so that your application will "know" where to draw. In this example, it uses GDI to draw a Rectangle. The window information your native side need will be in a  structure (depending on Operating System) which can be retrieved with this line:

dsi_win has the information, look in the "jni.h" file for details.

de:Java Standard: Grafische Oberflächen mit AWT