
/**	Complex Number Calculator with a GUI interface

	Sample starting code
	Ivo D. Dinov
	July 02, 2002
	v. 1.0

	Compile: javac PIC20_assign4.java
	Run by:  java PIC20_assign4   [appletviewer PIC20_assign4.html]


 */


// Java extension packages

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.BadLocationException;
import java.awt.geom.Point2D;




public class PIC20_assign4 extends JApplet {

   private String operations[]={"Operation",
		"(+) Addition","(-) Subtraction",
		"(*) Multiplication", "(/) Division", 
		"(1/z) Reciprocal/Inversion", "(|z|) Norm",
		"Clear", "Exit" };

   private String whichOperation= "";

   private JPanel northPanel, inputPanel, operationPanel, resultPanel;

   private JScrollPane x_y_PlanePanel;	// DrawPanel is attached to it
   private JScrollPane operScrollPane;  // Operation Scroll Pane

   private JTextField aTF, bTF, cTF, dTF;
   private JTextField a1TF, b1TF, c1TF;
   private JTextField d1TF, eTF, fTF, operationTF, equalTF;

   private JLabel aL, bL, cL, dL, i1L, i2L, i3L, i4L, i5L;
   private JList operationsList;
  
   private DrawPanel drawingPanel;	// Drawing action panel

   private Dimension boundingBox;
   private int centerX, centerY;
   static private int width = 400, height = 200;

   static boolean isApplet = true;

   private double  a, b, c, d, a1, b1, c1, d1, e, f; // Re(z), Im(z)



   // initialize applet; set up GUI
   public void init()
   {
	if (isApplet)
	{     // Get Applet parameters (sizes) from starting HTML, if any
	    try {   width = Integer.parseInt(this.getParameter("width"));
		height = Integer.parseInt(this.getParameter("height"));
	     }
	    catch (Exception e)  {  ; }
	 }

	System.out.println("(init) Setting applet W/H to: "+
		width+ " ; " + height);

      // set up DrawPanel
      if (isApplet)
	drawingPanel = new DrawPanel( this.getSize().height-70,
			this.getSize().width-20);
      else drawingPanel = new DrawPanel( width-20, height-70);

	System.out.println("IsApplet="+isApplet+
		" width="+(width-20)+
		" height = "+(height-70)+"\n");

	// Set up Drawing Scroll panel for display of Complex #'s
      x_y_PlanePanel = new JScrollPane ();
      x_y_PlanePanel.setViewportView(drawingPanel);

      x_y_PlanePanel.setPreferredSize(new Dimension(width,50));

	// Set up the basic panels

      northPanel = new JPanel();
      inputPanel = new JPanel();
      operationPanel = new JPanel();
      resultPanel = new JPanel();
     
      setAllTextFieldPanels();

      northPanel.setLayout( new GridLayout( 1, 2 ) );
      northPanel.add(inputPanel);
      northPanel.add(operationPanel);

      // create array of buttons
      operationsList = new JList(operations);
      operationsList.setSelectedIndex(0);
      operationsList.setVisibleRowCount(3);

      operationsList.addMouseListener( new MouseAdapter() 
	   {  public void mouseClicked(MouseEvent e) {
         	 if (e.getClickCount() >= 1)
             	 {    int index = 
			   operationsList.locationToIndex(e.getPoint());
		      if (index>=1)
             	      {  System.out.println("Selected Operation("+
			   index+"): "+operationsList.getSelectedValue() );
			 switch (index)  {
				case 0: whichOperation=""; break;
				case 1: whichOperation="+"; break;
				case 2: whichOperation="-"; break;
				case 3: whichOperation="*"; break;
				case 4: whichOperation="/"; break;
				case 5: whichOperation="1/z"; break;
				case 6: whichOperation="|z|"; break;
				case 7: whichOperation="Clear"; break;
				case 8: whichOperation="Exit"; break;
			   }	
			  processOperation();
		       }
          	   }
     		}
           }
      );

      operScrollPane = new JScrollPane();
      operScrollPane.getViewport().setView(operationsList);
      operationPanel.add(operScrollPane);

      // attach components to content pane
      Container container = getContentPane();
      container.add( northPanel, BorderLayout.NORTH );
      container.add( x_y_PlanePanel, BorderLayout.CENTER );
      container.add( resultPanel , BorderLayout.SOUTH );
   }


