1 /*
   2  * Copyright (c) 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <windows.h>
  27 #include "MyCanvas.h"
  28 #include "jawt_md.h"
  29 
  30 /*
  31  * Class:     MyCanvas
  32  * Method:    paint
  33  * Signature: (Ljava/awt/Graphics;)V
  34  */
  35 
  36 extern "C" {
  37 
  38 JNIEXPORT void JNICALL Java_MyCanvas_paint
  39 (JNIEnv* env, jobject canvas, jobject graphics)
  40 {
  41     /* Get the AWT */
  42     JAWT awt;
  43     awt.version = JAWT_VERSION_1_4;
  44     if (JAWT_GetAWT(env, &awt) == JNI_FALSE) {
  45         printf("AWT Not found\n");
  46         return;
  47     }
  48 
  49     /* Lock the AWT */
  50     awt.Lock(env);
  51 
  52     /* Unlock the AWT */
  53     awt.Unlock(env);
  54 
  55     /* Get the drawing surface */
  56     JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, canvas);
  57     if (ds == NULL) {
  58         printf("NULL drawing surface\n");
  59         return;
  60     }
  61 
  62     /* Lock the drawing surface */
  63     jint lock = ds->Lock(ds);
  64     printf("Lock value %d\n", (int)lock);
  65     if((lock & JAWT_LOCK_ERROR) != 0) {
  66         printf("Error locking surface\n");
  67         return;
  68     }
  69 
  70     /* Get the drawing surface info */
  71     JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
  72     if (dsi == NULL) {
  73         printf("Error getting surface info\n");
  74         ds->Unlock(ds);
  75         return;
  76     }
  77 
  78     /* Get the platform-specific drawing info */
  79     JAWT_Win32DrawingSurfaceInfo* dsi_win =
  80         (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
  81 
  82     /* Now paint */
  83     PAINTSTRUCT ps;
  84     /* Do not use the HDC returned from BeginPaint()!! */
  85     ::BeginPaint(dsi_win->hwnd, &ps);
  86     HBRUSH hbrush = (HBRUSH)::GetStockObject(BLACK_BRUSH);
  87     RECT rect;
  88     rect.left = 5;
  89     rect.top = 5;
  90     rect.right = 95;
  91     rect.bottom = 95;
  92     ::FillRect(dsi_win->hdc, &rect, hbrush);
  93     ::EndPaint(dsi_win->hwnd, &ps);
  94 
  95     jobject ref = awt.GetComponent(env, (void*)(dsi_win->hwnd));
  96     if (!env->IsSameObject(ref, canvas)) {
  97         printf("Error! Different objects!\n");
  98     }
  99 
 100     /* Free the drawing surface info */
 101     ds->FreeDrawingSurfaceInfo(dsi);
 102 
 103     /* Unlock the drawing surface */
 104     ds->Unlock(ds);
 105 
 106     /* Free the drawing surface */
 107     awt.FreeDrawingSurface(ds);
 108 }
 109 
 110 }