import java.awt.*; import java.util.ArrayList; import javax.swing.*; public class JFlocon extends JComponent { private int ordre; private ArrayList points; private int[] coordX; private int[] coordY; public JFlocon(int ordre) { this.ordre = ordre; this.points = new ArrayList(); } @Override public void paintComponent(Graphics pinceau) { Graphics secondPinceau = pinceau.create(); if (this.isOpaque()) { secondPinceau.setColor(this.getBackground()); secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight()); } 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); } 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); } } }