-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFrame.java
More file actions
280 lines (236 loc) · 8.4 KB
/
Copy pathMyFrame.java
File metadata and controls
280 lines (236 loc) · 8.4 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import java.awt.Color;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.*;
import javax.swing.*;
import java.lang.System;
import java.util.ArrayList;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import java.awt.Window;
public class MyFrame extends JFrame implements ActionListener{
private static JButton garbage;
private ImageIcon garbagepic;
private static JButton recycling;
private ImageIcon recyclingpic;
private static JButton compost;
private ImageIcon compostpic;
//MWMWMWMWMWMWMWMWMWMWMW
private JButton next;
private ImageIcon nextpic;
//MWMWMWMWMMWMWMWMWMWMW
private ImageIcon wastepic;
private static JLabel waste;
private static String[] wasteList;
private String[] wasteTypes;
private int index;
private String binType;
private static int score;
private static boolean playAgain;
public ArrayList<Integer> numbers;
// int i = -1;
public MyFrame(){
sortSmart();
}
public MyFrame(boolean first_time){
if (first_time){
instructions();
}
sortSmart();
}
private void sortSmart(){
score = 0;
wasteList = new String[]{"apple", "aluminum_foil", "banana", "can", "cardboard", "coffee_filter", "caprisun", "eggshells", "leaves", "milk_jug", "plastic_bottle", "newspaper", "napkins", "pizza_box", "styrofoam", "watermelon", "candy_wrapper", "broken_glass_bottle", "plastic_straws", "plastic_six_pack_rings", "broken_phone", "food_scraps", "broken_mug", "old_paint_cans", "plastic_cutlery", "takeout_container", "disposable_coffee_cup", "veggie_peels", "plastic_bag", "chocolate_milk"};
wasteTypes = new String[]{"c", "r", "c", "r", "r", "r", "g", "c", "c", "r", "r", "r", "c", "c", "g", "c", "g", "g", "g", "g", "g", "c", "g", "g", "g", "g", "g", "c", "g", "r"};
//no need for Waste class anymore
numbers = randomNumbers(wasteList.length);
garbagepic = new ImageIcon("garbage.png");
Image img = garbagepic.getImage();
Image newImg = img.getScaledInstance(100, 120, java.awt.Image.SCALE_SMOOTH);
garbagepic = new ImageIcon(newImg);
garbage = new JButton();
garbage.setIcon(garbagepic);
garbage.addActionListener(this);
garbage.setBounds(0,100,100,100);
recyclingpic = new ImageIcon("recycling.png");
img = recyclingpic.getImage();
newImg = img.getScaledInstance(90, 120, java.awt.Image.SCALE_SMOOTH);
recyclingpic = new ImageIcon(newImg);
recycling = new JButton();
recycling.setIcon(recyclingpic);
recycling.addActionListener(this);
recycling.setBounds(0,100,100,100);
compostpic = new ImageIcon("compost.png");
img = compostpic.getImage();
newImg = img.getScaledInstance(90, 120, java.awt.Image.SCALE_SMOOTH);
compostpic = new ImageIcon(newImg);
compost = new JButton();
compost.setIcon(compostpic);
compost.addActionListener(this);
compost.setBounds(0,100,100,100);
waste = new JLabel();
// numbers = randomNumbers();
index = changeWaste();
this.setSize(500,500);
this.add(garbage);
this.add(recycling);
this.add(compost);
this.add(waste);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(new Color(210, 237, 209));
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(20,10,10,10); //spacing
// c.gridwidth = 3; //number of columns
// c.gridheight = 4; //number of rows
c.gridx = 0; //col
c.gridy = 1; //row
c.ipady = 20; //vertical padding
panel.add(garbage,c);
c.gridx = 1;
panel.add(recycling,c);
c.gridx = 2;
panel.add(compost,c);
c.gridx = 1;
c.gridy = 0;
panel.add(waste,c);
//MWMWMWMWMWMWMWMWMWMWMWWMWMWMWMWMWMW
nextpic = new ImageIcon("next.png");
img = nextpic.getImage();
newImg = img.getScaledInstance(120, 60, java.awt.Image.SCALE_SMOOTH);
nextpic = new ImageIcon(newImg);
next = new JButton();
next.setIcon(nextpic);
next.addActionListener(this);
next.setBounds(0,100,90,30);
this.setSize(500,500);
this.add(next);
// this.setDefaultCloseOperation``(JFrame.EXIT_ON_CLOSE);
c.gridx = 1; //col
c.gridy = 1;
panel.add(next,c);
this.add(panel); //this is IMPORTANT
next.setVisible(false);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==garbage)
{
binType = "g";
System.out.print("Landfill");
}
else if(e.getSource()==recycling){
binType = "r";
System.out.print("Recycling");
}
else if(e.getSource()==compost){
binType = "c";
System.out.print("Compost");
}
else if(e.getSource()==next){
System.out.println("Next!");
}
if(binType.equals(wasteTypes[index])){
System.out.println(" -- Correct!");
score++;
}
else{
System.out.println(" -- Incorrect.");
}
System.out.println("Score: " + score);
index = changeWaste();
}
public ArrayList<Integer> randomNumbers(int length){
ArrayList<Integer> random = new ArrayList<>();
ArrayList<Integer> indices = new ArrayList<>();
for(int x = 0; x < length; x++){
random.add(x);
}
int rand;
for(int x = 0; x < length; x++) {
rand = (int)(Math.random()*length);
while(random.get(rand) == -1){
rand = (int)(Math.random()*length);
}
indices.add(random.get(rand));
random.set(rand, -1);
}
return indices;
}
// private static int aRandomNumber(int i){
// int[] numbers = randomNumbers();
// if(i < numbers.length()){
// return numbers[i];
// }
// return -1;
// }
public int changeWaste(){
int rand = numbers.get(0);
numbers.remove(0);
wastepic = new ImageIcon(wasteList[rand] + ".png");
Image img = wastepic.getImage();
Image newImg = img.getScaledInstance(100, 120, java.awt.Image.SCALE_SMOOTH);
wastepic = new ImageIcon(newImg);
waste.setIcon(wastepic);
return rand;
}
//MWMWMWMW
// public static void again(){
// int n = JOptionPane.showConfirmDialog(null,
// "Hi!! Welcome to Sort Smart!" + "\r\n" + "You will see pictures of different waste items. Click on the correct bin to sort the item. \nYou have 30seconds to sort as many as you can. Try to beat your score! \n Are you ready?\n click yes to begin!",
// "Sort Smart!",
// JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null);
// if (n == JOptionPane.YES_OPTION) {
// System.out.println("begin!");
// }
// else{
// System.out.println("goodbye!");
// System.exit(0);
// }
// }
//MWMW
public static void hideBins(){
garbage.setVisible(false);
recycling.setVisible(false);
compost.setVisible(false);
waste.setVisible(false);
int n = JOptionPane.showConfirmDialog(null,
"Your Score: " + score + "\r\n" + "Would you like to play again?",
"Time's Up!",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null);
if (n == JOptionPane.YES_OPTION) {
playAgain = true;
}
else{
closing();
System.exit(0);
}
//JOptionPane.showMessageDialog(null, "Your Score: " + score, "Time's Up!", JOptionPane.PLAIN_MESSAGE);
}
public static void instructions(){
Object[] options = {"BEGIN"};
// JOptionPane p = new JOptionPane(options[0], JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION,
// null,
// options,
// options[1]);
// p.setVisible(true);
// p.setSize(new Dimension(250,250));
JOptionPane j = new JOptionPane();
int q = j.showOptionDialog(null, "You will see pictures of different waste items. Click on the correct bin"+" \n to sort the item. Try to sort as many as you can into the correct waste bin"+" \n — landfill, recycling, or compost — within 30 seconds!"+" \n Are you ready?","Sort Smart", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
j.setSize(250, 250);
// System.out.println("instructions shown");
}
public static boolean getPlayAgain(){
return playAgain;
}
//mwmwmwmwm
public static void closing(){
int z = JOptionPane.showOptionDialog(null, "Goodbye. Thank you for playing Sort Smart!","Sort Smart", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
System.exit(0);
}
public void setNextVisible(){
next.setVisible(true);
}
}