import java.awt.*; import java.util.*; public class Fern { public static final int LENGTH = 500; public static final int WIDTH = 500; public static final int RADIUS = 1; public static void main(String[] args) throws InterruptedException{ int x = 5; // painters canvas DrawingPanel p = new DrawingPanel(WIDTH, LENGTH); // paint brush Graphics g = p.getGraphics(); fernGame(g, 2000000); } public static void fernGame(Graphics g, int iter) { double x = 0; double y = 0; g.setColor(Color.GREEN); for (int i = 0; i < iter; i++) { double tmpx, tmpy; double r = Math.random(); if (r <= 0.01) { tmpx = 0; tmpy = 0.16 * y; } else if (r <= 0.08) { tmpx = 0.2 * x - 0.26 * y; tmpy = 0.23 * x + 0.22 * y + 1.6; } else if (r <= 0.15) { tmpx = -0.15 * x + 0.28 * y; tmpy = 0.26 * x + 0.24 * y + 0.44; } else { tmpx = 0.85 * x + 0.04 * y; tmpy = -0.04 * x + 0.85 * y + 1.6; } x = tmpx; y = tmpy; g.fillOval((int) Math.round(WIDTH / 2 + x * WIDTH / 11), (int) Math.round(LENGTH - y * LENGTH / 11), RADIUS, RADIUS); } } }