-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIceCreamMaker.java
More file actions
131 lines (119 loc) · 3.53 KB
/
IceCreamMaker.java
File metadata and controls
131 lines (119 loc) · 3.53 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
123
124
125
126
127
128
129
130
131
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
/**
* This class creates an ice cream , holds each action for each flavor buttons
*
* @author Ching2 Huang
*
*/
public class IceCreamMaker extends JPanel implements ActionListener {
// the logic of the game
private IceCreamCone cone;
// the buttons to add or remove ice cream
private JButton vanilla, strawberry, greenTea, caramel, eat;
/**
* constructor add the game and button on the panel
*/
public IceCreamMaker() {
// use a BorderLayout
setLayout(new BorderLayout());
//create a cone
cone = new IceCreamCone();
// add the ice cream game to the center of the panel
add(cone, BorderLayout.CENTER);
// add the buttons on the top of the panel
add(buttonPanel(), BorderLayout.NORTH);
setFocusable(true);
}
/**
* create a panel to add all the buttons in it
*
* return panel that contains all the buttons
*/
private JPanel buttonPanel() {
// create the buttons for each flavor
vanilla = button("vanilla", Color.WHITE);
strawberry = button("strawberry", Color.PINK);
greenTea = button("green tea", Color.GREEN);
caramel = button("caramel", Color.ORANGE);
// create a panel to contain 4 flavor buttons
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout()); // use flowlayout
// add the buttons
panel.add(vanilla);
panel.add(strawberry);
panel.add(greenTea);
panel.add(caramel);
// create a button for removing
eat = new JButton("eat the scoop");
eat.addActionListener(this); //add action to the button
// create a bigger panel to contain all the buttons
JPanel bigPanel = new JPanel();
bigPanel.setLayout(new BorderLayout(0, 0)); // use borderLayout
bigPanel.add(panel, BorderLayout.NORTH); // add the flavor buttons to
// the north
bigPanel.add(eat, BorderLayout.CENTER); // add the remove button to the
// center
return bigPanel;
}
/**
* create a button and return a button
*
* @param flavor
* @param color
* @return button
*/
private JButton button(String flavor, Color color) {
// a button with a text on it
JButton theButton = new JButton(flavor);
theButton.setBackground(color); // the background color
theButton.setBorderPainted(false); // don't paint the border
theButton.setOpaque(true); // paint every pixel within it's bound
theButton.addActionListener(this); // tell the buttons what to do
return theButton;
}
/**
* tell each button what to do
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(vanilla)) {// if vanilla button is clicked
// add vanilla to the list
cone.add("vanilla");
} else if (e.getSource().equals(strawberry)) {// click strawberry button
// add strawberry to the list
cone.add("strawberry");
} else if (e.getSource().equals(greenTea)) {// click green tea button
//add green tea to the list
cone.add("greenTea");
} else if (e.getSource().equals(caramel)) { //click caramel button
//add caramel to the list
cone.add("caramel");
} else if (e.getSource() == eat) {//click eat button
//remove one scoop
cone.remove();
}
//repaint everything
cone.repaint();
cone.revalidate();
}
/**
* start a new cone
*/
public void nextCone(){
//remove all the scoops
cone.removeScoops();
}
/**
* get the ice cream from the maker/player
* @return the ice cream
*/
public IceCreamCone getIceCream(){
return cone;
}
}