package kr.co.swh.lecture.opensource.drawing;


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.google.gson.Gson;

public class LineDrawSolution extends JPanel {
	private List<List<Point>> point = new ArrayList<List<Point>>();

	public LineDrawSolution() {
		
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent event) {
				List<Point> list = new ArrayList<Point>();
				point.add(list);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}
			
		});

		addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent event) {
				Point p = event.getPoint();
				Gson gg = new Gson();
				System.out.println(gg.toJson(point));
				point.get(point.size()-1).add(event.getPoint());
				repaint();
			}
			
		});
		
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(new Color(0, 0, 128));
		g2.setStroke(new BasicStroke(15f,
				BasicStroke.CAP_ROUND,
				BasicStroke.JOIN_ROUND));
		for (int i = 0; i < point.size(); i++) {
			List<Point> l = point.get(i);
			for (int j = 1; j < l.size(); j++) {
				g2.draw(new Line2D.Float(l.get(j-1), l.get(j)));
			}
		}
	}

	public static void main(String[] args) {
//		Thread.currentThread().getContextClassLoader().getResourceAsStream("db.pro")
		JFrame f = new JFrame();
		f.add(new LineDrawSolution());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(800, 600);
		f.setVisible(true);
	}
}