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

public class lez20070410_2 extends JFrame
{
	JMenuItem stroke1, stroke2, stroke3;
	Pannello pannelloDisegno;
	
	public static void main (String[] args)
	{
		lez20070410_2 f = new lez20070410_2();
		f.setVisible(true);
	}
	
	public lez20070410_2()
	{
		setTitle("Prova");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400,400);
		pannelloDisegno = new Pannello();
		
		stroke1 = new JMenuItem("Stroke DIM 1");
		stroke1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
			pannelloDisegno.dim = 1; pannelloDisegno.repaint();}});
		stroke2 = new JMenuItem("Stroke DIM 3");
		stroke2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
			pannelloDisegno.dim = 3; pannelloDisegno.repaint();}});
		stroke3 = new JMenuItem("Stroke 1DIM 5");
		stroke3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
			pannelloDisegno.dim = 5; pannelloDisegno.repaint();}});
		
		JMenu strokeMenu = new JMenu("Tratto");
		strokeMenu.add(stroke1);
		strokeMenu.add(stroke2);
		strokeMenu.add(stroke3);
		
		JMenuBar menubar = new JMenuBar();
		menubar.add(strokeMenu);
		
		setJMenuBar(menubar);
		
		add(pannelloDisegno);
	}
}

class Pannello extends JPanel implements MouseMotionListener
{
	int dim = 1;
	ArrayList<Punto> coordinate;
	int x=0, y=0;
	Pannello() 
	{
		coordinate = new ArrayList<Punto>();
		addMouseMotionListener(this);
	}
	
	public void paint(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		for (int h = 1; h < coordinate.size(); h++)
		{
			Punto pp = coordinate.get(h-1);
			Punto p = coordinate.get(h);
			g2.setStroke(new BasicStroke(p.dim));
			g2.draw(new Line2D.Double(pp.x,pp.y,p.x,p.y));
		}
	}
	
	public void mouseMoved(MouseEvent e) {}
	public void mouseDragged(MouseEvent e) 
	{
		coordinate.add(new Punto((double)e.getX(), (double)e.getY(), dim));
		repaint();
	}
}

class Punto
{
	double x, y;
	int dim;
	
	Punto(double x, double y, int dim)
	{
		this.x = x;
		this.y = y;
		this.dim = dim;
	}
}
