Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获得';无效的可绘制';_Java_Macos_Opengl_Jna - Fatal编程技术网

Java 获得';无效的可绘制';

Java 获得';无效的可绘制';,java,macos,opengl,jna,Java,Macos,Opengl,Jna,我试图在MacOSX上使用JNA直接从Java使用OpenGL(我在Windows和Linux上成功地做到了这一点)。我已经浏览了JOGL资源,但他们使用的是CALayers,我还不明白。如果可能的话,我只想简单地使用NSOpenGLView,并将其放在AWT画布的顶部。我使用JNA查找NSWindow并添加我创建的NSOpenGLView,它似乎可以工作,除非调用[nsOpenGLContext setView]或[NSOpenGLView lockFocus]时出现“无效的可绘制”错误。我从

我试图在MacOSX上使用JNA直接从Java使用OpenGL(我在Windows和Linux上成功地做到了这一点)。我已经浏览了JOGL资源,但他们使用的是CALayers,我还不明白。如果可能的话,我只想简单地使用NSOpenGLView,并将其放在AWT画布的顶部。我使用JNA查找NSWindow并添加我创建的NSOpenGLView,它似乎可以工作,除非调用[nsOpenGLContext setView]或[NSOpenGLView lockFocus]时出现“无效的可绘制”错误。我从洛可可学会了如何从Java使用Objective

以下是一些示例代码:

  private static boolean createMac(GL gl, Component c) {
    NSAutoreleasePool pool = new NSAutoreleasePool();
    pool.alloc();
    pool.init();
    gl.nsopenglview = new NSOpenGLView();
    gl.nsopenglview.alloc();
    Pointer ptr = Native.getWindowPointer(findWindow(c));
    NSObject nsComponent = new NSObject();
    nsComponent.obj = ptr;
    Pointer cClass = nsComponent._class();
    NSView view = new NSView();
    view.alloc();
    boolean isView = view.isKindOfClass(cClass);
//    JFLog.log("test=" + isView);
    if (isView) {
      view.dealloc();
      view.obj = ptr;  //do NOT dealloc this (usually NSWindowViewAWT)
      gl.nswindow = view.window();
    } else {
      view.dealloc();
      gl.nswindow = new NSWindow();
      gl.nswindow.obj = ptr;
    }
    NSOpenGLPixelFormat fmt = new NSOpenGLPixelFormat();
    fmt.alloc();
    fmt.initWithAttributes(new int[] {
      NSOpenGLPFAWindow,
//      NSOpenGLPFAAccelerated,  //is not available on my test system
      NSOpenGLPFADoubleBuffer,
      NSOpenGLPFAColorSize,24,
      NSOpenGLPFADepthSize,16,
        0  //zero terminate list
      }
    );
    if (fmt.obj == null) {
      JFLog.log("NSOpenGLPixelFormat initWithAttributes failed");
      return false;
    }
    if (gl.nsopenglview != null) {
      gl.nsopenglview.initWithFrame(new NSRect(c.getBounds()), fmt);
    }

    NSView content = gl.nswindow.contentView();
    JFLog.log("content view=" + content.obj);
    content.addSubview(gl.nsopenglview);
    JFLog.log("layered=" + content.wantsLayer());

    //use created context
    gl.nsopenglcontext = gl.nsopenglview.openGLContext();

    //create some resize/move listeners
    final GL _gl = gl;
    final Component _c = c;
    c.addComponentListener(new ComponentListener() {
      public void componentResized(ComponentEvent e) {
        _gl.nsopenglview.setFrame(new NSRect(_c.getBounds()));
      }
      public void componentMoved(ComponentEvent e) {
        _gl.nsopenglview.setFrame(new NSRect(_c.getBounds()));
      }
      public void componentShown(ComponentEvent e) {}
      public void componentHidden(ComponentEvent e) {}
    });
    if (api == null) {
      api = new GLFuncs();
      gl.glLibrary = NativeLibrary.getInstance("OpenGL");
      try {
        Field fields[] = api.getClass().getFields();
        for(int a=0;a<fields.length;a++) {
          String name = fields[a].getName();
          try {
            fields[a].set(api, gl.glLibrary.getFunction(name));
          } catch (Throwable t) {
            JFLog.log("OpenGL:Warning:Function not found:" + name);
          }
        }
      } catch (Exception e) {
        JFLog.log(e);
      }
    }
    pool.release();
    return true;
  }
