
import java.awt.Button;

import javax.media.opengl.GL2;
import javax.media.opengl.GL;
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.media.opengl.fixedfunc.GLMatrixFunc;

import javax.swing.JButton;
import javax.swing.JFrame;

import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.FPSAnimator;

public class TestOverlay {

    public static void main(String args[]) throws Exception {
        System.out.println("Java version: " + System.getProperty("java.version"));
        System.out.println("Java vendor: " + System.getProperty("java.vendor"));
        new TestOverlay();
    }

    final JFrame frame = new JFrame(getClass().getSimpleName());
    final FPSAnimator animator;

    public TestOverlay() {
        GLJPanel panel = makeJPanel();
        panel.setSize(640, 480);
        frame.getContentPane().add(panel);

        Button button = new Button("Heavy");
        button.setBounds(100, 100, 100, 50);
        frame.getLayeredPane().add(button);

        JButton jbutton = new JButton("Light");
        jbutton.setBounds(225, 100, 100, 50);
        frame.getLayeredPane().add(jbutton);

        // this.pack();
        frame.setSize(640, 480);
        frame.setVisible(true);

        animator = new FPSAnimator(panel, 60);
        animator.start();
    }

    protected GLJPanel makeJPanel() {
        final GLProfile pro = GLProfile.getDefault();
        final GLCapabilities caps = new GLCapabilities(pro);
        final GLJPanel panel = new GLJPanel(caps);

        panel.addGLEventListener(new GLEventListener() {
            int quad_x = (int) (Math.random() * 640);
            int quad_y = (int) (Math.random() * 480);

            public void display(final GLAutoDrawable drawable) {
                this.quad_x = (this.quad_x + 1) % 640;
                this.quad_y = (this.quad_y + 1) % 480;

                final GL2 g2 = drawable.getGL().getGL2();
                g2.glClearColor(0.0f, 0.0f, 0.3f, 1.0f);
                g2.glClear(GL.GL_COLOR_BUFFER_BIT);

                g2.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
                g2.glLoadIdentity();
                g2.glOrtho(0, 640, 0, 480, 1, 100);
                g2.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
                g2.glLoadIdentity();
                g2.glTranslated(0, 0, -1);

                g2.glBegin(GL2.GL_QUADS);
                {
                    g2.glVertex2d(this.quad_x, this.quad_y + 10);
                    g2.glVertex2d(this.quad_x, this.quad_y);
                    g2.glVertex2d(this.quad_x + 10, this.quad_y);
                    g2.glVertex2d(this.quad_x + 10, this.quad_y + 10);
                }
                g2.glEnd();
            }

            public void dispose(final GLAutoDrawable arg0) {}
            public void init(final GLAutoDrawable arg0) {}
            public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) {}
        });

        return panel;
    }
}
