// This is just like SimpleGUI, only the square rotates.
	
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
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 SimpleGUIWithRotation implements GLEventListener, ActionListener, ChangeListener {
	
	public static void main(String[] args) {
		new SimpleGUIWithRotation();
	}
	
	private int INTITIAL_WIDTH=1000;
	private int INITIAL_HEIGHT=1000;
	private float XSCALE=10f;
	private float YSCALE=10f;
	private float x=5f;
	private float y=5f;
	private float side=1f;
	JButton quitButton;
	JSlider xSlider, ySlider;
	private GLCanvas canvas;
	private GL2 gl;
	private GLU glu;
	private JRadioButton redButton;
	private JRadioButton yellowButton;
	private JRadioButton greenButton;
	private JRadioButton blueButton;
	private JRadioButton violetButton;
	private float red = 1f;
	private float green = 0f;
	private float blue = 0f;
	private float theta = 0f;
	

	public  SimpleGUIWithRotation() {
			GLProfile glp=GLProfile.getDefault();
			GLCapabilities caps = new GLCapabilities(glp);
			canvas = new GLCanvas(caps);
			JFrame frame = new JFrame("");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
			frame.setSize(800, 800);
			frame.setLayout(new BorderLayout());
			
			JPanel north = new JPanel( new BorderLayout());
			JPanel northWest = new JPanel( new BorderLayout());
			JPanel topRow = new JPanel( new GridLayout(1, 2));
			
			JLabel label = new JLabel("SimpleGUI");
			topRow.add(label);
			quitButton = new JButton( "Quit");
			quitButton.addActionListener(this);
			topRow.add(quitButton);
			northWest.add(topRow, BorderLayout.NORTH);
			JPanel secondRow = new JPanel(new BorderLayout());
			JPanel thirdRow = new JPanel( new BorderLayout());
			
			xSlider = new JSlider(0, 100);
			xSlider.setValue((int)(x*10));
			xSlider.addChangeListener(this);
			JLabel xSliderName = new JLabel("   x");
			secondRow.add(xSliderName, BorderLayout.WEST);
			secondRow.add(xSlider, BorderLayout.EAST);
			
			ySlider = new JSlider(0, 100);
			ySlider.setValue((int)(y*10));
			ySlider.addChangeListener(this);
			JLabel ySliderName = new JLabel( "   y");
			thirdRow.add(ySliderName, BorderLayout.WEST);
			thirdRow.add(ySlider, BorderLayout.EAST);
			JLabel spacer = new JLabel( "   ");
			thirdRow.add(spacer, BorderLayout.SOUTH);

			northWest.add(secondRow, BorderLayout.CENTER);
			northWest.add(thirdRow, BorderLayout.SOUTH);
			north.add(northWest, BorderLayout.WEST);
			
			
			JPanel northEast = new JPanel(new GridLayout(5,1));
			redButton = new JRadioButton( "red", true);
			yellowButton = new JRadioButton( "yellow");
			greenButton = new JRadioButton( "green");
			blueButton = new JRadioButton( "blue");
			violetButton = new JRadioButton( "violet");
			ButtonGroup radioButtons = new ButtonGroup();
			radioButtons.add(redButton);
			radioButtons.add(yellowButton);
			radioButtons.add(greenButton);
			radioButtons.add(blueButton);
			radioButtons.add(violetButton);
			redButton.addActionListener( this );
			yellowButton.addActionListener( this );
			greenButton.addActionListener( this );
			blueButton.addActionListener( this );
			violetButton.addActionListener( this );
			redButton.setSelected(true);
			northEast.add(redButton);
			northEast.add(yellowButton);
			northEast.add(greenButton);
			northEast.add(blueButton);
			northEast.add(violetButton);
			north.add(northEast, BorderLayout.CENTER);
			JPanel center = new JPanel(new GridLayout(1,1));
			
			center.add(canvas);
		
			frame.add(north,  BorderLayout.NORTH);
			frame.add(center, BorderLayout.CENTER);
			frame.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() == redButton) {
				red = 1f;
				green = 0f;
				blue = 0f;
			}
			else if (event.getSource() == yellowButton)	 {
				red = 1f;
				green = 1f;
				blue = 0f;
			}
			else if (event.getSource() == greenButton) {
				red = 0f;
				green = 1f;
				blue = 0f;
			}
			else if (event.getSource() == blueButton) {
				red = 0f;
				green = 0f;
				blue = 1f;
			}
			else if (event.getSource() == violetButton) {
				red = 0.6f;
				green = 0f;
				blue = 1f;
			}
	}
	
	public void stateChanged(ChangeEvent e) {
		if (e.getSource() == xSlider) {
			x = xSlider.getValue()/10.0f;
		}
		else if (e.getSource() == ySlider) {
			y = ySlider.getValue()/10.0f;
		}
	}


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

	private void update() {
		theta += 0.1;
	}
	
	private void render() {
		drawBox();
	}
	
	private void drawBox() {
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);

		gl.glColor3f(red, green, blue);
		gl.glBegin(GL2.GL_POLYGON);
			gl.glVertex2f((float)(x+side*Math.cos(theta)), (float)(y+side*Math.sin(theta)));
			gl.glVertex2f((float)(x+side*Math.cos(theta+Math.PI/2)), (float)(y+side*Math.sin(theta+Math.PI/2)));
			gl.glVertex2f((float)(x+side*Math.cos(theta+Math.PI)), (float)(y+side*Math.sin(theta+Math.PI)));
			gl.glVertex2f((float)(x+side*Math.cos(theta+3*Math.PI/2)), (float)(y+side*Math.sin(theta+3*Math.PI/2)));
		gl.glEnd();	
	
	}
	
	
	public void dispose(GLAutoDrawable drawable) {
		// put the cleanup code here
	}

	public void init(GLAutoDrawable drawable) {
		gl = drawable.getGL().getGL2();
		glu = new GLU();
		gl.glClearColor(1,  1, 1, 1);
	}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
		// this is called when the window is resized
		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluOrtho2D(0f, 10f, 0f, (10f*height)/width); 
	}
	
}
