package math.bezier;

import math.utils.Tuple2d;

import javax.swing.*;
import java.awt.*;

public class Bezier26 extends JFrame {
    private static final long serialVersionUID = 431281611636634328L;
    public static final int fw = 1000;
    public static final int fh = 1000;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Bezier26(new BernsteinPanel26()));
    }

    public Bezier26(JComponent comp) {
        setLayout(null);
        setPreferredSize(new Dimension(fw, fh));
        setBounds((Toolkit.getDefaultToolkit().getScreenSize().width / 2)
                        - (fw / 2),
                (Toolkit.getDefaultToolkit().getScreenSize().height / 2)
                        - (fh / 2), fw, fh);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Ramka Graficzna");
        //	setResizable(false);
        JPanel panel = (JPanel) comp;
        panel.setLocation((fw - panel.getWidth()) / 2,
                (fh - panel.getHeight()) / 2);
        add(panel);
        setVisible(true);
    }
}

class BernsteinPanel26 extends JPanel {
    private static final long serialVersionUID = -5183600697149041028L;
    private static final int w = 800;
    private static final int h = 800;

    public BernsteinPanel26() {
        setPreferredSize(new Dimension(w, h));
        setBackground(Color.WHITE);
        setSize(w, h);
        setLayout(null);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.BLACK);
        g2.drawRect(0, 0, w - 1, h - 1);
        Tuple2d[] points = {new Tuple2d(60, 60), new Tuple2d(120, 150),
                new Tuple2d(180, 90), new Tuple2d(70, 160),
                new Tuple2d(220, 20), new Tuple2d(135, 135),
                new Tuple2d(20, 100), new Tuple2d(320, 180)};
        Tuple2d[][] arr = BezierUtil.divideCasteljau(points);
        Tuple2d[] points1 = arr[0];
        Tuple2d[] points2 = arr[1];
        //	g2.setColor(Color.BLUE);
        //BezierUtil.drawNBezier(g2, points, true);
        g2.setColor(Color.GREEN);
        BezierUtil.drawNBezier(g2, points1, true);
        g2.setColor(Color.RED);
        BezierUtil.drawNBezier(g2, points2, true);
    }
}