Repository navigation
Expand file tree
/
Copy pathvonKoch.java
More file actions
76 lines (75 loc) · 2.41 KB
/
Copy pathvonKoch.java
File metadata and controls
76 lines (75 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.awt.*;
import java.awt.event.*;
public class vonKoch extends Frame implements ActionListener {
private TextField lvl, len;
vonKoch() {
super("von Koch snowflake");
Label lvlLbl = new Label("level");
lvl = new TextField("4",3);
Label lenLbl = new Label("side");
len = new TextField("200",3);
Button draw = new Button("draw");
lvl.addActionListener(this);
len.addActionListener(this);
draw.addActionListener(this);
setLayout(new FlowLayout());
add(lvlLbl);
add(lvl);
add(lenLbl);
add(len);
add(draw);
setSize(600,400);
setForeground(Color.white);
setBackground(Color.red);
show();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private double angle;
private Point currPt, pt = new Point();
private void right(double x) {
angle += x;
}
private void left (double x) {
angle -= x;
}
private void drawFourLines(double side, int level, Graphics g) {
if (level == 0) {
// arguments to sin() and cos() must be angles given in radians,
// thus, the angles given in degrees must be multiplied by PI/180;
pt.x = ((int)(Math.cos(angle*Math.PI/180)*side)) + currPt.x;
pt.y = ((int)(Math.sin(angle*Math.PI/180)*side)) + currPt.y;
g.drawLine(currPt.x, currPt.y, pt.x, pt.y);
currPt.x = pt.x;
currPt.y = pt.y;
}
else {
drawFourLines(side/3.0,level-1,g);
left (60);
drawFourLines(side/3.0,level-1,g);
right(120);
drawFourLines(side/3.0,level-1,g);
left (60);
drawFourLines(side/3.0,level-1,g);
}
}
public void actionPerformed(ActionEvent e) { // ActionListener
repaint();
}
public void paint(Graphics g) {
int level = Integer.parseInt(lvl.getText().trim());
double side = Double.parseDouble(len.getText().trim());
currPt = new Point(200,150);
angle = 0;
for (int i = 1; i <= 3; i++) {
drawFourLines(side,level,g);
right(120);
}
}
static public void main(String[] a) {
new vonKoch();
}
}