Java AWT Native Interface: Difference between revisions

Content deleted Content added
m Robot-assisted disambiguation: Render - Changed link(s) to Rendering (computer graphics)
transferred how-to on Wikibooks
Line 1:
{{howto|date=May 2008}}
'''Java AWT Native Interface''' is an interface for the [[Java (programming language)|Java programming language]] that enables [[Rendering (computer graphics)|rendering]] [[library (computer science)|libraries]] compiled to [[native code]] to draw directly to a Java [[Abstract Window Toolkit]] (AWT) {{Javadoc:SE|java/awt|Canvas}} [[object (computer science)|object]] drawing surface.
 
Line 6 ⟶ 5:
The AWT Native Interface was added to the [[Java platform]] with the [[Java Platform, Standard Edition|J2SE]] 1.3 ("Kestrel") version.
 
== AWT Native Interface example walkthroughsteps ==
A complete walkthrough example of this technology is available on Wikibooks (see link below).
=== Create the Java application ===
 
=== Create a Java application ===
Type in this in a .java file named JavaSideCanvas and compile:
<source lang="java">
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 frame = new Frame();
frame.setBounds(0, 0, 500, 500);
JavaSideCanvas jsc = new JavaSideCanvas();
frame.add(jsc);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
System.exit(0);
}
});
frame.show();
}
}
</source>
See the [[Java Native Interface]] article for an explanation of the <code>native</code> [[keyword (computer programming)|keyword]] and the <code>loadLibrary()</code> method. The <code>paint()</code> method will be simply invoked when the AWT [[event dispatching thread]] "repaints" the screen.
 
See the [[Java Native Interface]] article for an explanation of the <code>native</code> [[keyword (computer programming)|keyword]] and the <code>loadLibrary()</code> method. A <code>paint()</code> method will be simply invoked when the AWT [[event dispatching thread]] "repaints" the screen.
=== Create the C++ header file ===
 
=== Create thea [[C++]] [[header file]] as usual (See [[Java Native Interface]] for more complete explanation.)===
 
Create the [[C++]] [[header file]] as usual (See [[Java Native Interface]] for more complete explanations.)
The header file looks like this now:
<source lang="cpp">
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JavaSideCanvas */
#ifndef _Included_JavaSideCanvas
#define _Included_JavaSideCanvas
#ifdef __cplusplus
extern "C" {
#endif
#undef JavaSideCanvas_FOCUS_TRAVERSABLE_UNKNOWN
#define JavaSideCanvas_FOCUS_TRAVERSABLE_UNKNOWN 0L
#undef JavaSideCanvas_FOCUS_TRAVERSABLE_DEFAULT
#define JavaSideCanvas_FOCUS_TRAVERSABLE_DEFAULT 1L
#undef JavaSideCanvas_FOCUS_TRAVERSABLE_SET
#define JavaSideCanvas_FOCUS_TRAVERSABLE_SET 2L
#undef JavaSideCanvas_TOP_ALIGNMENT
#define JavaSideCanvas_TOP_ALIGNMENT 0.0f
#undef JavaSideCanvas_CENTER_ALIGNMENT
#define JavaSideCanvas_CENTER_ALIGNMENT 0.5f
#undef JavaSideCanvas_BOTTOM_ALIGNMENT
#define JavaSideCanvas_BOTTOM_ALIGNMENT 1.0f
#undef JavaSideCanvas_LEFT_ALIGNMENT
#define JavaSideCanvas_LEFT_ALIGNMENT 0.0f
#undef JavaSideCanvas_RIGHT_ALIGNMENT
#define JavaSideCanvas_RIGHT_ALIGNMENT 1.0f
#undef JavaSideCanvas_serialVersionUID
#define JavaSideCanvas_serialVersionUID -7644114512714619750i64
#undef JavaSideCanvas_serialVersionUID
#define JavaSideCanvas_serialVersionUID -2284879212465893870i64
/*
* Class: JavaSideCanvas
* Method: paint
* Signature: (Ljava/awt/Graphics;)V
*/
JNIEXPORT void JNICALL Java_JavaSideCanvas_paint
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
</source>
 
=== Implement the C++ native code ===
 
Type this in a file named "NativeSideCanvas.cpp" and compile into a library. See [[Java Native Interface]] for a more complete explanation. (For [[Solaris Operating System|Solaris]] code and other operating systems see links below.)
 
=== Run the program ===
(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.
 
One should run the file as usual. One should then see a window with, for example, a rectangle drawn in it. (See [[Java Native Interface]] for complete instructions.)
Microsoft C++:
<source lang="cpp">
#include "jawt_md.h"
#include <assert.h>
#include "JavaSideCanvas.h"
JNIEXPORT void JNICALL Java_JavaSideCanvas_paint(JNIEnv* env, jobject canvas, jobject graphics)
{
JAWT awt;
JAWT_DrawingSurface* ds;
JAWT_DrawingSurfaceInfo* dsi;
JAWT_Win32DrawingSurfaceInfo* dsi_win;
jboolean result;
jint lock;
// Get the AWT
awt.version = JAWT_VERSION_1_3;
result = JAWT_GetAWT(env, &awt);
assert(result != JNI_FALSE);
// Get the drawing surface
ds = awt.GetDrawingSurface(env, canvas);
assert(ds != NULL);
// Lock the drawing surface
lock = ds->Lock(ds);
assert((lock & JAWT_LOCK_ERROR) == 0);
// Get the drawing surface info
dsi = ds->GetDrawingSurfaceInfo(ds);
// Get the platform-specific drawing info
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
//////////////////////////////
// !!! DO PAINTING HERE !!! //
//////////////////////////////
//Simple paints a rectangle (GDI32)
//It's a GDI API, Use Windows provided GDI library in order to compile the code.
Rectangle(dsi_win->hdc, 50, 50, 200, 200);
// Free the drawing surface info
ds->FreeDrawingSurfaceInfo(dsi);
// Unlock the drawing surface
ds->Unlock(ds);
// Free the drawing surface
awt.FreeDrawingSurface(ds);
}
</source>
(For [[Solaris Operating System|Solaris]] code and other operating systems see links below.)
 
Note: One can notice 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.)
=== Run the example ===
 
Run 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 ==
One can paint as if it is a native application. In [[Microsoft Windows|Windows]], the JVM will pass a HWND and other window information to the native application so that the application will "know" where to draw. It could use GDI to draw a Rectangle. The window information the native side needs will be in a <code>JAWT_Win32DrawingSurfaceInfo</code> structure (depending on [[Operating System]]) which can be retrieved with this line: dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
{{cleanup-section|date=May 2008}}
As you can see, you can paint as if it is a native application. In [[Microsoft Windows|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 <code>JAWT_Win32DrawingSurfaceInfo</code> structure (depending on [[Operating System]]) which can be retrieved with this line:
 
dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
 
dsi_win has the information, look in the "jni.h" file for details.
 
== See also ==
Line 169 ⟶ 34:
* [[Abstract Window Toolkit]]
* [[Event dispatching thread]]
* [[:wikibooks:Java_Swings/AWT|Java Swings, a Wikibook about graphic programming in Java]]
 
== External links ==