我试图让JOGL与Swing一起正常工作。我使用了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上。
答案 0 :(得分:1)
我找到了解决方案。 GLJPanel
应直接在main
方法中初始化,而不是EventQueue
。然后它工作得很好。另请注意,GLCanvas
的性能比GLJPanel
好很多,但在调整窗口大小时有时会出现一些问题。