   public void setAllTextFieldPanels()
   {	aL = new JLabel("a");
	bL = new JLabel("b");
	cL = new JLabel("c");
	dL = new JLabel("d");
	i1L = new JLabel("+ i ");
	i2L = new JLabel("+ i ");
	i3L = new JLabel("+ i ");
	i4L = new JLabel("+ i ");
	i5L = new JLabel("+ i ");

	aTF = new JTextField ();  aTF.setEditable(true);
	bTF = new JTextField ();  bTF.setEditable(true);
	cTF = new JTextField ();  cTF.setEditable(true);
	dTF = new JTextField ();  dTF.setEditable(true);
	a1TF = new JTextField (); a1TF.setEditable(true);
	b1TF = new JTextField (); b1TF.setEditable(true);
	c1TF = new JTextField (); c1TF.setEditable(true);
	d1TF = new JTextField (); d1TF.setEditable(true);
	eTF = new JTextField ();  eTF.setEditable(true);
	fTF = new JTextField ();  fTF.setEditable(true);
	operationTF = new JTextField("?");
		operationTF.setEditable(false);
	equalTF = new JTextField ("="); equalTF.setEditable(false);
	
	
	inputPanel.setLayout( new GridLayout( 2, 5 ) );
	inputPanel.add(aL);
	inputPanel.add(aTF);
	inputPanel.add(i1L);
	inputPanel.add(bTF);
	inputPanel.add(bL);
	inputPanel.add(cL);
	inputPanel.add(cTF);
	inputPanel.add(i2L);
	inputPanel.add(dTF);
	inputPanel.add(dL);
	
	resultPanel.setLayout( new GridLayout( 1, 11 ) );
	resultPanel.add(a1TF);
	resultPanel.add(i3L);
	resultPanel.add(b1TF);
	resultPanel.add(operationTF);
	resultPanel.add(c1TF);
	resultPanel.add(i4L);
	resultPanel.add(d1TF);
	resultPanel.add(equalTF);
	resultPanel.add(eTF);
	resultPanel.add(i5L);
	resultPanel.add(fTF);
	
    }

   public void processOperation()
   {	
	if (whichOperation.startsWith("Ex"))
	{	if (!isApplet)   System.exit(0);    }
	else if(whichOperation.startsWith("Op"))
	{	clearResultPanel();  }
	else if(whichOperation.startsWith("Clear"))
	{	clearInputPanel();
		clearGraphics();
		clearResultPanel();
	 }
	else
	{  operationTF.setText("  ["+whichOperation+"]");
	   drawComplexNumbers();
	 }
    }


   public void clearInputPanel()
   {
	aTF.setText("");
	bTF.setText("");
	cTF.setText("");
	dTF.setText("");
	operationsList.setSelectedIndex(0);
    }

   public void clearGraphics()
   {
 	drawingPanel.setCurrentChoice( -1 );

    }


   public void clearResultPanel()
   {
	a1TF.setText("");
	b1TF.setText("");
	c1TF.setText("");
	d1TF.setText("");
	eTF.setText("");
	fTF.setText("");
	operationTF.setText("?");
    }


   // enables application to specify width of drawing area
   public void setWidth( int newWidth )
   {
      width = ( newWidth >= 0 ? newWidth : width ); 
   }

   // enables application to specify height of drawing area
   public void setHeight( int newHeight )
   { 
      height = ( newHeight >= 0 ? newHeight : height );
   }


