import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
 * An application that implements image Morphing as described in....
 * The features of this application are ...
 * Use this application by ...
 * Special Notes: (restrictions, assumptions etc.)
 */
public class MorphGUI extends JFrame {
	public static void main(String [] args){
		MorphGUI mg=new MorphGUI();
	}

	// ImageFrame s1, s2, interpolant, s1toInterpolant, s2toInterpolant;

	/**
	 *
	 */
	public MorphGUI(){
		super("Morph");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// s1=new ImageFrame("s1"); s1.setVisible(true); 
		// s2=new ImageFrame("s2"); s2.setVisible(true);

                getContentPane().add(commonMorphParametersGUI(), BorderLayout.NORTH);

		// Create the tabbed pane 
		// Single Image Morph, Two Image Morph, Unattended Morphing
		JTabbedPane tp=new JTabbedPane();

		tp.addTab("Single Image Morph", null, singleImageMorphGUI(),null);

		tp.addTab("Two Image Morph", null, twoImageMorphGUI(),null);

		tp.addTab("Movie", null, movieImageMorphGUI(),null);

		// Add the tabbed pane to the JFrame
                getContentPane().add(tp, BorderLayout.CENTER);
		
		pack();
		setVisible(true);
	}

	/**
	 * Create and return a JPanel with Common Morph Parameters.
	 * These are Morph parameters are applicable to all morphing modes
	 * Single Image Morph (actually known as image warping) and Two Image Morph.
	 * The common parameters are Anti-Alias option, weight function parameters (a,b,p)
	 * @return a JPanel with Common Morph Parameters
	 */
	private JPanel commonMorphParametersGUI(){
		JPanel p=new JPanel();
		p.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder("Common Parameters"),
			BorderFactory.createEmptyBorder(5,5,5,5)));
		p.setLayout(new GridLayout(0, 1));

		// A collection of TextField/Label controls
		// for the weight function parameters a, b, and p
		int textFieldWidth=3;
		JPanel pWeightParameters=new JPanel();

			JTextField tfA=new JTextField(textFieldWidth);
			JLabel lblA=new JLabel(" a:");
			pWeightParameters.add(lblA);
			pWeightParameters.add(tfA);

			JTextField tfB=new JTextField(textFieldWidth);
			JLabel lblB=new JLabel(" b:");
			pWeightParameters.add(lblB);
			pWeightParameters.add(tfB);

			JTextField tfP=new JTextField(textFieldWidth);
			JLabel lblP=new JLabel(" p:");
			pWeightParameters.add(lblP);
			pWeightParameters.add(tfP);

		p.add(pWeightParameters);

		JCheckBox cbAntiAlias=new JCheckBox("Anti-Alias");
		p.add(cbAntiAlias);

		JCheckBox cbControlVectors=new JCheckBox("Show Control Vectors");
		p.add(cbControlVectors);

		return p;
	}

	/**
	 * Generate the single image morph GUI (aka Image Warping), 
	 * a button to Morph S1 into S2 and a Save button for saving S2.
	 * @return a JPanel with a Morph and Save button
	 */
	private JPanel singleImageMorphGUI(){
		JPanel p=new JPanel();
		p.setLayout(new BorderLayout());

			// Morph and Save buttons
			JPanel pSouth=new JPanel();
			JButton b=new JButton("Morph S1 to S2");
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){ morphS1toS2(); }
			});
			pSouth.add(b);
	
			b=new JButton("Save S2");
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){  save(); }
			});
			pSouth.add(b);

		p.add(pSouth, BorderLayout.SOUTH);

		return p;
	}

	/**
	 * Generate the two image morph GUI, 
	 * this allows the user to specify whether to display
	 * the final result
	 * the morph from s1 to the interpolant
	 * the morph from s2 to the interpolant
	 * it also includes the t (interpolant) parameter to determine
	 * how close the interpolant image is to s1 (t=0) or s2 (t=1)
	 * and a Morph and Save button for saving the final result
	 */
	private JPanel twoImageMorphGUI(){

		// p consists of the View switches followed by
		// the t parameter slider and finally the Morph and Save buttons

		JPanel p=new JPanel();
		p.setLayout(new BorderLayout());

			// Parameters
			JPanel pCenter=new JPanel();
			pCenter.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Options"),
				BorderFactory.createEmptyBorder(5,5,5,5)));
			pCenter.setLayout(new GridLayout(0, 1));
	
			JCheckBox cbFinalResult=new JCheckBox("View Final Result");
			JCheckBox cbS1toInterpolant=new JCheckBox("View S1 to interpolant morph");
			JCheckBox cbS2toInterpolant=new JCheckBox("View S2 to interpolant morph");
	
			pCenter.add(cbFinalResult);
			pCenter.add(cbS1toInterpolant);
			pCenter.add(cbS2toInterpolant);

			// t (interpolant) parameter
			// A slider here would be nice
			JPanel pT=new JPanel();
				JTextField tfT=new JTextField(5);
				JLabel lblT=new JLabel(" t:");
			pT.add(lblT);
			pT.add(tfT);

			pCenter.add(pT);
	
		p.add(pCenter,BorderLayout.CENTER);

			// Morph and Save buttons
			JPanel pSouth=new JPanel();
			JButton b=new JButton("Morph");
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){ morphS1andS2toInterpolant(); }
			});
			pSouth.add(b);
	
			b=new JButton("Save");
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){  save(); }
			});
			pSouth.add(b);

		p.add(pSouth,BorderLayout.SOUTH);

		return p;
	}

	/**
	 * Generate the movie Image Morph GUI, 
	 * users of this GUI create a Movie (morph between s1 and s2) by specifying
	 * a Directory in which to save the images comprising the movie,
	 * a file name prefix used to name all the images
	 * number of frames (that is, the number of images comprising
	 * the movie)
	 */
	private JPanel movieImageMorphGUI(){

		JPanel p=new JPanel();
		p.setLayout(new BorderLayout());
		
			JPanel pCenter=new JPanel();
			pCenter.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Options"),
				BorderFactory.createEmptyBorder(5,5,5,5)));
			pCenter.setLayout(new GridLayout(0,1));

			JPanel pTemp=new JPanel(); 
				JButton bDirectory=new JButton("Directory");
				JTextField tfDirectory=new JTextField(10);
				pTemp.add(bDirectory);
				pTemp.add(tfDirectory);
			pCenter.add(pTemp);

			pTemp=new JPanel(); 
				JLabel lblFileNamePrefix=new JLabel("File Name Prefix:");
				JTextField tfFileNamePrefix=new JTextField(10);
				pTemp.add(lblFileNamePrefix);
				pTemp.add(tfFileNamePrefix);
			pCenter.add(pTemp);

			pTemp=new JPanel(); 
				JLabel lblNumFrames=new JLabel("Number of Frames:");
				JTextField tfNumFrames=new JTextField(10);
				pTemp.add(lblNumFrames);
				pTemp.add(tfNumFrames);
			pCenter.add(pTemp);
		
		p.add(pCenter,BorderLayout.CENTER);

			// Morph
			JPanel pSouth=new JPanel();
			JButton b=new JButton("Morph");
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){ morphS1andS2Movie(); }
			});
			pSouth.add(b);
	
		p.add(pSouth,BorderLayout.SOUTH);

		return p;
	}

	/**
	 * morph the image in S1 into S2. The resulting Image is in S2.
	 */
	private void morphS1toS2(){ 
		// Morph.morph(s1.getImagePanel().getMorphImage(), s2.getImagePanel().getMorphImage()); 
		// s2.repaint();
	}

	/**
	 * morph s1 and s2 into the interpolant
	 */
	private void morphS1andS2toInterpolant(){ 
		// create interpolant 
		// morph s1 to the interpolant
		// morph s2 to the interpolant
		// combine the resulting two images and store the result in the interpolant
	}

	/**
	 * morph s1 to s2 saving frames along the way
	 */
	private void morphS1andS2Movie(){ 
		// for each frame
		// 	create interpolant 
		// 	morph s1 to the interpolant
		// 	morph s2 to the interpolant
		// 	combine the resulting two images and store the result in the interpolant
		// 	store current image in file
	}

	private void save(){ 

	}
}


