1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  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 }