
import wheels.users.*;
import java.awt.Color;

/**
 * Models a complex number, which includes both a real and an
 * imaginary part.  Can also draw complex number vectors on
 * a graphical coordinate plane using the number's currently set color.
 *
 * @author Zach Tomaszewski
 * @version Sep 28, 2007
 */
public class ComplexNumber {

  protected static wheels.users.CoordinateFrame display = new wheels.users.CoordinateFrame();

  protected double real;
  protected double imaginary;
  private Color color;


  /**
   * Constructs a new ComplexNumber with the given parts.
   */
  public ComplexNumber(double real, double imaginary) {
    this.real = real;
    this.imaginary = imaginary;
    color = Color.black;
  }

  /**
   * Sets this number's color.
   */
  public void setColor(Color newColor) {
    color = newColor;
  }

  /**
   * Returns this number's current color.
   */
  public Color getColor() {
    return color;
  }


  /**
   * Draws a line in this number's color from the origin to this
   * number's coordinates.
   */
  public void draw() {
    this.drawFrom(new ComplexNumber(0, 0));
  }

  /**
   * Draws a line from the given complex number to this complex number.
   * Uses this number's color.
   */
  public void drawFrom(ComplexNumber source) {
    Line vector = new Line((int) source.real, (int) source.imaginary,
                           (int) this.real, (int) this.imaginary);
    vector.setColor(this.getColor());
    display.repaint();
  }


  /**
   * Returns a string representation of this ComplexNumber
   * in the form "[real] + [imag]i".
   */
  public String toString() {
    return this.real + " + " + this.imaginary + "i";
  }

  /**
   * Displays a few sample complex numbers.
   */
  public static void main(String[] args) {
    ComplexNumber n1 = new ComplexNumber(30, 0);
    ComplexNumber n2 = new ComplexNumber(50, 50);
    n2.setColor(Color.red);
    ComplexNumber n3 = new ComplexNumber(10, -20);
    n3.setColor(Color.blue);
    ComplexNumber n4 = new ComplexNumber(-40, -60);
    n4.setColor(Color.green);

    n1.draw();
    n2.drawFrom(n1);
    n3.drawFrom(n1);
    n4.drawFrom(n2);

  }

}
