2025-10-09 12:11:25 +02:00
|
|
|
import java.awt.*;
|
2025-10-09 16:14:30 +02:00
|
|
|
import java.util.ArrayList;
|
2025-10-09 12:11:25 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
|
|
|
|
public class JFlocon extends JComponent {
|
|
|
|
|
|
|
|
private int ordre;
|
2025-10-09 16:14:30 +02:00
|
|
|
private ArrayList<Point> points;
|
|
|
|
private int[] coordX;
|
|
|
|
private int[] coordY;
|
2025-10-09 12:11:25 +02:00
|
|
|
|
|
|
|
public JFlocon(int ordre) {
|
|
|
|
this.ordre = ordre;
|
2025-10-09 16:14:30 +02:00
|
|
|
this.points = new ArrayList<Point>();
|
2025-10-09 12:11:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void paintComponent(Graphics pinceau) {
|
|
|
|
Graphics secondPinceau = pinceau.create();
|
|
|
|
|
|
|
|
if (this.isOpaque()) {
|
2025-10-09 16:14:30 +02:00
|
|
|
secondPinceau.setColor(this.getBackground());
|
|
|
|
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
|
2025-10-09 12:11:25 +02:00
|
|
|
}
|
|
|
|
|
2025-10-09 16:14:30 +02:00
|
|
|
secondPinceau.setColor(Color.BLUE);
|
|
|
|
this.calculCoordonnees();
|
|
|
|
|
|
|
|
int[] coordX = new int[this.points.size()];
|
|
|
|
int[] coordY = new int[this.points.size()];
|
|
|
|
|
|
|
|
for (int i = 0; i != this.points.size(); i++) {
|
|
|
|
coordX[i] = points.get(i).x;
|
|
|
|
coordY[i] = points.get(i).y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Polygon p = new Polygon(coordX, coordY, this.points.size());
|
|
|
|
|
|
|
|
secondPinceau.drawPolygon(p);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void calculCoordonnees() {
|
|
|
|
Point a = new Point(this.getWidth()/4, this.getHeight()/4);
|
|
|
|
Point b = new Point(this.getWidth()/4*3, this.getHeight()/4);
|
|
|
|
Point c = new Point(
|
|
|
|
this.getWidth()/2,
|
|
|
|
this.getHeight()/4*3
|
|
|
|
);
|
|
|
|
segment(a, b, this.ordre);
|
|
|
|
segment(b, c, this.ordre);
|
|
|
|
segment(c, a, this.ordre);
|
2025-10-09 12:11:25 +02:00
|
|
|
|
|
|
|
}
|
2025-10-09 16:14:30 +02:00
|
|
|
|
|
|
|
public void segment(Point debut, Point fin, int ordre) {
|
|
|
|
if (ordre == 0) {
|
|
|
|
this.points.add(debut);
|
|
|
|
this.points.add(fin);
|
|
|
|
} else {
|
|
|
|
Point p2 = new Point(
|
|
|
|
(2 * debut.x + fin.x) / 3,
|
|
|
|
(2 * debut.y + fin.y) / 3
|
|
|
|
);
|
|
|
|
|
|
|
|
Point p3 = new Point(
|
|
|
|
(debut.x + fin.x) / 2 - (int) (Math.sqrt(3) * (debut.y - fin.y) / 6),
|
|
|
|
(debut.y + fin.y) / 2 - (int) (Math.sqrt(3) * (fin.x - debut.x) / 6)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Point p4 = new Point(
|
|
|
|
(debut.x + 2 * fin.x) / 3,
|
|
|
|
(debut.y + 2 * fin.y) / 3
|
|
|
|
);
|
|
|
|
|
|
|
|
segment(debut, p2, ordre-1);
|
|
|
|
segment(p2, p3, ordre-1);
|
|
|
|
segment(p3, p4, ordre-1);
|
|
|
|
segment(p4, fin, ordre-1);
|
|
|
|
}
|
|
|
|
}
|
2025-10-09 12:11:25 +02:00
|
|
|
}
|