-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHolidayCard.java
More file actions
123 lines (101 loc) · 3.49 KB
/
Copy pathHolidayCard.java
File metadata and controls
123 lines (101 loc) · 3.49 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.applet.AudioClip;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JApplet;
import org.teachingextensions.logo.Colors;
import org.teachingextensions.logo.Tortoise;
import org.teachingextensions.logo.TurtlePanel;
public class HolidayCard extends MouseAdapter {
double treeWidth = 15;
double scale = 1.1; // This is how much the width of the tree grows with each layer
/* 1. Paste your methods from the Christmas Tree Recipe here. */
void drawTreeBody() {
// 8. Change the color of the line the tortoise draws to forest green
Tortoise.setSpeed(10);
Tortoise.setPenColor(Colors.Greens.Chartreuse);
// 1. Make a variable for turnAmount and set it to 175
int turnAmount = 175;
// 2. Start the Tortoise facing to the right
Tortoise.turn(90);
// 5. Repeat steps 3 through 11, 11 times
for (int i=0; i<11; i++) {
// 3. Move the tortoise the width of the tree
Tortoise.move(treeWidth);
// 4. Turn the tortoise the current turnAmount to the right
Tortoise.turn(turnAmount);
// 6. Set the treeWidth to the current treeWidth times the scale
treeWidth=treeWidth*scale;
// 7. Move the tortoise the width of a tree again
Tortoise.move(treeWidth);
// 9. Turn the tortoise the current turn amount to the LEFT
Tortoise.turn(-turnAmount);
// 10. Set the treeWidth to the current treeWidth times the scale again
treeWidth=treeWidth*scale;
// 11. Decrease turnAmount by 1
turnAmount--;
}
}
void drawTreeTrunk() {
// 1. Move the tortoise half the width of the tree
Tortoise.move(treeWidth/2);
// 2. Change the tortoise so that it is pointing straight down
Tortoise.turn(90);
// 4. Set the pen width to the tree width divided by 10
Tortoise.setPenWidth(treeWidth/10);
// 5. Change the color of the line the tortoise draws to brown
Tortoise.setPenColor(Colors.Browns.Chocolate);
// 3. Move the tortoise a quarter the tree width
Tortoise.move(treeWidth/4);
}
void drawStar() {
// * Optional: Draw a red star on top of the tree. Hint: 144 degrees makes a star.
Tortoise.setPenColor(Colors.Reds.IndianRed);
for (int i=0; i<5; i++) {
Tortoise.move(15);
Tortoise.turn(144);
}
}
/* 2. When the mouse is clicked draw a tree at that position. */
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("mouse clicked!");
int mouseX = mouseEvent.getX();
int mouseY = mouseEvent.getY();
// Set the X position of the Tortoise to the X position of the mouse
Tortoise.setX(mouseX);
Tortoise.setY(mouseY);
drawTree();
}
/* 3. Personalize your card. */
void drawGreetingAndSing() {
// Download a Christmas sound (wav, midi or aiff) and use the playMusic() method to play it
playMusic("santa.wav");
// Use the writeGreeting() method to add a festive message
writeGreeting("Have a happy holiday break! (I think you have a break :)");
}
private void writeGreeting(String greeting) {
tortoiseWindow.getGraphics().drawString(greeting, 250, 50);
}
private void playMusic(String nameOfSoundFile) {
AudioClip sound = JApplet.newAudioClip(getClass().getResource(nameOfSoundFile));
sound.play();
}
void drawTree() {
drawStar();
drawTreeBody();
drawTreeTrunk();
treeWidth = 15;
Tortoise.setAngle(0);
Tortoise.setPenWidth(treeWidth/5);
}
public static void main(String[] args) {
new HolidayCard();
}
TurtlePanel tortoiseWindow;
HolidayCard() {
tortoiseWindow = Tortoise.getBackgroundWindow();
tortoiseWindow.addMouseListener(this);
Tortoise.show();
Tortoise.setSpeed(10);
drawGreetingAndSing();
}
}