import java.awt.BorderLayout;

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.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

/**
 * This test program fills in the screen with a different color and notes that
 * color in the title bar in the order
 * 
 * Black Red Green Blue Red Green Blue . . .
 * 
 * With any of the first three settings uncommented the title and the color
 * match
 * 
 * With the final setting the initial BLACK shows garbage from video ram and
 * then the displayed color lags the title bar color by one call to repaint()
 * 
 * @author bkuker
 * 
 */
public final class Bug841 extends JFrame implements GLEventListener {

	public static void main(String[] args) {
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);

		// Any of these three work correctly, and the same
		// new Bug841(true, true);
		// new Bug841(false, true);
		// new Bug841(false, false);

		// This one is the problem one.
		new Bug841(true, false);
	}

	private float[] c = new float[3];

	public Bug841(boolean enableAA, boolean glsl) {
		super("Bug823");
		setSize(400, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		JPanel p = new JPanel(new BorderLayout());

		if (!glsl) {
			System.setProperty("jogl.gljpanel.noglsl", "true");
		}

		final GLProfile glp = GLProfile.get(GLProfile.GL2);
		final GLCapabilities caps = new GLCapabilities(glp);

		if (enableAA) {
			caps.setSampleBuffers(true);
			caps.setNumSamples(6);
		}

		final GLJPanel canvas = new GLJPanel(caps);

		canvas.addGLEventListener(this);
		p.add(canvas, BorderLayout.CENTER);
		setContentPane(p);

		setVisible(true);

		final String title = "AA: " + enableAA + " GLSL: " + glsl + " ";

		setTitle(title + "BLACK");

		new Thread() {
			public void run() {
				try {
					while (true) {
						Thread.sleep(1000);

						setTitle(title + "RED");
						c[0] = 1;
						c[1] = 0;
						c[2] = 0;
						repaint();
						Thread.sleep(1000);

						setTitle(title + "GREEN");
						c[0] = 0;
						c[1] = 1;
						c[2] = 0;
						repaint();
						Thread.sleep(1000);

						setTitle(title + "BLUE");
						c[0] = 0;
						c[1] = 0;
						c[2] = 1;
						repaint();

					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}.start();

	}

	@Override
	public void display(GLAutoDrawable drawable) {
		GL2 gl = drawable.getGL().getGL2();
		gl.glClearDepth(1.0f);
		gl.glClearColor(0, 0, 0, 1);
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

		gl.glColor3fv(c, 0);
		gl.glDisable(GL.GL_CULL_FACE);

		gl.glBegin(GL.GL_TRIANGLE_STRIP);
		gl.glNormal3f(0, 0, -1);
		gl.glTexCoord2f(0, 1);
		gl.glVertex3f(-1, -1, 0);
		gl.glTexCoord2f(1, 1);
		gl.glVertex3f(1, -1, 0);
		gl.glTexCoord2f(0, 0);
		gl.glVertex3f(-1, 1, 0);
		gl.glTexCoord2f(1, 0);
		gl.glVertex3f(1, 1, 0);
		gl.glEnd();

	}

	@Override
	public void init(GLAutoDrawable drawable) {
		// Left Blank
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		// Left Blank
	}

	@Override
	public void dispose(GLAutoDrawable drawable) {
		// Left Blank
	}

}
