package DDrawing;


import java.awt.*;


/**
 * @author  mkron
 */
public class DLine extends DComponent{
  DPoint start;
  DPoint end;

  public DLine( double x1, double y1, double x2, double y2 ){
    this( new DPoint( x1, y1 ), new DPoint( x2, y2 ) );
  }

  public DLine( DPoint start, DPoint end ){
    this.start = start;
    this.end = end;
  }
  public DLine( double x1, double y1, double x2, double y2, Color color ){
    this( new DPoint( x1, y1 ), new DPoint( x2, y2 ), color );
  }

  public DLine( DPoint start, DPoint end, Color color ){
    this.start = start;
    this.end = end;
    this.color = color;
  }

  public DRectangle getRectangle(){
    double x = start.x, y = start.y, width = end.x - x, height = end.y - y;
    if( width < 0 ) { x += width; width *= -1; }
    if( height < 0 ) { y += height; height *= -1; }
    return new DRectangle( x, y, width, height );
  }

  public void paint( DMeasures m ){
    //System.out.println("DLine.paint(Measures): "+this);
    Graphics g = m.getGraphics();
    Color old_color = g.getColor();
    if( color != null ) g.setColor( color );
    else g.setColor( DEFAULT_COLOR );
    Point p1 = m.getPoint( start ),
          p2 = m.getPoint( end ) ;
    g.drawLine( p1.x, p1.y, p2.x, p2.y );
    g.setColor( old_color );
  }

  public String toString(){
    return "DLine[("+start.x+","+start.y+") --> ("+end.x+","+end.y+", color: "+color+"]";
  }
}
