[vtkusers] vtk as JNI - sample code

Gopalakrishnan, Gowri Gowri.Gopalakrishnan at inteq.com
Wed Dec 12 04:26:02 EST 2001


I am attaching here three files. Two *.java file and one *.cpp file

The first java file myFrame.java is just a frame of java to hold all the
user interfaces one wants to put in 
The second java file myCanvas basically provides the drawing surface for
vtk. This class must extend the
canvas class of sun awt and the paint method of canvas declared native ( Sun
java site has some explanation on this. 
Look for JNI / native rendering in java )
The *.cpp file just shows how to extract window handle from the canvas and
use it with vtk. It seems you can 
achieve this with some swing components instead of Canvas, but i did not try
it. 
Also I have tested this in Windows platform. If anyone is interested we can
test it for Linux. 

 <<myCanvas.java>>  <<myFrame.java>>  <<temp.cpp>> 

I am sorry I could not attach the file i am working with at the moment to
give you a working example. But this *.cpp file
should give you all that you need to start rendering. For events handling I
catch all my events on the java side and 
make a native call for the events I want and do the corresponding action in
C. Here is a example
>From java when a mouse is clicked and moved call the corresponding C
function. 

JNIEXPORT void JNICALL Java_myCanvas_OnMouseLButtonDown(JNIEnv *env, jobject
obj, jint x, jint y)
{
	renWin->SetDesiredUpdateRate(5.0);
	MouseX = x;
	MouseY = y;
	InteractionMode=VTK_ROTATE;
}

JNIEXPORT void JNICALL Java_myCanvas_OnMouseMoved(JNIEnv *env, jobject obj,
jint x , jint y)
{
	vtkCamera *cam = renderer->GetActiveCamera();
	switch(InteractionMode)
	{
	case VTK_ROTATE:
		
		cam->Azimuth(MouseX - x);
		cam->Elevation(y - MouseY);
		cam->OrthogonalizeViewUp();
		renderer->ResetCameraClippingRange();
	break;

	case VTK_ZOOM:
		double zoomFactor = pow(1.02,(y - MouseY));
		if (cam->GetParallelProjection() == 1)
		{
	
cam->SetParallelScale(cam->GetParallelScale()/zoomFactor);
		}
		else
		{
			cam->Dolly(zoomFactor);
			renderer->ResetCameraClippingRange();
		}
	break;
	}

	MouseX = x;
	MouseY = y;
	renWin->Render();
}

If any one has any question/ or something is not alright in this let me know

Regards
Gowri

-------------- next part --------------
A non-text attachment was scrubbed...
Name: myCanvas.java
Type: application/octet-stream
Size: 3690 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20011212/164bb083/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myFrame.java
Type: application/octet-stream
Size: 7266 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20011212/164bb083/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: temp.cpp
Type: application/octet-stream
Size: 2524 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20011212/164bb083/attachment-0002.obj>


More information about the vtkusers mailing list