如何将GUI组件放在OpenGL屏幕中

时间:2012-10-30 18:45:45

标签: java user-interface opengl jframe jtextarea

我有一个opengl程序,我想在其中放置一个文本框/区域,但我不确定是否有办法在我的opengl窗口中执行此操作。有没有办法做到这一点或嵌入OR或者可能将opengl窗口嵌入到JFrame中?

1 个答案:

答案 0 :(得分:4)

当然有一种方法 - 您可以编写自己的GUI元素来渲染OpenGL。至于你是否可以将它嵌入到JFrame中 - 这取决于你使用的库,在LWJGL中你可以使用Display.setParent执行此操作 - 它需要一个awt.Canvas,我不会知道你是否可以使用Swing组件。

我认为可以这样做:

import java.awt.Canvas;

import javax.swing.JFrame;
import javax.swing.JTextField;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class CanvasTest {
    public static void main(String[] args) throws LWJGLException, InterruptedException {
        // note that this is a very bare bones
        // proof-of-concept thing. You'd want to 
        // install your own close handlers etc here.
        Canvas openglSurface = new Canvas();
        JFrame frame = new JFrame();
        frame.setSize(800, 800);
        frame.add(openglSurface);
        frame.setVisible(true);
        frame.add(new JTextField("Hello World!"));
        openglSurface.setSize(500, 500);
        Display.setParent(openglSurface);
        Display.create();
        GL11.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        Display.update();
        Thread.sleep(2000);
        Display.destroy();
    }
}

我实际上并没有尝试这个,因为我自己从不需要这个,但它可能会起作用。

注意:我现在尝试了。它确实有效,但显然需要一些额外的工作才能使它与LayoutManagers等一起发挥作用。