// This is like the FrameTester program, only the control
// panel is in a separate window.
	
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;



public class TwoFrameTester implements GLEventListener, ActionListener, ChangeListener {
	
	public static void main(String[] args) {
		new TwoFrameTester();
	}
	
	private double theta = 0;
	private double inc = 0.01;
	JButton quitButton, pauseButton, restartButton;
	JSlider speedSlider;
	private GLCanvas canvas;
	private GL2 gl;

	public  TwoFrameTester() {
			GLProfile glp=GLProfile.getDefault();
			GLCapabilities caps = new GLCapabilities(glp);
			canvas = new GLCanvas(caps);
			
			JFrame frame = new JFrame("controls");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
			frame.setSize(300, 80);
			frame.setLocation(400, 100);
			frame.setLayout(new BorderLayout());
			
			JPanel north = new JPanel( new GridLayout(2, 1));
			JPanel topRow = new JPanel( new GridLayout(1, 3));
			
			quitButton = new JButton( "Quit");
			quitButton.addActionListener(this);
			
			pauseButton = new JButton( "Pause");
			pauseButton.addActionListener(this);
			
			restartButton = new JButton( "Resume");
			restartButton.addActionListener(this);
			
			speedSlider = new JSlider(1, 100);
			speedSlider.setValue(4);
			speedSlider.addChangeListener(this);
			
			topRow.add( quitButton);
			topRow.add( pauseButton);
			topRow.add(restartButton);
			north.add(topRow);
			
			JPanel nextRow = new JPanel();
			nextRow.add(speedSlider);
			north.add(nextRow);
	
			frame.add(north,  BorderLayout.NORTH);
			frame.setVisible(true);
			
			Frame frame1 = new Frame("Canvas");
			frame1.setSize(300, 300);
			frame1.setLocation(100, 100);
			frame1.add(canvas);
			frame1.setVisible(true);
			canvas.addGLEventListener(this);
			
			FPSAnimator animator = new FPSAnimator(canvas, 60);
			animator.start(); 
				

	}
	public void actionPerformed(ActionEvent event) {
			if (event.getSource() == quitButton)
				System.exit(0);
			else if (event.getSource() == pauseButton)
				inc = 0.0;
			else if (event.getSource() == restartButton)
				inc = speedSlider.getValue()/400.0;
	}
	
	public void stateChanged(ChangeEvent e) {
		inc = speedSlider.getValue()/400.0;
	}

	public void display(GLAutoDrawable drawable) {
		update();
		render();
	}

	private void update() {
		theta += inc;
	}
	
	private void render() {
		float x = (float) Math.cos(theta);
		float y = (float) Math.sin(theta);
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		//draw a triangle filling the window
		gl.glBegin(GL.GL_TRIANGLES);
		gl.glColor3f(1, 0, 0);
		gl.glVertex2f(-x, -x);
		gl.glColor3f(0, 1, 0);
		gl.glVertex2f(0,x);
		gl.glColor3f(0, 0, 1);
		gl.glVertex2f(y, -y);
		gl.glEnd();
	}
	
	
	public void dispose(GLAutoDrawable drawable) {
		// put the cleanup code here	
	}

	public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL().getGL2();
	}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		// this is called when the window is resized	
	}
	
}
