0

Swing で JOGL を正しく動作させようとしています。私は WindowBuilder を使用し、いくつかの JOGL の例と一緒にハックしました。正常に動作しますが、問題があります。プログラムを起動すると、空白のウィンドウが表示されます。Swing コンポーネントも表示されません。

ここに画像の説明を入力

ウィンドウのコンテンツ上にマウスカーソルを移動するか、別のイベントで再描画が強制されると、すべてが機能し始めます-フォーカスの変更、ウィンドウの移動、サイズ変更など.

これが私のコードです:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jogamp.opengl.util.Animator;

public class OpenGLTestMin {
    private JFrame frame;
    private Animator animator;
    private double theta = 0;
    private double s = 0;
    private double c = 0;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OpenGLTestMin window = new OpenGLTestMin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public OpenGLTestMin() {
        frame = new JFrame();

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowevent) {
                animator.stop();
                frame.dispose();
                System.exit(0);
            }
        });

        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(640, 480);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0};
        gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        frame.getContentPane().setLayout(gridBagLayout);

        JPanel panelMain = new JPanel();
        panelMain.setBorder(new EmptyBorder(5, 5, 5, 5));
        GridBagConstraints gbc_panelMain = new GridBagConstraints();
        gbc_panelMain.fill = GridBagConstraints.BOTH;
        gbc_panelMain.gridx = 0;
        gbc_panelMain.gridy = 0;
        frame.getContentPane().add(panelMain, gbc_panelMain);
        GridBagLayout gbl_panelMain = new GridBagLayout();
        gbl_panelMain.columnWidths = new int[]{0, 0, 0};
        gbl_panelMain.rowHeights = new int[]{0, 0};
        gbl_panelMain.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
        gbl_panelMain.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panelMain.setLayout(gbl_panelMain);

        JPanel panelButton = new JPanel();
        GridBagConstraints gbc_panelButton = new GridBagConstraints();
        gbc_panelButton.fill = GridBagConstraints.BOTH;
        gbc_panelButton.gridx = 1;
        gbc_panelButton.gridy = 0;
        panelMain.add(panelButton, gbc_panelButton);
        GridBagLayout gbl_panelButton = new GridBagLayout();
        gbl_panelButton.columnWidths = new int[]{0, 0};
        gbl_panelButton.rowHeights = new int[]{0, 0, 0};
        gbl_panelButton.columnWeights = new double[]{0.0, Double.MIN_VALUE};
        gbl_panelButton.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        panelButton.setLayout(gbl_panelButton);

        JButton btn1 = new JButton("Button 1");
        GridBagConstraints gbc_btn1 = new GridBagConstraints();
        gbc_btn1.insets = new Insets(0, 0, 5, 0);
        gbc_btn1.gridx = 0;
        gbc_btn1.gridy = 0;
        panelButton.add(btn1, gbc_btn1);

        JButton btn2 = new JButton("Button 2");
        GridBagConstraints gbc_btn2 = new GridBagConstraints();
        gbc_btn2.gridx = 0;
        gbc_btn2.gridy = 1;
        panelButton.add(btn2, gbc_btn2);


        GLProfile glprofile = GLProfile.getDefault();
        GLCapabilities glcapabilities = new GLCapabilities(glprofile);
        GLJPanel glcanvas = new GLJPanel(glcapabilities);

        glcanvas.addGLEventListener(new GLEventListener() {
            @Override
            public void reshape(GLAutoDrawable glautodrawable, int x, int y, int width, int height) {
            }

            @Override
            public void init(GLAutoDrawable glautodrawable) {
            }

            @Override
            public void dispose(GLAutoDrawable glautodrawable) {
            }

            @Override
            public void display(GLAutoDrawable glautodrawable) {
                theta += 0.01;
                s = Math.sin(theta);
                c = Math.cos(theta);

                GL2 gl = glautodrawable.getGL().getGL2();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                gl.glBegin(GL.GL_TRIANGLES);
                gl.glColor3f(1, 0, 0);
                gl.glVertex2d(-c, -c);
                gl.glColor3f(0, 1, 0);
                gl.glVertex2d(0, c);
                gl.glColor3f(0, 0, 1);
                gl.glVertex2d(s, -s);
                gl.glEnd();
            }
        });

        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.insets = new Insets(0, 0, 0, 5);
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        panelMain.add(glcanvas, gbc_panel);

        animator = new Animator(glcanvas);
        animator.start();
    }
}

これを修正する方法はありますか?私はWindows 7を使用しています。

4

1 に答える 1

1

私は解決策を見つけました。ではなく、メソッドでGLJPanel直接初期化する必要があります。その後、問題なく動作します。また、よりもはるかに優れたパフォーマンスを発揮しますが、ウィンドウのサイズを変更するときに少し不具合が発生することがあります。mainEventQueueGLCanvasGLJPanel

于 2013-10-29T13:36:39.183 に答える