import javax.swing.*;
import java.awt.*;
public class MovingLine extends JComponent  {
    private boolean isMajorDiagonal;

	public MovingLine(int x1, int y1, int x2, int y2)  {
	    super();
	    setEndPoints(x1,y1,x2,y2);
	    setBackground(Color.black);
	}

    public void setEndPoints(int x1, int y1, int x2, int y2) {
	    setBounds(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2)+1, Math.abs(y1-y2)+1);
	    isMajorDiagonal = (x1==Math.min(x1,x2) && y1==Math.min(y1,y2)) || (x2==Math.min(x1,x2) && y2==Math.min(y1,y2));         
    }

    /**   postconditon
            this method draws a line segment from point (getX(),getY()) to point 
            (x2, y2) in the background coordinate system and with  color from getBackground() */
    public void paint(Graphics g)  {
        g.setColor( getBackground() );
        if (isMajorDiagonal)
            g.drawLine(0, 0, getWidth()-1, getHeight()-1);
        else
            g.drawLine(0, getHeight()-1, getWidth()-1, 0);
   }

}