private静态布尔createMac(GL,组件c){
NSAutoreleasePool=新的NSAutoreleasePool();
pool.alloc();
pool.init();
gl.nsopenglview=新的nsopenglview();
gl.nsopenglview.alloc();
指针ptr=Native.getWindowPointer(findWindow(c));
NSObject nsComponent=新的NSObject();
nsComponent.obj=ptr;
指针cClass=nsComponent._class();
NSView视图=新的NSView();
view.alloc();
布尔值isView=view.iskindof类(cClass);
//JFLog.log(“test=“+isView”);
如果(iView){
view.dealloc();
view.obj=ptr;//不要解除锁定(通常为NSWindowViewAWT)
gl.nswindow=view.window();
}否则{
view.dealloc();
gl.nswindow=新的nswindow();
gl.nswindow.obj=ptr;
}
NSOpenGLPixelFormat fmt=新的NSOpenGLPixelFormat();
fmt.alloc();
fmt.initWithAttributes(新int[]{
新南威尔士州,
//NSOpenGLPFAAAccelerated,//在我的测试系统上不可用
NSOPENGLPFADOUBLEBUFER,
NSOpenGLPFAColorSize,24,
NSOpenGLPFADepthSize,16岁,
0//0终止列表
}
);
如果(fmt.obj==null){
log(“NSOpenGLPixelFormat initWithAttributes失败”);
返回false;
}
如果(gl.nsopenglview!=null){
gl.nsopenglview.initWithFrame(新的NSRect(c.getBounds()),fmt);
}
NSView content=gl.nswindow.contentView();
JFLog.log(“内容视图=“+content.obj”);
content.addSubview(gl.nsopenglview);
JFLog.log(“layered=“+content.wantsLayer());
//使用创建的上下文
gl.nsopenglcontext=gl.nsopenglview.openGLContext();
//创建一些调整大小/移动侦听器
最终总帐_总帐=总帐;
最终成分_c=c;
c、 addComponentListener(新的ComponentListener(){
公共无效组件已恢复(组件事件e){
_gl.nsopenglview.setFrame(新的NSRect(_c.getBounds());
}
公共无效组件已移动(组件事件e){
_gl.nsopenglview.setFrame(新的NSRect(_c.getBounds());
}
显示的公共无效组件(组件事件e){}
public void componentHidden(ComponentEvent e){}
});
如果(api==null){
api=新的GLFuncs();
gl.glLibrary=nativellibrary.getInstance(“OpenGL”);
试一试{
字段字段[]=api.getClass().getFields();

对于(int a=0;a我让它工作了!问题出在洛可可(或者可能是JNA中的错误)。他们的NSRect结构不能正确地传递给[NSOpenGLView initWithFrame]或[NSWindow initWithContentRect]。如果我直接将4个字段(x、y、width、height)传递给函数而不是结构本身,那么它就工作了。我还使用了[NSObject performSelectorOnMainThread]以确保我在主线程上完成所有GUI工作

因此,可以使用Java中的纯JNA使用OpenGL。无需本机代码。
这应该可以在v7.16的javaforce.sf.net中找到,我将在稍后发布


谢谢。

最近的OSX Java版本(从1.7开始)已经不推荐使用JAWT来获取本机窗口的句柄。可能在某个时候,
native.getWindowPointer()的行为
将在OSX上变得未定义。我已经取得了一些进展,我现在使用NSOpenGLLayer,不再获得“无效的可绘制”选项。但是该层仍然不可见。我想我需要覆盖updateLayer()这在JNA中是不容易做到的。但是从回调中提取将不适用于我的库设计。然后有technomage评论说JAWT不受欢迎(即使JOGL仍然使用它)。我想我知道这个问题。我尝试过使用其他Cocoa函数,如NSWindow,但什么都没有出现。我想你不能在主线程下使用Mac GUI函数。我如何从主线程调用Cocoa?如果不再有效,我可以使用其他方法获取除JAWT之外的窗口句柄(枚举WindowList)具有一些用于在主线程上调用可调用函数的实用程序。