
import javax.swing.JOptionPane;

/**
 * Queries the user to enter a number of complex numbers then
 * draws them.  The first number is connected to the origin;
 * each subsequent number is connected to the previous one, in
 * alternating colors.
 * 
 * @author Zach Tomaszewski
 * @version Sep 27, 2007
 */
public class Plotter {

  public static void main(String[] args) {
    
    JOptionPane.showMessageDialog(null, 
        "Please enter a series of complex numbers, using this format:" +
        "\n\n  real, imaginary \n\n" +
        "For example, to plot the complex number \"10.3 - 3i\", you would enter:" +
        "\n\n  10.3, 3 \n\n" +
        "Enter nothing or click Cancel to stop entering numbers.", 
        "Instructions", JOptionPane.INFORMATION_MESSAGE);
    
    ComplexNumber lastNumber = null;
    int color = 0;
    while (true) {  //keep entering numbers until explicitly break
      try {
        
        String entered = JOptionPane.showInputDialog("Enter a complex number: ");
        
        if (entered == null || entered.equals("")) {
          //didn't enter anything at all, so done entering numbers
          break;
        }
        
        String realPart = entered.substring(0, entered.indexOf(','));
        String imagPart = entered.substring(entered.indexOf(',') + 1);
        ComplexNumber cn = new ComplexNumber(Double.parseDouble(realPart),
                                             Double.parseDouble(imagPart));
        
        //testing toString
        System.out.println("Drawing: " + cn);
        
        //setting color and drawing
        if (lastNumber == null) {
          //test starting from origin with default color
          cn.draw();
        }else {
          //alternate colors
          color = (color + 1) % 4;
          switch (color) {
            case 0: cn.setColor(java.awt.Color.red); break;
            case 1: cn.setColor(java.awt.Color.blue); break;
            case 2: cn.setColor(java.awt.Color.green); break;
            case 3: cn.setColor(java.awt.Color.yellow); break;            
          }          
          cn.drawFrom(lastNumber);  
        }
        lastNumber = cn;
             
      }catch (StringIndexOutOfBoundsException sioobe) {
        JOptionPane.showMessageDialog(null, 
            "You did not include a comma.\n" + 
            "Please use the format: real, imaginary", 
            "Format Error", JOptionPane.ERROR_MESSAGE);
      }catch (NumberFormatException nfe) {
        JOptionPane.showMessageDialog(null, 
            "Could not recognize your numbers.\n" + 
            "Please give two numbers, separated by a comma.", 
            "Format Error", JOptionPane.ERROR_MESSAGE);
      }
    }
  }
  
}//end Plotter
