import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;

public class Snowflake extends JApplet implements ActionListener
{
        private final int maxdepth = 8;
        private final int mindepth = 1;
        private int currentdepth;

        private JPanel userPanel;
        private FlowLayout flow;

        private JButton plus;
        private JButton minus;

        private Point p1, p2, p3;

        private void draw( Point s, Point e, int depth )
        {
                DecimalFormat df = new DecimalFormat("0.00000");
                Graphics board = getContentPane().getGraphics();
                double x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6;

                x1 = s.getX();
                x5 = e.getX();
                x2 = (2 * x1 + x5) / 3;
                x4 = (x1 + 2 * x5) / 3;
                x6 = (x1 + x5) / 2;

                y1 = s.getY();
                y5 = e.getY();
                y2 = (2 * y1 + y5) / 3;
                y4 = (y1 + 2 * y5) / 3;
                y6 = (y1 + y5) / 2;

                x3 = (Math.sqrt(3) / 2) * (y2 - y1) + x6;
                y3 = (Math.sqrt(3) / 2) * (x1 - x2) + y6;

                board.setFont(new Font(board.getFont().getFamily(), Font.BOLD, 16));
                board.drawString("n = " + currentdepth, 20, 20);
                board.drawString("length = " + df.format(3 * Math.pow( (4. / 3), (currentdepth - 1))), 20, 40);

                if ( depth == 1 ) {
                        board.drawLine((int)x1, (int)y1, (int)x5, (int)y5);
                        return;
                }

                draw( new Point((int)x1, (int)y1), new Point((int)x2, (int)y2), depth - 1);
                draw( new Point((int)x2, (int)y2), new Point((int)x3, (int)y3), depth - 1);
                draw( new Point((int)x3, (int)y3), new Point((int)x4, (int)y4), depth - 1);
                draw( new Point((int)x4, (int)y4), new Point((int)x5, (int)y5), depth - 1);

        }

        public void actionPerformed ( ActionEvent ev )
        {
                if ( ev.getSource() == plus && currentdepth < maxdepth ) {
                        currentdepth++;
                } else if ( ev.getSource() == minus && currentdepth > mindepth) {
                        currentdepth--;
                }

                if( currentdepth == maxdepth ) {
                        plus.setEnabled( false );
                } else if ( currentdepth == mindepth ) {
                        minus.setEnabled( false );
                } else {
                        plus.setEnabled( true );
                        minus.setEnabled( true );
                }

                repaint();
        }

        public void paint( Graphics g )
        {
                super.paint( g );
                draw( p1, p2, currentdepth );
                draw( p2, p3, currentdepth );
                draw( p3, p1, currentdepth );
        }

        public void init()
        {
                int width = getWidth();
                int height = getHeight();

                Container c = getContentPane();

                userPanel = new JPanel();

                plus = new JButton("+");
                minus = new JButton("-");
                plus.addActionListener(this);
                minus.addActionListener(this);

                flow = new FlowLayout();
                flow.setAlignment(FlowLayout.CENTER);

                userPanel.setLayout( flow );
                userPanel.add(plus);
                userPanel.add(minus);
                c.add(userPanel, BorderLayout.SOUTH);

                currentdepth = 3;
                p1 = new Point( (int)(width * 0.5), (int)(height * 0.1) );
                p2 = new Point( (int)(width * 0.9), (int)(height * 0.1 + width * 0.4 * Math.sqrt(3)) );
                p3 = new Point( (int)(width * 0.1), (int)(height * 0.1 + width * 0.4 * Math.sqrt(3)) );
        }
}