/* This shows a small square on the canvas.  There are sliders 
 * for controlling position, a set of radio buttons for color
 * and a Quit button for terminating the prgram.
 */

	
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 SimpleGUI implements GLEventListener, ActionListener, ChangeListener {
	
	public static void main(String[] args) {
		new SimpleGUI();
	}
	
	private int INITIAL_WIDTH=300; 
	private int INITIAL_HEIGHT=300;
	private float x=2f;
	private float y=2f;
	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;
	

	public  SimpleGUI() {
			GLProfile glp=GLProfile.getDefault();
			GLCapabilities caps = new GLCapabilities(glp);
			canvas = new GLCanvas(caps);
			JFrame frame = new JFrame("Simple GUI");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
			frame.setSize(INITIAL_WIDTH, INITIAL_HEIGHT);
			frame.setLayout(new BorderLayout());
			
			JPanel north = new JPanel( new BorderLayout());
			JPanel northWest = new JPanel( new BorderLayout());
			JPanel topRow = new JPanel( new FlowLayout(FlowLayout.LEADING));
			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(greenButton);
			northEast.add(blueButton);
			northEast.add(yellowButton);
			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()/10f;
		}
		else if (e.getSource() == ySlider) {
			y = ySlider.getValue()/10f;
		}
	}


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

	
	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(x, y);
			gl.glVertex2f(x, y+side);
			gl.glVertex2f(x+side, y+side);
			gl.glVertex2f(x+side, y);
		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, 0, 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); 
	}
	
}
