-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIceCreamCone.java
175 lines (158 loc) · 3.92 KB
/
IceCreamCone.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JComponent;
/**
* this class is the model and view of the ice cream game model: add and remove
* data view: paint the ice cream
*
* @author Ching2 Huang
*
*/
public class IceCreamCone extends JComponent {
// the list to store the value of ice creams
private StackLL<String> flavorList;
// The size of the cone
private static final int CONE_WIDTH = 20;
private static final int CONE_HEIGHT = CONE_WIDTH * 4;
// the size of the scoop
private static final int SCOOP_SIZE = 50;
// over lap distance
private static final int OVERLAP = 15;
/**
* constructor initializes the flavorList
*/
public IceCreamCone() {
// the list for flavors
flavorList = new StackLL<String>();
}
/**
* paint the cone and the ice cream
*/
public void paint(Graphics g) {
cone(g); // paint cone
paintIceCream(flavorList, g); // paint ice cream
}
/**
* add data to flavorList stack
*
* @param flavor
*/
public void add(String flavor) {
flavorList.push(flavor);
}
/**
* remove data from the flavorList stack
*/
public void remove() {
flavorList.pop();
}
/**
* paint the scoops of ice cream
*
* @param flavorList
*/
private void paintIceCream(StackLL<String> list, Graphics g) {
// temporary list to access the first added data in flavor list
StackLL<String> tmp = new StackLL<String>();
// while the list exist
while (!list.isEmpty()) {
// tmp stack add the last added ice cream in the flavor list
tmp.push(list.pop());
}
// to keep track the number of scoop and reset the data
int scoopNum = 1;
// while tmp list exists, paint the scoop
while (!tmp.isEmpty()) {
// check what flavor to paint then paint the scoop
// after painting, pop the data in the tmp list and push it back to
// list
// finally, update scoopNum
switch (tmp.peek()) {
case "vanilla":
addScoop(g, Color.WHITE, scoopNum);
list.push(tmp.pop());
scoopNum++;
break;
case "strawberry":
addScoop(g, Color.PINK, scoopNum);
list.push(tmp.pop());
scoopNum++;
break;
case "greenTea":
addScoop(g, Color.GREEN, scoopNum);
list.push(tmp.pop());
scoopNum++;
break;
case "caramel":
addScoop(g, Color.ORANGE, scoopNum);
list.push(tmp.pop());
scoopNum++;
break;
}
}
}
/**
* paint a cone for the ice cream
*
* @param g
*/
private void cone(Graphics g) {
// the color of the cone
Color coneColor = new Color(250, 180, 90);
g.setColor(coneColor);
g.fillPolygon(new int[] { this.getWidth() / 2,
this.getWidth() / 2 - CONE_WIDTH,
this.getWidth() / 2 + CONE_WIDTH },
new int[] { this.getHeight(), this.getHeight() - CONE_HEIGHT,
this.getHeight() - CONE_HEIGHT }, 3);
}
/**
* paint one fillOval for the scoop, pass in the color of the ice cream and
* the number of ice creams
*
* @param g
* @param color
* @param scoopNumber
*/
private void addScoop(Graphics g, Color color, int n) {
// xLoc is the x location of the scoop
// yLoc is the y location of the scoop. Increase by multiplying the
// number of scoops
int xLoc = this.getWidth() / 2 - SCOOP_SIZE / 2;
int yLoc = this.getHeight() - CONE_HEIGHT - (SCOOP_SIZE - OVERLAP) * n;
// be able to change color for each flavor
g.setColor(color);
// paint the scoop
g.fillOval(xLoc, yLoc, SCOOP_SIZE, SCOOP_SIZE);
}
/**
* add a random flavor scoop
*
* @param g
*/
private void addRandScoop(Graphics g) {
// generate random number to decide flavor
Random random = new Random();
int randomFlavor = random.nextInt(4); // 0-3
// check the number of randomFlavor
switch (randomFlavor) {
case 0:
// vanilla
addScoop(g, Color.WHITE, 1);
break;
case 1:
// strawberry
addScoop(g, Color.PINK, 1);
break;
case 2:
// greenTea
addScoop(g, Color.GREEN, 1);
break;
case 3:
// caramel
addScoop(g, Color.ORANGE, 1);
break;
}
}
}