1 /**
   2  * Copyright (c) 2007, 2012 Oracle and/or its affiliates. All rights reserved.
   3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
   4  *
   5  *
   6  *
   7  *
   8  *
   9  *
  10  *
  11  *
  12  *
  13  *
  14  *
  15  *
  16  *
  17  *
  18  *
  19  *
  20  *
  21  *
  22  */
  23 
  24 #include <windows.h>
  25 #include "MyCanvas.h"
  26 #include "jawt_md.h"
  27 
  28 /*
  29  * Class:     MyCanvas
  30  * Method:    paint
  31  * Signature: (Ljava/awt/Graphics;)V
  32  */
  33 
  34 extern "C" {  
  35  
  36 JNIEXPORT void JNICALL Java_MyCanvas_paint
  37 (JNIEnv* env, jobject canvas, jobject graphics)
  38 {
  39     /* Get the AWT */
  40     JAWT awt;
  41     awt.version = JAWT_VERSION_1_4;
  42     if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {
  43         printf("AWT Not found\n");
  44         return;
  45     }
  46 
  47     /* Lock the AWT */
  48     awt.Lock(env);
  49 
  50     /* Unlock the AWT */
  51     awt.Unlock(env);
  52 
  53     /* Get the drawing surface */
  54     JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);
  55     if (ds == NULL) {
  56         printf("NULL drawing surface\n");
  57         return;
  58     }
  59 
  60     /* Lock the drawing surface */
  61     jint lock = ds->Lock(ds);
  62     printf("Lock value %d\n", (int)lock);
  63     if((lock & JAWT_LOCK_ERROR) != 0) {
  64         printf("Error locking surface\n");
  65         return;
  66     }
  67 
  68     /* Get the drawing surface info */
  69     JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
  70     if (dsi == NULL) {
  71         printf("Error getting surface info\n");
  72         ds->Unlock(ds);
  73         return;
  74     }
  75 
  76     /* Get the platform-specific drawing info */
  77     JAWT_Win32DrawingSurfaceInfo* dsi_win =
  78         (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
  79 
  80     /* Now paint */
  81     PAINTSTRUCT ps;
  82     /* Do not use the HDC returned from BeginPaint()!! */
  83     ::BeginPaint(dsi_win->hwnd, &ps);
  84     HBRUSH hbrush = (HBRUSH)::GetStockObject(BLACK_BRUSH);
  85     RECT rect;
  86     rect.left = 5;
  87     rect.top = 5;
  88     rect.right = 95;
  89     rect.bottom = 95;
  90     ::FillRect(dsi_win->hdc, &rect, hbrush);
  91     ::EndPaint(dsi_win->hwnd, &ps);
  92 
  93     jobject ref = awt.GetComponent(env, (void*)(dsi_win->hwnd));
  94     if (!env->IsSameObject(ref, canvas)) {
  95         printf("Error! Different objects!\n");
  96     }
  97 
  98     /* Free the drawing surface info */
  99     ds->FreeDrawingSurfaceInfo(dsi);
 100 
 101     /* Unlock the drawing surface */
 102     ds->Unlock(ds);
 103 
 104     /* Free the drawing surface */
 105     awt.FreeDrawingSurface(ds);
 106 }
 107 
 108 }