How to visualise OCC with java AWT ?

I'm trying to build a simple example to use a java awt canvas for OCC visualization, but I'm failing. The program crashes always in the following line:

Handle(V3d_View) myView = myViewer -> CreateView();

How can this be? Any sugestions?

As recommended, in the java part I inherited from an AWT canvas and declared a native paint method. The corresponding methon in C++ gets called and even the java window appears for a short time. So everything seems to work fine and I've no idea where is the reason for the crash.

/*
* Class: JNI_NativeCanvas
* Method: paint
* Signature: (Ljava/awt/Graphics;)V
*/
JNIEXPORT void JNICALL Java_JNI_NativeCanvas_paint
(JNIEnv* jniEnvironment, jobject canvas, jobject awtGraphics)
{
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(jniEnvironment, &awt);
assert(result != JNI_FALSE);

// Get the drawing surface
ds = awt.GetDrawingSurface(jniEnvironment, 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;

// Create a 3D viewer
Handle(Graphic3d_WNTGraphicDevice) defaultDevice = new Graphic3d_WNTGraphicDevice();

TCollection_ExtendedString aName("3DV");
V3d_Viewer* myViewer = new V3d_Viewer (defaultDevice,aName.ToExtString (), "");
myViewer -> SetDefaultLights ();
myViewer -> SetLightOn ();

// Create a 3D view
Handle (WNT_Window) aWNTWindow;

//2nd param: handle of the window used in the view
assert(dsi_win->hwnd != NULL);
aWNTWindow = new WNT_Window(defaultDevice, dsi_win->hwnd);

// *** HERE WE CRASH ***
Handle(V3d_View) myView = myViewer -> CreateView();
myView -> SetWindow (aWNTWindow);

// Free the drawing surface info
ds->FreeDrawingSurfaceInfo(dsi);

// Unlock the drawing surface
ds->Unlock(ds);

// Free the drawing surface
awt.FreeDrawingSurface(ds);
}

Java part

@Override
public native void paint(Graphics g);

public static void main(String[] args) {
Frame f = new Frame();
f.setBounds(0, 0, 500, 500);
f.setBackground(new Color(120,140,160));
f.add( new NativeCanvas() );
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(@SuppressWarnings("unused")
WindowEvent ev) {
System.exit(0);
}
} );
f.setVisible(true);
}

mz's picture

OK, in meanwhile I found out that when using a handle instead of a pointer for myViewer, it seems to work. As I'm not the only one having problems to create a first simple example with java: this code paints a box on an AWT canvas:

#include "JNI_NativeCanvas.h"

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

/*
* Class: JNI_NativeCanvas
* Method: paint
* Signature: (Ljava/awt/Graphics;)V
*/
JNIEXPORT void JNICALL Java_JNI_NativeCanvas_paint
(JNIEnv* jniEnvironment, jobject canvas, jobject awtGraphics)
{
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(jniEnvironment, &awt);
assert(result != JNI_FALSE);

// Get the drawing surface
ds = awt.GetDrawingSurface(jniEnvironment, 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;

// Create a 3D viewer
Handle(Graphic3d_WNTGraphicDevice) defaultDevice = new Graphic3d_WNTGraphicDevice() ;
TCollection_ExtendedString aName = "3dView";
Handle(V3d_Viewer) myViewer = new V3d_Viewer(defaultDevice, aName.ToExtString());

// Create a 3D view
Handle (WNT_Window) aWNTWindow;

assert(dsi_win->hwnd != NULL);

// Set the window for the view to paint on
aWNTWindow = new WNT_Window(defaultDevice, dsi_win->hwnd);
Handle(V3d_View) myView = myViewer -> CreateView();
myView -> SetWindow (aWNTWindow);

// create interactive context
AIS_InteractiveContext* myAISContext = new AIS_InteractiveContext (myViewer);

// paint a box
TopoDS_Shape shape (BRepPrimAPI_MakeBox(80,90,110));
Handle(AIS_Shape) aisShape = new AIS_Shape(shape);
myAISContext->Display(aisShape);

// Free the drawing surface info
ds->FreeDrawingSurfaceInfo(dsi);

// Unlock the drawing surface
ds->Unlock(ds);

// Free the drawing surface
awt.FreeDrawingSurface(ds);
}

(java part as in last message)