public class FernWindow extends JFrame
1:
2: import java.util.Scanner;
3: import java.awt.*;
4: import javax.swing.*;
5:
6: /**
7: * A collection of methods for drawing ferns.
8: *
9: * @author Mark Young
10: * @version 1.0
11: */
12: public class FernWindow extends JFrame {
13:
14: // ---------- Here is the program part -------------------------------- //
15:
16: /** default width for a fern window */
17: public static final int DEFAULT_WIDTH = 800;
18: /** default height for a fern window */
19: public static final int DEFAULT_HEIGHT = 600;
20:
21: // here is the program -- the class is below
22: public static void main(String[] args) {
23: Scanner kbd = new Scanner(System.in);
24:
25: // introduce yourself
26: System.out.println("\n\n"
27: + "Fern Drawing Program\n"
28: + "--------------------\n\n");
29:
30: // get window parameters (all windows the same size)
31: System.out.print("How big should the windows be (width height)? ");
32: int width = kbd.nextInt();
33: int height = kbd.nextInt();
34: kbd.nextLine();
35: if (width <= 0) width = DEFAULT_WIDTH;
36: if (height <= 0) height = DEFAULT_HEIGHT;
37:
38: // repeat
39: String resp;
40: do
41: {
42: // get root location
43: System.out.println();
44: System.out.print("Where should the root of the fern be? ");
45: int x = kbd.nextInt();
46: int y = kbd.nextInt();
47: kbd.nextLine();
48:
49: // get angle of growth
50: System.out.print("What angle should the fern grow at "
51: + "(180 = straight up)? ");
52: int angle = kbd.nextInt();
53: kbd.nextLine();
54:
55: // get fern size
56: System.out.print("And what height should the fern be? ");
57: int size = kbd.nextInt();
58: kbd.nextLine();
59:
60: // draw that fern in a new window
61: FernWindow win = new FernWindow(width, height, x, y, angle, size);
62: win.setVisible(true);
63:
64: // ask if there's more
65: System.out.print("another (y/n)> ");
66: resp = kbd.nextLine().toUpperCase();
67: } while (resp.startsWith("Y"));
68: System.exit(0);
69: }
70:
71: // ---------- below is the class -------------------------------------- //
72:
73: /** the part of the window we draw in */
74: private Graphics canvas;
75: /** the x-coordinate of the fern's root */
76: private double rootX;
77: /** the y-coordinate of the fern's root */
78: private double rootY;
79: /** the angle the fern grows away from its root (180 = straight up) */
80: private double rootAngle;
81: /** the root-to-tip length of the fern (it'll actually be a bit shorter) */
82: private double fullSize;
83:
84: /**
85: * Create a window for a fern to be drawn in.
86: *
87: * @param w the outer width of the window
88: * @param h the outer height of the window
89: * @param x the x-coordinate of the fern's root
90: * @param y the y-coordinate of the fern's root
91: * @param a the angle the fern grows from its root
92: * @param s the nominal size of the fern
93: */
94: public FernWindow(int w, int h, double x, double y, double a, double s) {
95: super("A Fern by MYoung");
96: setSize(w, h);
97: setBackground(Color.BLACK);
98: rootX = x;
99: rootY = y;
100: rootAngle = a;
101: fullSize = s;
102: }
103:
104: /**
105: * Respond to the computer's request to paint the window.
106: * This method called by the computer when the window gets shown.
107: *
108: * @param g the canvas to draw on -- provided by the computer.
109: */
110: public void paint(Graphics g) {
111: // use the canvas the computer provides
112: canvas = g;
113:
114: // choose a darkish green pen
115: canvas.setColor(new Color(0, 127, 0));
116:
117: // draw the fern
118: drawFern(rootX, rootY, rootAngle, fullSize);
119: }
120:
121: /**
122: * Draw a fern on my own canvas.
123: * This is a recursive method. You should understand how it works.
124: *
125: * @param x the x-coordinate of the fern's root
126: * @param y the y-coordinate of the fern's root
127: * @param angle the angle the fern grows from its root
128: * @param size the nominal size of the fern
129: */
130: public void drawFern(double x, double y, double angle, double size) {
131: // draw the stem and note where it ends
132: double length = size * 0.5;
133: double[] end = drawStem(x, y, angle, length);
134:
135: // if the fern is big...
136: if (size > 1.0) {
137: // ...draw smaller ferns for the fronds
138: double smaller = size * 0.4;
139: drawFern(end[0], end[1], angle+60, smaller);
140: drawFern(end[0], end[1], angle, smaller);
141: drawFern(end[0], end[1], angle-60, smaller);
142: }
143: }
144:
145: /**
146: * Draw the stem of a fern, returning the coordinates of its upper end.
147: * (You do not need to understand how this method works.)
148: *
149: * @param x the x-coordinate of the stem's root
150: * @param y the y-coordinate of the stem's root
151: * @param angle the angle the stem grows from its root
152: * @param length the length of the stem
153: * @return an array with the x and y coordinates of the stem's top
154: * in its two cells
155: */
156: public double[] drawStem(double x, double y, double angle, double length) {
157: // convert from degrees to radians for the sin & cos functions
158: double radians = Math.toRadians(angle);
159:
160: // figure out (and remember) where the line should end
161: double[] end = new double[2];
162: end[0] = x + Math.sin(radians) * length;
163: end[1] = y + Math.cos(radians) * length;
164:
165: // draw the line
166: drawLine(x, y, end[0], end[1]);
167:
168: // return the end-point
169: return end;
170: }
171:
172: /**
173: * Draw a line on my convas.
174: * (You do not need to understand how this method works.)
175: *
176: * @param x1 the x-coordinate of the start of the line
177: * @param y1 the y-coordinate of the start of the line
178: * @param x2 the x-coordinate of the end of the line
179: * @param y2 the y-coordinate of the end of the line
180: */
181: public void drawLine(double x1, double y1, double x2, double y2) {
182: // round off the start/end coordinates...
183: int startX = (int)Math.round(x1);
184: int startY = (int)Math.round(y1);
185: int endX = (int)Math.round(x2);
186: int endY = (int)Math.round(y2);
187:
188: // ... because the canvas expects to be given integer coordinates
189: canvas.drawLine(startX, startY, endX, endY);
190: }
191:
192: }
193: