import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;

public class Lines2Dbis
 {
	public static void main(String[] args)
   {
      LinesFrame frame = new LinesFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
   }
}

/**
   A frame containing a panel for drawing polygonals
*/
class LinesFrame extends JFrame
{
   public LinesFrame()
   {
      setTitle("Drawing lines");
      setSize(WIDTH, HEIGHT);

      // add panel to frame

      LinesPanel panel = new LinesPanel();
      Container contentPane = getContentPane();
      contentPane.add(panel);
   }

   public static final int WIDTH  = 300;
   public static final int HEIGHT = 200;  
}

/**
   A panel with mouse operations for adding and removing squares.
*/
class LinesPanel extends JPanel
{
   public LinesPanel()
    {					
      starts= new ArrayList();	// starting points of lines
      ends= new ArrayList();      // ending points of lines
     
      Point2D.Double pivot;                          // start of current line
      Point2D.Double currentPoint;                   // current end of line
    
      addMouseListener(new MouseHandler());
      addMouseMotionListener(new MouseMotionHandler());
    }

    void addline(Point2D endpoint) 
    {
        starts.add(pivot);
        ends.add(endpoint);
        currentPoint = null;
        pivot = endpoint;
        repaint();
    }

    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;

        // Draw existing lines
        for (int i = 0; i < starts.size(); i++) 
		{
          g2.draw(new Line2D.Double((Point2D)starts.get(i),(Point2D)ends.get(i)));
        }

        // draw current line
        g2.setColor(Color.blue);
						 System.out.println("Eccoci qui");
        if (currentPoint != null)
            g2.draw(new Line2D.Double(pivot,currentPoint));
    					 System.out.println("Eccoci qua");
	}

    private class MouseHandler extends MouseAdapter
    {
	    public void mousePressed(MouseEvent evt) 
	    {
	        Point2D pivot = (Point2D)evt.getPoint();
	    }
	       
	
	    public void mouseReleased(MouseEvent evt) 
	    {
	        addline((Point2D)evt.getPoint());
								 System.out.println("Eccoci li");
	    }   
    }
    private class MouseMotionHandler implements MouseMotionListener
    {
	    public void mouseDragged(MouseEvent evt) 
	    {
	        Point2D currentPoint = (Point2D)evt.getPoint();
	        repaint();
								 System.out.println("Eccoci la'");
	    }
		public void mouseMoved(MouseEvent evt)
		{
		}
    }
	
	private ArrayList starts;
	private ArrayList ends;
	private Point2D pivot;                         // start of current line
	private Point2D currentPoint;                  // current end of line
}
