
// External imports
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.GLCanvas;
import javax.swing.JButton;

/**
 * Example application that demonstrates how to put together a single-threaded
 * rendering system.
 */
public class JoglDisposeApp extends Frame
    implements GLEventListener
{
    private GLAutoDrawable canvas;
    private double theta = 0;
    private double s = 0;
    private double c = 0;
    private JButton removeButton = new JButton("Remove Renderer");

    public JoglDisposeApp()
    {
        super("Basic JOGL Demo");

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        setLayout(new BorderLayout());

        setSize(853, 880);
        setLocation(40, 40);

        // Need to set visible first before starting the rendering thread due
        // to a bug in JOGL. See JOGL Issue #54 for more information on this.
        // http://jogl.dev.java.net
        setVisible(true);

        setupJOGL();
    }

    //---------------------------------------------------------------
    // Methods defined by GLEventListener
    //---------------------------------------------------------------

    /**
     * Called by the drawable immediately after the OpenGL context is
     * initialized; the GLContext has already been made current when
     * this method is called.
     *
     * @param drawable The display context to render to
     */
    public void init(GLAutoDrawable drawable)
    {

    }

    /**
     * Called by the drawable when the surface resizes itself. Used to
     * reset the viewport dimensions.
     *
     * @param drawable The display context to render to
     */
    public void reshape(GLAutoDrawable drawable,
                        int x,
                        int y,
                        int width,
                        int height)
    {
    }

    /**
     * Called by the drawable when the display mode or the display device
     * associated with the GLDrawable has changed
     */
    public void displayChanged(GLAutoDrawable drawable,
                               boolean modeChanged,
                               boolean deviceChanged)
    {
    }

    /**
     * Called by the drawable to perform rendering by the client.
     *
     * @param drawable The display context to render to
     */
    public void display(GLAutoDrawable drawable)
    {
        update();
        render(drawable);            
    }

    private void update() {
        theta += 0.01;
        s = Math.sin(theta);
        c = Math.cos(theta);
    }

    private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);      

        // draw a triangle filling the window
        gl.glBegin(GL2.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();
        
        gl.glColor3f(1,1, 1);
        gl.glShadeModel(GL2.GL_FLAT);
        gl.glBegin(GL.GL_LINES);
        gl.glVertex2d(-c, -c);
        gl.glVertex2d(0, c); 
        gl.glVertex2d(0, c);
        gl.glVertex2d(s, -s);

        gl.glVertex2d(s, -s);
        gl.glVertex2d(-c, -c);
        gl.glEnd();
        
        
    }    
    
    
    //---------------------------------------------------------------
    // Local methods
    //---------------------------------------------------------------

    /**
     * Create the basics of the JOGL screen details.
     */
    private void setupJOGL()
    {
        GLProfile glp = GLProfile.get(GLProfile.GL2);        
        GLCapabilities capabilities = new GLCapabilities(glp);                    
//        capabilities.setDoubleBuffered(true);
//        capabilities.setHardwareAccelerated(true);
//        capabilities.setSampleBuffers(true);
//        capabilities.setStencilBits(1);

        // Works...
//        canvas = new GLJPanel(capabilities);        

        // Crashes JVM...
        canvas = new GLCanvas(capabilities);        
        
        this.add((Component) canvas, BorderLayout.CENTER);
        this.add((Component) removeButton, BorderLayout.SOUTH);

        canvas.addGLEventListener(this);
        
        final Frame frame = this;
        
        removeButton.addActionListener(new ActionListener()
        {            
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // TODO Auto-generated method stub
                frame.remove((Component) canvas);
            }
        });
        
        
    }

    public static void main(String[] args)
    {
        JoglDisposeApp demo = new JoglDisposeApp();
        demo.setVisible(true);
    }

    @Override
    public void dispose(GLAutoDrawable arg0)
    {
        // TODO Auto-generated method stub
        
    }
}
