/*
 * BarrierPolygon.java
 *
 * Created on January 24, 2007, 5:05 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package usda.weru.erosion.barriereditor;

import java.awt.Polygon;

/**
 *
 * @author wjr
 */
public final class FieldPolygon extends Polygon implements Cloneable {
    private static final long serialVersionUID = 1L;

    /** Creates a new instance of BarrierPolygon */
    public FieldPolygon() {
        super();
        addPoints();
    }
    private double x1 = 0;
    private double y1 = 0;
    private double x2 = 500;
    private double y2 = 300;

	/**
	 *
	 * @param widStr
	 */
	public void setWidStr(String widStr) {
        try {
            double wid = Double.parseDouble(widStr);
            y2 = y1 + wid;
            addPoints();
        } catch (Exception e) {
            return;
        }
    }

	/**
	 *
	 * @param lenStr
	 */
	public void setLenStr(String lenStr) {
        try {
            double len = Double.parseDouble(lenStr);
            x2 = x1 + len;
            addPoints();
        } catch (Exception e) {
            return;
        }
    }

	/**
	 *
	 * @return
	 */
	public double getX1() {
        return x1;
    }

	/**
	 *
	 * @return
	 */
	public double getY1() {
        return y1;
    }

	/**
	 *
	 * @return
	 */
	public double getX2() {
        return x2;
    }

	/**
	 *
	 * @return
	 */
	public double getY2() {
        return y2;
    }

	/**
	 *
	 * @return
	 */
	public double getLen() {
        return Math.abs(x1 - x2);
    }

	/**
	 *
	 * @return
	 */
	public double getWid() {
        return Math.abs(y1 - y2);
    }

	/**
	 *
	 * @return
	 */
	public String getLenStr() {
        return "" + Math.abs(x1 - x2);
    }

	/**
	 *
	 * @return
	 */
	public String getWidStr() {
        return "" + Math.abs(y1 - y2);
    }

    private void addPoints() {
        reset();
        addPoint((int) x1, (int) y1);
        addPoint((int) x1, (int) y2);
        addPoint((int) x2, (int) y2);
        addPoint((int) x2, (int) y1);
    }

	/**
	 *
	 * @param x1Str
	 * @param y1Str
	 * @param x2Str
	 * @param y2Str
	 */
	public FieldPolygon(String x1Str, String y1Str, String x2Str, String y2Str) {
        super();
        x1 = Double.parseDouble(x1Str);
        x2 = Double.parseDouble(x2Str);
        y1 = Double.parseDouble(y1Str);
        y2 = Double.parseDouble(y2Str);
        addPoints();
    }

	/**
	 *
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 */
	public FieldPolygon(double x1, double y1, double x2, double y2) {
        super();
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        addPoints();
    }

	/**
	 *
	 * @return
	 */
	@Override
    public String toString() {
        return "Field " + x1 + " " + y1 + " " + x2 + " " + y2;
    }

	/**
	 *
	 * @param obj
	 * @return
	 */
	@Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if ((obj instanceof FieldPolygon) == false) {
            return false;
        }
        FieldPolygon temp = (FieldPolygon) obj;
        if (getX1() != temp.getX1()) {
            return false;
        }
        if (getX2() != temp.getX2()) {
            return false;
        }
        if (getY1() != temp.getY1()) {
            return false;
        }
        if (getY2() != temp.getY2()) {
            return false;
        }
        if (getWid() != temp.getWid()) {
            return false;
        }
        if (getLen() != temp.getLen()) {
            return false;
        }
        return true;
    }

	/**
	 *
	 * @return
	 */
	@Override
    public Object clone() {
//        try {
//            FieldPolygon fp = (FieldPolygon) super.clone();
        FieldPolygon fp = new FieldPolygon();
        fp.x1 = x1;
        fp.y1 = y1;
        fp.x2 = x2;
        fp.y2 = y2;
        fp.addPoints();
        return fp;
//        } catch (CloneNotSupportedException ex) {
//            System.err.println("FieldPolygon not cloned");
//        }
//        return null;
    }

	/**
	 *
	 * @return
	 */
	@Override
    public int hashCode() {
/**
                 * Note:  Assertions are not enabled.  These will be useless items
                 * unless assertions are enabled.  Thus, they will be commented out unless
                 * the user wishes to enable specific assertions (feed the virtual machine 
                 * the -ea argument).
                 */
//        assert false : "hashCode not designed";
        return 42; // any arbitrary constant will do 
    }
}
