1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package sample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class _koch_islnad extends Application { public static void main(String... args) { Application.launch(args); } private static final int VIEW_W = 400, VIEW_H = 300; private GraphicsContext gc; @Override public void start(Stage stage) throws Exception { BorderPane pane = new BorderPane(); stage.setScene(new Scene(pane)); Canvas view = new Canvas(VIEW_W, VIEW_H); gc = view.getGraphicsContext2D(); pane.setCenter(view); draw(); stage.show(); } private _turtle turtle; private void draw() { gc.setTransform(1, 0, 0, -1, 0, 0); gc.beginPath(); turtle = new _turtle(gc); turtle.position(80, -80); koch(240, 4); turtle.turn(-120); koch(240, 4); turtle.turn(-120); koch(240, 4); gc.fill(); } private void koch(double len, int n) { if (n == 0) { turtle.line(len); return; } koch(len / 3, n - 1); turtle.turn(60); koch(len / 3, n - 1); turtle.turn(-120); koch(len / 3, n - 1); turtle.turn(60); koch(len / 3, n - 1); } }