The following code fails because JAWT_DrawingSurface *ds is null after the assignment ds = awt.GetDrawingSurface(env, canvas);
I don't understand why this is null and am struggling to debug the code with print statements. As I don't know how to debug JNI through xcode. think it's probably something simple I am missing as I downloaded a working example that uses the same setup code.
Thanks for all your help
David
JAWT awt;
JAWT_DrawingSurface* ds = NULL;
JAWT_DrawingSurfaceInfo* dsi = NULL;
JAWT_MacOSXDrawingSurfaceInfo* dsi_mac = NULL;
jboolean result = JNI_FALSE;
jint lock = 0;
// get the AWT
awt.version = JAWT_VERSION_1_4;
result = JAWT_GetAWT(env, &awt);
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
assert(result != JNI_FALSE);
// Get the drawing surface. This can be safely cached.
// Anything below the DS (DSI, contexts, etc)
// can possibly change/go away and should not be cached.
ds = awt.GetDrawingSurface(env, canvas);
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
if(!ds)
{
std::cout << "ds is null" << std::endl;
std::cout << "env address is : " << env << std::endl;
std::cout << "canvas address is : " << &canvas << std::endl;
return;
}
assert(ds != NULL);
// Lock the drawing surface
// You must lock EACH TIME before drawing
lock = ds->Lock(ds);
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
assert((lock & JAWT_LOCK_ERROR) == 0);
// Get the drawing surface info
dsi = ds->GetDrawingSurfaceInfo(ds);
// Check DrawingSurfaceInfo. This can be NULL on Mac OS X
// if the windowing system is not ready
if (dsi != NULL) {
// Get the platform-specific drawing info
// We will use this to get at Cocoa and CoreGraphics
// See <JavaVM/jawt_md.h>
dsi_mac = (JAWT_MacOSXDrawingSurfaceInfo*)dsi->platformInfo;
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
// Get the corresponding peer from the caller canvas
NSView *view = dsi_mac->cocoaViewRef;
// Get the CoreGraphics context from the parent window.
// DO NOT CACHE NSGraphicsContexts -- they may go away.
NSWindow *window = [view window];
NSGraphicsContext *ctxt = [NSGraphicsContext graphicsContextWithWindow:window];
CGContextRef cg = static_cast<CGContextRef>([ctxt graphicsPort]);
// Match Java's ctm
NSRect windowRect = [window frame];
CGContextConcatCTM(cg, CGAffineTransformMake(1, 0, 0, -1, dsi->bounds.x, windowRect.size.height-dsi->bounds.y));
// Draw a pattern using CoreGraphics
CGContextSetRGBFillColor(cg, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextFillRect(cg, CGRectMake(15, 15, dsi->bounds.width-30, dsi->bounds.height-30));
// Free the DrawingSurfaceInfo
ds->FreeDrawingSurfaceInfo(dsi);
if (env->ExceptionOccurred()){
env->ExceptionDescribe();
}