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

public class lez20070626 extends JPanel
{
	ArrayList poligonali;
	ArrayList coordinate;
	int poligonaleCorrente = 0;
	int lastX= -1, lastY= -1;
	public lez20070626()
	{
		coordinate = new ArrayList();
		poligonali = new ArrayList();
		addMouseListener(new MouseAdapter(){public void mousePressed(MouseEvent e){
			poligonali.add(new Tratto());
			}});
		addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e){
			poligonaleCorrente++;
			lastX = -1;
			lastY = -1;
			}});
		addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent evento){
			Tratto appoggio = (Tratto)poligonali.get(poligonaleCorrente);
			if (lastX != -1 && lastY != -1)
				appoggio.punti.add(new Line2D.Double(lastX, lastY, evento.getX(), evento.getY()));
			lastX = evento.getX();
			lastY = evento.getY();
			repaint();
			}});
	}
	
	public void paint(Graphics __g)
	{
		super.paintComponent(__g);
		Graphics2D _g = (Graphics2D)__g;
		
		
		for (int h = 0; h < poligonali.size(); h++)
		{
			Tratto appoggio = (Tratto)poligonali.get(h);
			_g.setStroke(new BasicStroke(appoggio.spessore));
			_g.setColor(appoggio.colore);
			for (int g = 0; g < appoggio.punti.size(); g++)
				_g.draw((Line2D)appoggio.punti.get(g));
		}
	}
	public static void main(String[] a )
	{
		JFrame f = new JFrame();
		f.setSize(400,400);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().add(new lez20070626());
		f.setVisible(true);
	}
}

class Tratto
{
	ArrayList punti;
	int spessore = 1;
	Color colore;
	public Tratto()
	{
		spessore = (int)(Math.random()*10) + 1;
		colore = new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255));
		punti = new ArrayList();
	}
}


