package edu.school.target;

import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import java.awt.Color;
import javax.swing.border.LineBorder;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * Урок 3.4.2.1. Компютърна  графика
 * Задача: Да се създаде приложение с ГПИ, което изчертава линиите 
 * от мишена от концентрични кръгове.
 * 
 * Версия 1.
 */
public class DrawingsTarget extends JFrame {


	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanel pnlCanvas;
	private JPanel pnlControl;
	private JButton btnDraw;
	private JLabel lblMargin;
	private JTextField txtMargin;
	private JLabel lblDistance;
	private JTextField txtDistance;
	private JLabel lblMinDiameter;
	private JTextField txtMinDiameter;
	private JButton btnReset;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DrawingsTarget frame = new DrawingsTarget();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DrawingsTarget() {
		setTitle("\u041C\u0438\u0448\u0435\u043D\u0430");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setForeground(new Color(51, 102, 102));
		contentPane.setBackground(new Color(255, 255, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[35%][65%,grow]", "[grow]"));

		pnlControl = new JPanel();
		pnlControl.setBackground(new Color(255, 255, 153));
		pnlControl.setBorder(new LineBorder(new Color(102, 0, 0)));
		contentPane.add(pnlControl, "cell 0 0,grow");
		pnlControl.setLayout(new MigLayout("", "[][grow]", "[][][grow][][bottom]"));

		lblMargin = new JLabel("\u041F\u043E\u043B\u0435:");
		lblMargin.setVerticalAlignment(SwingConstants.BOTTOM);
		lblMargin.setHorizontalAlignment(SwingConstants.CENTER);
		lblMargin.setForeground(new Color(51, 102, 102));
		pnlControl.add(lblMargin, "cell 0 0,alignx trailing,aligny top");
		lblMargin.setLabelFor(txtMargin);

		txtMargin = new JTextField();
		txtMargin.setForeground(new Color(51, 102, 102));
		pnlControl.add(txtMargin, "cell 1 0,growx,aligny top");
		txtMargin.setColumns(10);

		lblDistance = new JLabel("\u041E\u0442\u0441\u0442\u043E\u044F\u043D\u0438\u0435:");
		lblDistance.setForeground(new Color(51, 102, 102));
		pnlControl.add(lblDistance, "cell 0 1,alignx trailing");

		txtDistance = new JTextField();
		txtDistance.setForeground(new Color(51, 102, 102));
		pnlControl.add(txtDistance, "cell 1 1,growx");
		txtDistance.setColumns(10);

		lblMinDiameter = new JLabel("\u041C\u0438\u043D. \u0434\u0438\u0430\u043C\u0435\u0442\u044A\u0440:");
		lblMinDiameter.setForeground(new Color(51, 102, 102));
		pnlControl.add(lblMinDiameter, "cell 0 2,alignx trailing,aligny top");

		txtMinDiameter = new JTextField();
		txtMinDiameter.setForeground(new Color(51, 102, 102));
		pnlControl.add(txtMinDiameter, "cell 1 2,growx,aligny top");
		txtMinDiameter.setColumns(10);

		btnDraw = new JButton("\u0427\u0435\u0440\u0442\u0435\u0436");
		btnDraw.setForeground(new Color(0, 0, 0));
		btnDraw.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Извличане на графиката
				Graphics g = pnlCanvas.getGraphics();

				// Извличане на ширината на панела
				int w = pnlCanvas.getWidth();
				// Извличане на височината на панела
				int h = pnlCanvas.getHeight();

				Color clr1 = new Color(52, 103, 92);
				Color clr2 = new Color(208, 231, 226);

				int margin = Integer.parseInt(txtMargin.getText());
				int distance = Integer.parseInt(txtDistance.getText());
				int minDiameter = Integer.parseInt(txtMinDiameter.getText());

				int diameter = Math.min(w, h) - 2 * margin; // Текущ диаметър
				int x = margin;
				int y = margin;

				int n = 1; // номер на кръг
				while (diameter > minDiameter) {
					// Избор на цвят
					if (n % 2 == 0) {
						g.setColor(clr2);
					} else {
						g.setColor(clr1);
					}
					g.fillOval(x, y, diameter, diameter);
					x += distance;
					y += distance;
					diameter -= 2 * distance;
					n++;
				}

			}
		});
		pnlControl.add(btnDraw, "cell 0 3,growx,aligny bottom");

		btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pnlCanvas.repaint();
				
				txtDistance.setText("");
				txtMargin.setText("");
				txtMinDiameter.setText("");
			}
		});
		pnlControl.add(btnReset, "cell 1 3");

		JButton btnExit = new JButton("\u0418\u0437\u0445\u043E\u0434");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnExit.setForeground(new Color(102, 0, 0));
		pnlControl.add(btnExit, "cell 1 4,growx,aligny bottom");

		pnlCanvas = new JPanel();
		pnlCanvas.setForeground(new Color(102, 0, 0));
		pnlCanvas.setBackground(new Color(255, 255, 204));
		pnlCanvas.setBorder(new LineBorder(new Color(102, 0, 0)));
		contentPane.add(pnlCanvas, "cell 1 0,grow");
	}

}
