Results 1 to 2 of 2
  1. #1
    timmycafe is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Question How to attach opengl display to a JFrame and dispose of it properly?

    I am new to this website and i am needing some help. How can i attach the OpenGl display to a JFrame and so that when i close the JFrame is destroys the display? Here is my code so far:

    Java Code:
    package test.core;
    
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    import org.lwjgl.LWJGLException;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    import org.lwjgl.opengl.GL11;
    
    import static org.lwjgl.opengl.GL11.*;
    
    public class Main {
    
    
    
        private static CreateCanvas canvas;
        private static CreateFrame frame;
    
        private static int width = 800;
        private static int height = 600;
    
        public static void main(String[] args) throws InterruptedException {
            startFrames();
    
            startDisplay();
    
        }
    
        public static void cleanUp() {
            Display.destroy();
        }
    
        private static void startDisplay() {
            try
            {
                Display.setParent(canvas);
                Display.create();
            }catch(LWJGLException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        private static void startFrames()
        {
            Runnable r = new Runnable(){
                @Override
                public void run(){
                    frame = new CreateFrame();
                    JButton button = new JButton("BUTTON");
                    canvas = new CreateCanvas();
                    JPanel panel = frame.panel;
    
                    panel.add(canvas);
                    panel.add(button);
                    frame.add(panel);
    
                    canvas.setSize(300, 300);
                    frame.setSize(width, height);
    
                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
                    WindowListener listen = 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);
                                cleanUp();
                                frame.dispose();
                            }
                        }
                    };
    
                    frame.addWindowListener(listen);
    
    
                    frame.setVisible(true);
                }
    
            };
            SwingUtilities.invokeLater(r);
        }
    
    }
    I had the opengl display attach to the JFrame before i did the runnable. But after adding the runnable the display now shows up the same size as my screen size. I have tried rearranging the
    Java Code:
    canvas.setSize();
    and the

    Java Code:
    frame.setSize();
    but nothing changes the opengl display is still the same size and when i try to close the JFrame first rather then close the display first i get this error:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: From thread Thread[AWT-EventQueue-0,6,main]: Thread[main,5,main] already has the context current
    which points me to my

    Java Code:
    Display.destroy();
    which im guessing is telling me that i am not properly disposing the display? Can anyone help me attach the opengl display to the JFrame and fix the error above?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to attach opengl display to a JFrame and dispose of it properly?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JFrame dispose() works only first time
    By StainedWolf in forum AWT / Swing
    Replies: 10
    Last Post: 08-16-2014, 12:02 AM
  2. JFrame not responding to dispose() function
    By amitSingh in forum New To Java
    Replies: 15
    Last Post: 06-14-2014, 07:17 AM
  3. Dispose Jframe from another class.
    By vahshir in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 12:33 PM
  4. [SOLVED] JButton does not display ImageIcon properly
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 04-17-2009, 03:47 AM
  5. i cant dispose the jframe(urgent)
    By arunkumarinfo in forum NetBeans
    Replies: 2
    Last Post: 02-24-2009, 09:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •