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 javax.swing.JFrame;
import javax.swing.JPanel;

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

	public LineDraw_Quiz() {
		addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent event) {
				point.add(event.getPoint());
				repaint();
			}
		});

		addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseDragged(MouseEvent event) {
				point.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 = 1; i < point.size(); i++)
			g2.draw(new Line2D.Float(point.get(i-1), point.get(i)));
	}

	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.add(new LineDraw_Quiz());
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(800, 600);
		f.setVisible(true);
	}
}