It seems that you started displaying in the "main" thread (which gives the main thread the current OpenGL context), but you are trying to destroy the display from another thread, which in this case is the Theme (EDT) event dispatcher. However, only one thread can have the current OpenGL context at a given time.
Although it is possible to change which thread has the current context, I don’t think what you want to do here.
Here we want to destroy the display in the same stream in which we created it (the stream with the current OpenGL context). approach I have seen the use of methods of Canvas addNotify( ) and removeNotify() , which run on the EDT, select the check box that is checked in OpenGL stream to determine when to destroy the display.
In addition, questions about setting the display size were mentioned to the question. The size of your JFrame display and the size of the display have not been adjusted as you would like, due to the way the setSize () and LayoutManager functions. See Java tutorials and documentation for more information. In the following example, I use one approach to solve this problem.
So, here is an example that is trying to get closer to the goal of the code posted in the question:
import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; public class LWJGLTester { private volatile boolean isRunning = false; private int frameWidth = 800; private int frameHeight = 600; private int displayWidth = 300; private int displayHeight = 300; private Thread glThread; public static void main(String[] args) { new LWJGLTester().runTester(); } private void runTester() { final JFrame frame = new JFrame("LWJGL in Swing"); frame.setSize(frameWidth, frameHeight); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent we){ int result = JOptionPane.showConfirmDialog(frame, "Do you want to quit the Application?"); if(result == JOptionPane.OK_OPTION){ frame.setVisible(false); frame.dispose();
Note. This example was tested using lwjgl version 2.9.1, as it seemed to be the latest version available at the time the question was originally published.
source share