/*----------------------------------------------------------------------+
|	Title:	LineElement.java					|
|		Java Class extends Element				|
|									|
|	Author:	David E. Joyce						|
|		Department of Mathematics and Computer Science   	|
|		Clark University					|
|		Worcester, MA 01610-1477				|
|		U.S.A.							|
|									|
|		http://aleph0.clarku.edu/~djoyce/home.html		|
|		djoyce@clarku.edu					|
|									|
|	Date:	February, 1996.  					|
+----------------------------------------------------------------------*/

import java.awt.*;

public class LineElement extends Element {
  PointElement A,B;

  LineElement () {dimension = 1;}

  LineElement (PointElement Aval, PointElement Bval) {
    dimension = 1;
    A = Aval; B = Bval;
  }

  double length() {return A.distance(B);}
  double length2() {return A.distance2(B);}

  public String toString() {
    return "[" + name + ": " + A + " " + B + "]";
  }

  protected boolean defined() {return A.defined() && B.defined();}

  protected void drawName (Graphics g) {
    if (nameColor!=null && name!=null && defined()) {
      g.setColor(nameColor);
      FontMetrics fm = g.getFontMetrics();
      int w = fm.stringWidth(name)+4;
      int h = fm.getHeight()+2;
      g.drawString(name, (int)((A.x+B.x)/2.0) - w/2,
			 (int)((A.y+B.y)/2.0) - h + fm.getAscent());
  } }

  protected void drawVertex (Graphics g) {
    if (vertexColor != null && defined()) {
      g.setColor(vertexColor);
      g.fillOval ((int)Math.round(A.x) - 2, (int)Math.round(A.y) - 2, 4, 4);
      g.fillOval ((int)Math.round(B.x) - 2, (int)Math.round(B.y) - 2, 4, 4);
  } }

  protected void drawEdge (Graphics g) {
    if (edgeColor!=null  && defined()) {
      g.setColor(edgeColor);
      g.drawLine ((int)Math.round(A.x), (int)Math.round(A.y),
	          (int)Math.round(B.x), (int)Math.round(B.y));
} } }


