flocon
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import javax.swing.*;
|
||||
|
||||
public class JFlocon extends JComponent {
|
||||
|
||||
private int ordre;
|
||||
private ArrayList<Point> points;
|
||||
private int[] coordX;
|
||||
private int[] coordY;
|
||||
|
||||
public JFlocon(int ordre) {
|
||||
this.ordre = ordre;
|
||||
this.points = new ArrayList<Point>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -14,10 +19,66 @@ public class JFlocon extends JComponent {
|
||||
Graphics secondPinceau = pinceau.create();
|
||||
|
||||
if (this.isOpaque()) {
|
||||
this.setColor(this.getBackgroundColor());
|
||||
this.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user