Source of SnowflakeWindow.java


  1: import java.awt.*;
  2: import javax.swing.*;

  4: /**
  5:  * Snowflake application
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  * @version 1.00 2009/7/23
  9:  */

 11: public class SnowflakeWindow extends JFrame {

 13:     /** The part of the window we draw in */
 14:     private Graphics canvas;
 15:     /** The size of the snowflake */
 16:     private int size;

 18:     /**
 19:      * Create a window for the snowflake to be shown in.
 20:      * Make the window large enuf to show the snowflake.
 21:      * The size of the snowflake is the end-to-end distance
 22:      * from the starting and ending points of the flake's longest edges.
 23:      *
 24:      * @param size  the size of the snowflake
 25:      */
 26:     public SnowflakeWindow(int size) {
 27:         super("A Snowflake by Koch & Young");
 28:         // make the window a bit larger than the flake will be
 29:         setSize(size+size/2, size+size/3);
 30:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 31:         setBackground(Color.BLACK);
 32:         this.size = size;
 33:     }

 35:     /**
 36:      * Initialize the window.  The computer calls this method.
 37:      * You don't need to understand it.
 38:      */
 39:     public void init() {
 40:         // use a black background
 41:         setBackground(Color.BLACK);
 42:     }

 44:     /**
 45:      * Respond to the computer's request to paint the window.
 46:      * This method called by the computer when the window gets shown.
 47:      *
 48:      * @param g     the canvas to draw on -- provided by the computer.
 49:      */
 50:     public void paint(Graphics g) {
 51:         // starting location 25% of way across, 40% of way down
 52:         int across = size / 4;
 53:         int down = 2 * size / 5;

 55:         // use the canvas the computer provides
 56:         canvas = g;

 58:         // choose a white pen
 59:         canvas.setColor(Color.BLUE);

 61:         // start the flake near the upper-left corner
 62:         drawFlake(across, down, size);
 63:     }

 65:     /**
 66:      * Draw a flake on my own canvas.
 67:      * Draw each of the flake's three (sic) edges.
 68:      *
 69:      * @param x     the x-coordinate of the starting point
 70:      * @param y     the y-coordinate of the starting point
 71:      * @param size  the size of the flake
 72:      */
 73:     public void drawFlake(double x, double y, double size) {
 74:         double[] end;
 75:         end = drawEdge(x, y, 90, size);
 76:         end = drawEdge(end[0], end[1], -30, size);
 77:         end = drawEdge(end[0], end[1], -150, size);
 78:     }

 80:     /**
 81:      * Draw an edge of the snowflake, returning the coordinates
 82:      * of its end.
 83:      * This is a recursive method. You should understand how it works.
 84:      *
 85:      * @param x         the x-coordinate where the edge starts
 86:      * @param y         the y-coordinate where the edge starts
 87:      * @param angle     the angle the edge goes from its starting point
 88:      * @param length    the length of the line
 89:      * @return  an array with the x and y coordinates of the line's end point
 90:      *          in its two cells
 91:      */
 92:     public double[] drawEdge(double x, double y, double angle, double length) {
 93:         if (length <= 1.0) {
 94:             return drawAngledLine(x, y, angle, length);
 95:         } else {
 96:             double[] end;
 97:             end = drawEdge(x, y, angle, length/3);
 98:             end = drawEdge(end[0], end[1], angle+60, length/3);
 99:             end = drawEdge(end[0], end[1], angle-60, length/3);
100:             return drawEdge(end[0], end[1], angle, length/3);
101:         }
102:     }

104:     /**
105:      * Draw a straight portion of the snowflake's edge.
106:      * (You do not need to understand how this method works.)
107:      *
108:      * @param x         the x-coordinate of the stem's root
109:      * @param y         the y-coordinate of the stem's root
110:      * @param angle     the angle the stem grows from its root
111:      * @param length    the length of the stem
112:      * @return  an array with the x and y coordinates of the stem's top
113:      *          in its two cells
114:      */
115:     public double[] drawAngledLine(double x, double y,
116:             double angle,
117:             double length) {
118:         // convert from degrees to radians for the sin and cos functions
119:         double radians = Math.toRadians(angle);

121:         // figure out (and remember) where the line should end
122:         double[] end = new double[2];
123:         end[0] = x + Math.sin(radians) * length;
124:         end[1] = y + Math.cos(radians) * length;

126:         // draw the line
127:         drawLine(x, y, end[0], end[1]);

129:         // return its end point
130:         return end;
131:     }

133:     /**
134:      * Draw a line on my convas.
135:      * (You do not need to understand how this method works.)
136:      *
137:      * @param x1    the x-coordinate of the start of the line
138:      * @param y1    the y-coordinate of the start of the line
139:      * @param x2    the x-coordinate of the end of the line
140:      * @param y2    the y-coordinate of the end of the line
141:      */
142:     public void drawLine(double x1, double y1, double x2, double y2) {
143:         int startX = (int)Math.round(x1);
144:         int startY = (int)Math.round(y1);
145:         int endX = (int)Math.round(x2);
146:         int endY = (int)Math.round(y2);
147:         canvas.drawLine(startX, startY,  endX, endY);
148:     }

150:     public static void main(String[] args) {
151:         SnowflakeWindow win = new SnowflakeWindow(600);
152:         win.setVisible(true);
153:     }

155: }