[vtkusers] Java + VTK (C++ version)

Mei, Longyu LMei at analogic.com
Tue Apr 17 12:33:20 EDT 2007


Hello everyone, 

I think I am one of the people try to find a way to create an small
adapter to embed VTK into a Java GUI application.
Thanks Sebastien Jourdain to guide me to the VTK Java Wrapper. But I
think it is not what I am looking for. By using VTK Java Wrapper, you
can use VTK in the contents of Java, not need to know the underneath C++
code. 

What I am looking for is to plugin an existing VTK C++ application into
a Java application without many change, just like we redirect our
existing output stream from screen to file, only one place change is
enough. Java application will be the driver and Java AWT Native
Interface (JNI) will be the bridge. I think three native function calls
from Java side will be enough: init, start, release. All other image
display/process will be done in VTK independently with Java.

If we can do this, we can have our Java engineers to do the GUI
application and have our C++ OpenGL engineers to do the image
display/process. They can do their work independ with each other. Right
now I am evaluating this to see if it is possible. We don't have VTK or
C++ OpenGL engineers yet. So I need your help to create this adapter.
Basically we may need to replace the vtkRendererWindow with one Win32
window .....


I have (downloaded online) a small sample program which uses Microsoft
openGL and controlled (init, start, release) from Java. The image will
be displayed inside a Java window.

Header file created by Java: javah

//////////////// --- header file begin --- ////////////////
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MyWindow */

#ifndef _Included_MyWindow
#define _Included_MyWindow
#ifdef __cplusplus
extern "C" {
#endif
#undef MyWindow_FOCUS_TRAVERSABLE_UNKNOWN
#define MyWindow_FOCUS_TRAVERSABLE_UNKNOWN 0L
#undef MyWindow_FOCUS_TRAVERSABLE_DEFAULT
#define MyWindow_FOCUS_TRAVERSABLE_DEFAULT 1L
#undef MyWindow_FOCUS_TRAVERSABLE_SET
#define MyWindow_FOCUS_TRAVERSABLE_SET 2L
#undef MyWindow_TOP_ALIGNMENT
#define MyWindow_TOP_ALIGNMENT 0.0f
#undef MyWindow_CENTER_ALIGNMENT
#define MyWindow_CENTER_ALIGNMENT 0.5f
#undef MyWindow_BOTTOM_ALIGNMENT
#define MyWindow_BOTTOM_ALIGNMENT 1.0f
#undef MyWindow_LEFT_ALIGNMENT
#define MyWindow_LEFT_ALIGNMENT 0.0f
#undef MyWindow_RIGHT_ALIGNMENT
#define MyWindow_RIGHT_ALIGNMENT 1.0f
#undef MyWindow_serialVersionUID
#define MyWindow_serialVersionUID -7644114512714619750i64
#undef MyWindow_serialVersionUID
#define MyWindow_serialVersionUID -2284879212465893870i64
#undef MyWindow_TIMER_SECONDS
#define MyWindow_TIMER_SECONDS 50L
/*
 * Class:     MyWindow
 * Method:    paintOpenGL
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_MyWindow_paintOpenGL
  (JNIEnv *, jobject);

/*
 * Class:     MyWindow
 * Method:    initializeOpenGL
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_MyWindow_initializeOpenGL
  (JNIEnv *, jobject);

/*
 * Class:     MyWindow
 * Method:    cleanupOpenGL
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_MyWindow_cleanupOpenGL
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
/////////////// --- header file end  --- ////////////////


/////////////// --- cpp file begin  --- ////////////////
#include <windows.h>
#include <assert.h>
#include <gl/gl.h>
#include "jawt_md.h"
#include "JAWT_Info.h"
#include "MyWindow.h"

// Static variables for the OpenGL calls.
static HGLRC    hRC = NULL;
static HDC        hDC = NULL;

/*
 * Class:     MyWindow
 * Method:    initializeOpenGL
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_MyWindow_initializeOpenGL
  (JNIEnv *env, jobject panel)
{
    // get the window handle
    JAWT_Info info(env, panel);
    HWND hWnd = (HWND)info.getHWND();

    if(hWnd == NULL)
        return;

    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    // get the device context (DC)
    HWND hwnd = info.getHWND();
    hDC = ::GetDC(hwnd);

    // set the pixel format for the DC
    ::ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
    PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ::ChoosePixelFormat( hDC, &pfd );
    ::SetPixelFormat( hDC, iFormat, &pfd );

    // create and enable the render context (RC)
    hRC = ::wglCreateContext( hDC );
    ::wglMakeCurrent( hDC, hRC );
}

/*
 * Class:     MyWindow
 * Method:    paint
 * Signature: (Ljava/awt/Graphics;)V
 */
JNIEXPORT void JNICALL Java_MyWindow_paintOpenGL
  (JNIEnv *env, jobject panel)
{
    static float theta = 0.0f;
    // get the window handle
    JAWT_Info info(env, panel);
    HWND hWnd = (HWND)info.getHWND();

    if(hWnd == NULL)
        return;

    // OpenGL animation code goes here
    ::glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    ::glClear( GL_COLOR_BUFFER_BIT );

    ::glPushMatrix();
    ::glRotatef( theta, 0.0f, 0.0f, 1.0f );
    ::glBegin( GL_TRIANGLES );
    ::glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
    ::glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
    ::glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
    ::glEnd();
    ::glPopMatrix();

    ::SwapBuffers( hDC );

    theta += 1.0f;
}

/*
 * Class:     MyWindow
 * Method:    cleanupOpenGL
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_MyWindow_cleanupOpenGL
(JNIEnv *env, jobject panel)
{
    // get the window handle
    JAWT_Info info(env, panel);
    HWND hWnd = (HWND)info.getHWND();
    if(hWnd == NULL)
        return;

    ::wglMakeCurrent( NULL, NULL );
    ::wglDeleteContext( hRC );
    ::ReleaseDC( hWnd, hDC );
}
/////////////// --- cpp file end  --- ////////////////

/////////////// --- help header file begin  --- ////////////////
#ifndef JAWT_INFO_H
#define JAWT_INFO_H

// Helper class for accessing JAWT Information.
class JAWT_Info 
{
private:
    JAWT awt;
    JAWT_DrawingSurface* ds;
    JAWT_DrawingSurfaceInfo* dsi;
    JAWT_Win32DrawingSurfaceInfo* dsi_win;

public:
    JAWT_Info(JNIEnv *env, jobject panel)
    {
        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, panel);
        if(ds == NULL)
            return;
        // 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;
    }
    
    HWND getHWND()
    {
        if(dsi_win == NULL)
            return NULL;
        return dsi_win->hwnd;
    }

    HDC getHDC()
    {
        if(dsi_win == NULL)
            return NULL;
        return dsi_win->hdc;
    }

    virtual ~JAWT_Info()
    {
        if(ds != NULL)
        {
            // Free the drawing surface info
            ds->FreeDrawingSurfaceInfo(dsi);
            // Unlock the drawing surface
            ds->Unlock(ds);
            // Free the drawing surface
            awt.FreeDrawingSurface(ds);
        }
    }
};

#endif
/////////////// --- help header file end  --- ////////////////



****************************************************************
The information transmitted in this message is confidential and may be privileged.  Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited.  If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors at analogic.com - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.



More information about the vtkusers mailing list