Java AWT Native Interface
Encyclopedia
|
| Tutorials | Encyclopedia | Dictionary | Directory |
|
Java AWT Native Interface
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 The AWT Native Interface was added to the Java platform with the J2SE 1.3 ("Kestrel") version.
AWT Native Interface example walkthroughCreate the Java applicationType in this in a .java file named JavaSideCanvas and compile:
import java.awt.*;
import java.awt.event.*;
public class JavaSideCanvas extends Canvas {
static {
System.loadLibrary("NativeSideCanvas");
}
public native void paint(Graphics g);
public static void main(String[] args) {
Frame f = new Frame();
f.setBounds(0, 0, 500, 500);
JavaSideCanvas jsc = new JavaSideCanvas();
f.add(jsc);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
f.show();
}
}
See the Java Native Interface article for an explanation of the Create the C++ header fileCreate the C++ header file as usual (See Java Native Interface for more complete explanation.) The header file looks like this now: Implement the C++ native codeType this in a file named "NativeSideCanvas.cpp" and compile into a library. See Java Native Interface for a more complete explanation. (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++: (For Solaris code and other operating systems see links below.) Run the exampleRun the file as usual. (See Java Native Interface for complete instructions.) 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 dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo; dsi_win has the information, look in the "jni.h" file for details. See alsoExternal linksSource: Wikipedia | The above article is available under the GNU FDL. | Edit this article
|
|
top
©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement