import java.awt.*; class Koch { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(800, 300); Graphics2D g = panel.getGraphics(); g.translate(50, panel.getHeight() - 50); String path = "F"; for (int i = 0; i < 5; i++) { path = path.replace("F", "F-F++F-F"); } drawPath(path, g, 3); } // Accepts a String representing a drawing path, and draws the path according // to the following interpretation of characters within the String: // F: draw a 40 pixel line to the right and move forward by 40 pixels // +: rotate clockwise by PI/3 radians (60 degrees) // -: rotate counterclockwise by PI/3 radians (60 degrees) public static void drawPath(String path, Graphics2D g, int size) { for (int i = 0; i < path.length(); i++) { char c = path.charAt(i); if (c == 'F') { g.drawLine(0, 0, size, 0); g.translate(size, 0); } else if (c == '+') { g.rotate(Math.PI/3.0); } else { g.rotate(-Math.PI/3.0); } } } }