package formacurvilineairregolarechiusa;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;

public class FormaCurvilineaIrregolareChiusa {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Forma Curvilinea Irregolare Chiusa");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new CustomPanel());
            frame.setSize(500, 500);
            frame.setVisible(true);
        });
    }

    static class CustomPanel extends JPanel {
        private boolean clicked = false;
        private Path2D path;

        CustomPanel() {
            path = createPath();
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (path.contains(e.getPoint())) {
                        clicked = !clicked;
                        repaint();
                    }
                }
            });
        }

        private Path2D createPath() {
            Path2D path = new Path2D.Double();
            path.moveTo(100, 200); // Punto di inizio
            path.curveTo(150, 100, 250, 100, 300, 200); // Prima curva di Bézier
            path.curveTo(350, 300, 250, 400, 200, 300); // Seconda curva di Bézier
            path.curveTo(150, 400, 100, 300, 100, 200); // Terza curva di Bézier che ritorna al punto di inizio
            path.closePath(); // Chiude la forma
            return path;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.BLACK);

            if (clicked) {
                g2d.setStroke(new BasicStroke(5)); // Aumenta lo spessore
            } else {
                g2d.setStroke(new BasicStroke(1)); // Spessore normale
            }

            g2d.draw(path);
        }
    }
}