   public void drawComplexNumbers()
   {	Point2D.Double z1, z2, z3;

	try {
	   z1 = new Point2D.Double(
	   	Double.parseDouble(aTF.getText()),
		Double.parseDouble(bTF.getText())
	   	);
	   z2 = new Point2D.Double(
	   	Double.parseDouble(cTF.getText()),
		Double.parseDouble(dTF.getText())
	   	);

	   z3 = computeResult(z1, z2);   

	   drawingPanel.setThreeComplexNumbers(z1, z2, z3);
	 }
	catch(Exception e)
	{  System.out.println("(drawComplexNumbers()) "+
		"Can't pase out the complex numbers ..."+
		"::[aTF.getText()="+aTF.getText()+
		" bTF.getText()="+bTF.getText()+
		 "]! & [cTF.getText()="+cTF.getText()+
		" dTF.getText()="+dTF.getText()+"]!"
		);
	 }
    }


   public Point2D.Double computeResult(Point2D.Double z1,
		Point2D.Double z2)
   {
	Point2D.Double z_out = 
		new Point2D.Double(0, 0);

	if (whichOperation.startsWith("+"))
	{	} 	
	if (whichOperation.startsWith("-"))
	{	}
	if (whichOperation.startsWith("*"))
	{	}
	if (whichOperation.startsWith("/"))
	{	} 
	if (whichOperation.startsWith("1"))
	{	} 
	if (whichOperation.startsWith("|"))
	{	} 
	if (whichOperation.startsWith("Clear"))
	{	} 
	if (whichOperation.startsWith("Ex"))
	{	} 

	return z_out;
    }


   // execute applet as an application
   public static void main( String args[] )
   {
      isApplet = false;

	// Get Size of Application from command-line args
      if ( args.length == 2 ) {
         try { 	width = Integer.parseInt( args[ 0 ] );
         	height = Integer.parseInt( args[ 1 ] );
	     }
	 catch (Exception e ) { ; }
      }
 

      // create window in which applet will execute
      JFrame applicationWindow =
         new JFrame( 
	    "PIC20_assign4 - Complex Number Calculator" );

      applicationWindow.setDefaultCloseOperation(
         JFrame.EXIT_ON_CLOSE );

      // create one applet instance
      PIC20_assign4 appletObject = new PIC20_assign4();
      appletObject.setWidth( width );
      appletObject.setHeight( height );

      // call applet's init and start methods
      appletObject.init();
      appletObject.start();

      // attach applet to center of window
      applicationWindow.getContentPane().add( appletObject );

      // set the window's size
      applicationWindow.setSize( width, height );

      // showing the window causes all GUI components
      // attached to the window to be painted
      applicationWindow.setVisible( true );
   }

}  // end class PIC20_assign4




// subclass of JPanel to allow drawing in a separate area

class DrawPanel extends JPanel {

   private Point2D.Double z1, z2, z3;

   private int currentChoice = -1;  // don't draw first time
   private int width = 100, height = 100;

   // initialize width and height of DrawPanel
   public DrawPanel( int newWidth, int newHeight )
   {
      width = ( newWidth >= 0 ? newWidth : width );
      height = ( newHeight >= 0 ? newHeight : height );
	System.out.println("(DrawPanel) Setting applet W/H to: "+
		width+ " ; " + height);

   }

   // draw line, rectangle or oval based on user's choice
   public void paintComponent( Graphics g )
   {
      super.paintComponent( g );

	// Here is where the actual drawing of
	// the three complex numbers must happen in
	// terms of the operation selected and 
	// g.drawLine() calls ( z3 =  z1 OPER z2 )

   }  // end method paintComponent

   public void setThreeComplexNumbers(Point2D.Double
	_z1, Point2D.Double _z2, Point2D.Double _z3)
   {
	z1 = _z1;
	z2 = _z2;
	z3 = _z3;
	repaint();
    }

}  // end class DrawPanel
