KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > KochSnowflakePlot


1 import java.applet.*;
2 import java.awt.*;
3 import JSci.maths.chaos.*;
4
5 /**
6 * Plot of the Koch snowflake.
7 * @author Mark Hale
8 * @version 1.0
9 */

10 public final class KochSnowflakePlot extends Applet {
11         private final int N=5;
12         private Image img;
13         private KochCurveGraphic curve;
14         private int width, height;
15         public void init() {
16                 width=getSize().width;
17                 height=getSize().height;
18                 img=createImage(width, height);
19                 curve=new KochCurveGraphic(img.getGraphics());
20                 final int len=width/2;
21                 final int h_2=(int)Math.round(len*Math.sqrt(3.0)/4.0);
22                 curve.draw((width-len)/2, height/2-h_2, width/2, height/2+h_2, N);
23                 curve.draw(width/2, height/2+h_2, (width+len)/2, height/2-h_2, N);
24                 curve.draw((width+len)/2, height/2-h_2, (width-len)/2, height/2-h_2, N);
25         }
26         public void paint(Graphics g) {
27                 g.drawImage(img, 0, 0, this);
28         }
29         class KochCurveGraphic extends KochCurve {
30                 private final Graphics g;
31                 public KochCurveGraphic(Graphics grafixs) {
32                         g=grafixs;
33                 }
34                 public void draw(int startX, int startY, int endX, int endY, int n) {
35                         g.setColor(Color.black);
36                         g.drawLine(startX, height-startY, endX, height-endY);
37                         recurse(startX, startY, endX, endY, n);
38                 }
39                 protected void drawLine(double startX, double startY, double endX, double endY) {
40                         g.drawLine((int)Math.round(startX), height-(int)Math.round(startY), (int)Math.round(endX), height-(int)Math.round(endY));
41                 }
42                 protected void eraseLine(double startX, double startY, double endX, double endY) {
43                         g.setColor(getBackground());
44                         drawLine(startX, startY, endX, endY);
45                         g.setColor(Color.black);
46                 }
47         }
48 }
49
50
Popular Tags