具有原生awt渲染功能的我的Canvas立即被透支

时间:2019-12-11 00:04:30

标签: java c++ jawt

我在正确设置AWT帆布涂料时遇到问题。我正在使用JAWT使用c ++渲染它(因为我需要访问DirectX),并且所有JNI部分都可以正常工作。但是,当渲染awt时似乎在Canvas上进行绘制,就像我看到的那样,它被渲染得很好,但是一秒钟之后,它使用其下的任何内容(通常只是awt的背景)在我的Canvas上进行绘制帧。即使运行我可以想出的最简单的示例,也会发生这种情况:

JNICanvas.java

import java.awt.Canvas;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

@SuppressWarnings("serial")
public class JNICanvas extends Canvas {

    static {
        System.loadLibrary("renderLib");
    }

    @Override
    public native void paint(Graphics g);

    public static void main(String[] args) {
        Frame f = new Frame();
        f.setBounds(0, 0, 500, 110);
        f.add(new JNICanvas());
        f.addWindowListener( new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                System.exit(0);
            }
        } );
        f.show();
    }

}

JNICanvas.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNICanvas */

#ifndef _Included_JNICanvas
#define _Included_JNICanvas
#ifdef __cplusplus
extern "C" {
#endif
#undef JNICanvas_FOCUS_TRAVERSABLE_UNKNOWN
#define JNICanvas_FOCUS_TRAVERSABLE_UNKNOWN 0L
#undef JNICanvas_FOCUS_TRAVERSABLE_DEFAULT
#define JNICanvas_FOCUS_TRAVERSABLE_DEFAULT 1L
#undef JNICanvas_FOCUS_TRAVERSABLE_SET
#define JNICanvas_FOCUS_TRAVERSABLE_SET 2L
#undef JNICanvas_TOP_ALIGNMENT
#define JNICanvas_TOP_ALIGNMENT 0.0f
#undef JNICanvas_CENTER_ALIGNMENT
#define JNICanvas_CENTER_ALIGNMENT 0.5f
#undef JNICanvas_BOTTOM_ALIGNMENT
#define JNICanvas_BOTTOM_ALIGNMENT 1.0f
#undef JNICanvas_LEFT_ALIGNMENT
#define JNICanvas_LEFT_ALIGNMENT 0.0f
#undef JNICanvas_RIGHT_ALIGNMENT
#define JNICanvas_RIGHT_ALIGNMENT 1.0f
#undef JNICanvas_serialVersionUID
#define JNICanvas_serialVersionUID -7644114512714619750i64
#undef JNICanvas_serialVersionUID
#define JNICanvas_serialVersionUID -2284879212465893870i64
/*
 * Class:     JNICanvas
 * Method:    paint
 * Signature: (Ljava/awt/Graphics;)V
 */
JNIEXPORT void JNICALL Java_JNICanvas_paint
  (JNIEnv *, jobject, jobject);

#ifdef __cplusplus
}
#endif
#endif

JNICanvas.cpp

#include "jawt_md.h"
#include "framework.h"
#include <cassert>
#include <iostream>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>


JNIEXPORT void JNICALL Java_JNICanvas_init(JNIEnv* env, jobject canvas) {

}

JNIEXPORT void JNICALL Java_JNICanvas_paint(JNIEnv* env, jobject canvas, jobject g) {

    JAWT awt;
    JAWT_DrawingSurface* ds;
    JAWT_DrawingSurfaceInfo* dsi;
    JAWT_Win32DrawingSurfaceInfo* dsi_win;

    jboolean result;
    jint lock;
    // Get the AWT
    awt.version = JAWT_VERSION_9;
    result = JAWT_GetAWT(env, &awt);
    assert(result != JNI_FALSE);
    // Get the drawing surface
    ds = awt.GetDrawingSurface(env, canvas);
    if (ds == NULL)
        return;
    // 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;

    HDC hdc = dsi_win->hdc;
    HWND hWnd = dsi_win->hwnd;

    RECT rect = { 0 };
    GetClientRect(hWnd, &rect);
    HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0));

    FillRect(hdc, &rect, hBrush);

    // Free the drawing surface info
    ds->FreeDrawingSurfaceInfo(dsi);
    // Unlock the drawing surface
    ds->Unlock(ds);
    // Free the drawing surface
    awt.FreeDrawingSurface(ds);

}

在这一点上,我倾向于认为这实际上是一个错误,并且我确实没有看到很多解决方法,但是如果你们有任何想法,我很想听听他们。预先感谢。

0 个答案:

没有答案