import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import java.util.Random;

public class JOGLMSAA extends JFrame implements GLEventListener {

    GLAutoDrawable fCanvas;

    static boolean sUseGLCanvas = false;
    private int _numTriangles;
    private float[] _vertices;
    private float[] _colors;


    JOGLMSAA(int x, int y, int w, int h) {
        setBounds(x, y, w, h);
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    if (fCanvas != null) {
                        fCanvas.destroy();
                    }
                    System.exit(0);
                }
            });

        //setLayout(null);
        MyPanel panel = new MyPanel(new BorderLayout());
        getContentPane().add(panel, BorderLayout.CENTER);

        GLCapabilities caps = new GLCapabilities(null);
        caps.setSampleBuffers(true);
        caps.setNumSamples(8);

        if (sUseGLCanvas) {
            System.out.println("GLCanvas");
            fCanvas = new GLCanvas(caps);
        }
        else {
            System.out.println("GLJPanel");
            fCanvas = new GLJPanel(caps);
        }
        fCanvas.addGLEventListener(this);
        fCanvas.setAutoSwapBufferMode(true);
        panel.add((Component)fCanvas, BorderLayout.CENTER);

        Random r = new Random();
        _numTriangles = 20;
        _colors = new float[_numTriangles*3];
        _vertices = new float[_numTriangles*3*3];
        int ci = 0;
        int vi = 0;
        for (int i = 0; i < _numTriangles; ++i) {
            _colors[ci++] = r.nextFloat();
            _colors[ci++] = r.nextFloat();
            _colors[ci++] = r.nextFloat();
            for (int j = 0; j < 3; ++j) {
                _vertices[vi++] = r.nextFloat()*2.0f - 1.0f;
                _vertices[vi++] = r.nextFloat()*2.0f - 1.0f;
                _vertices[vi++] = r.nextFloat()*2.0f - 1.0f;
            }
        }
    }

    public void setBounds(int x, int y, int w, int h) {
    	System.err.println("bounds " + x + " " + y + " " + w + " " + h);
    	super.setBounds(x, y, w, h);
    }
    
    public class MyPanel extends JPanel {
    	public MyPanel(LayoutManager layout) {
    		super(layout);
    	}
    	
    	public void setBounds(int x, int y, int w, int h) {
	    	//System.err.println("mypanel bounds " + x + " " + y + " " + w + " " + h);
	    	super.setBounds(x, y, w, h);
	    }
    }
    
    public void init(GLAutoDrawable d) {
        final GL2 gl = d.getGL().getGL2();
        gl.glClearColor(1.0f, 1.0f, 0.5f, 1.0f);
    }

    public void display(GLAutoDrawable d) {
        final GL2 gl = d.getGL().getGL2();
        gl.glClearColor(1.0f, 1.0f, 0.5f, 1.0f);
        gl.glClearDepth(1.0);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();

        gl.glBegin(GL.GL_TRIANGLES);
        int ci = 0;
        int vi = 0;
        for (int i = 0; i < _numTriangles; ++i) {
            gl.glColor3f(_colors[ci++], _colors[ci++], _colors[ci++]);
            gl.glVertex3f(_vertices[vi++], _vertices[vi++], _vertices[vi++]);
            gl.glVertex3f(_vertices[vi++], _vertices[vi++], _vertices[vi++]);
            gl.glVertex3f(_vertices[vi++], _vertices[vi++], _vertices[vi++]);
        }
        gl.glEnd();
    }

    public void reshape(GLAutoDrawable d,int x,int y,int w,int h) {
        final GL2 gl = d.getGL().getGL2();
	gl.glViewport(x, y, w, h);
    }

    public void dispose(GLAutoDrawable d) {
    }

    public void update(Graphics g) {
        paint(g);
    }

    public static void main(String[] args) {
        sUseGLCanvas = false;
        if (args.length > 0) {
            if (args[0].equals("c")) {
                sUseGLCanvas = true;
            }
        }

        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    final JOGLMSAA frame = new JOGLMSAA(400, 400, 600, 400);
                    frame.setVisible(true);
                }
            });
    }
}
