使用swingnode在JavaFX框架内进行JOGL

时间:2018-08-09 14:46:36

标签: java javafx jogl

当我尝试在JOGL应用程序中实现SwingNode以便可以使用JavaFX时,将显示JavaFX窗口,但不显示OpenGL,并且在打开窗口大约2秒钟后,我会收到此错误。

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.security.AccessController.getContext(AccessController.java:820)
at java.awt.Component.<init>(Component.java:477)
at java.awt.Canvas.<init>(Canvas.java:57)
at com.jogamp.opengl.awt.GLCanvas.<init>(GLCanvas.java:261)
at com.jogamp.opengl.awt.GLCanvas.<init>(GLCanvas.java:231)
at com.rs1.app.GL.GLMaster.<init>(GLMaster.java:56)
at com.rs1.app.GL.GLMaster.<init>(GLMaster.java:57)
at com.rs1.app.GL.GLMaster.<init>(GLMaster.java:57)
at com.rs1.app.GL.GLMaster.<init>(GLMaster.java:57)
at com.rs1.app.GL.GLMaster.<init>(GLMaster.java:57)

这是我的代码:

GLMaster.java

            package com.rs1.app.GL;

            import com.jogamp.opengl.*;
            import com.jogamp.opengl.awt.GLCanvas;

            import javax.swing.*;

            public class GLMaster extends JComponent implements GLEventListener {

                @Override
                public void init(GLAutoDrawable arg0)
                {

                }

                @Override
                public void display(GLAutoDrawable drawable) {
                    final GL2 gl = drawable.getGL().getGL2();
                    //Draw H
                    gl.glBegin(GL2.GL_LINES);
                    gl.glVertex2d(-0.8, 0.6);
                    gl.glVertex2d(-0.8, -0.6);
                    gl.glVertex2d(-0.8, 0.0);
                    gl.glVertex2d(-0.4, 0.0);
                    gl.glVertex2d(-0.4, 0.6);
                    gl.glVertex2d(-0.4, -0.6);
                    gl.glEnd();
            //Draw W
                    gl.glBegin(GL2.GL_LINES);
                    gl.glVertex2d(0.4,0.6);
                    gl.glVertex2d(0.4,-0.6);
                    gl.glVertex2d(0.4,-0.6);
                    gl.glVertex2d(0.6,0);
                    gl.glVertex2d(0.6,0);
                    gl.glVertex2d(0.8,-0.6);
                    gl.glVertex2d(0.8,-0.6);
                    gl.glVertex2d(0.8,0.6);
                    gl.glEnd();
                }
                @Override
                public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4)
                {

                }
                @Override
                public void dispose(GLAutoDrawable arg0)
                {

                }

                public GLMaster() {

                    final GLProfile gp = GLProfile.get(GLProfile.GL2);
                    GLCapabilities cap = new GLCapabilities(gp);

                    final GLCanvas gc = new GLCanvas(cap);
                    GLMaster sq = new GLMaster();
                    gc.addGLEventListener(sq);
                    gc.setSize(400, 400);

                    final JFrame frame = new JFrame("Hello World");
                    frame.add(gc);
                    frame.setSize(500,400);
                    frame.setVisible(true);
                }
            }

JFXFrame.java

            package com.rs1.app.swingnode;

            import com.rs1.app.GL.GLMaster;
            import javafx.application.Application;
            import javafx.embed.swing.SwingNode;
            import javafx.scene.Scene;
            import javafx.scene.layout.StackPane;
            import javafx.stage.Stage;

            import javax.swing.*;

            public class JFXFrame extends Application {

                private void createSwingContent(final SwingNode swingNode) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            swingNode.setContent(
                                    new GLMaster()
                            );
                        }
                    });
                }

                @Override
                public void start(Stage stage) {
                    final SwingNode swingNode = new SwingNode();

                    createSwingContent(swingNode);

                    StackPane pane = new StackPane();
                    pane.getChildren().add(swingNode);

                    stage.setTitle("Swing in JavaFX");
                    stage.setScene(new Scene(pane, 500, 400));
                    stage.show();
                }

                public static void main(String args[]){
                    launch(args);
                }
            }

任何帮助或见识将不胜感激!

0 个答案:

没有答案