-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBananaCreamPie.java
214 lines (172 loc) · 7.7 KB
/
BananaCreamPie.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
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
import java.awt.event.*; // access to WindowAdapter, WindowEvent
import javax.swing.*; // access to JFrame and Jcomponents
import javax.swing.event.*; // access to JSlider events
import javax.swing.Timer;
import java.util.*;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import java.awt.Image;
import java.awt.Rectangle;
/**
* Write a description of class BananaCreamPie here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class BananaCreamPie extends Food
{
JLabel pie; // instantiates the pie label
JLabel crust; // instantiates the crust label
JLabel filling; // instantiates the pie filling label
JLabel cream; // instantiates the cream label
JLabel banana; // instantiates the banana label
ArrayList<ImageIcon> pieimgs; // a list that holds the pie images
ArrayList<JLabel> ingredients; // a list that holds the JLabeles for the ingredients
Point previousPoint; // holds the point for the previous point
boolean collision;
boolean crustTouch; //Returns true or false if the crust is touching
boolean fillingTouch; //Returns true or false if the filling is touching
boolean bananaTouch; //Returns true or false if the banana is touching
boolean creamTouch;//Returns true or false if the cream is touching
JLabel focusLabel; //Label being dragged
JButton returnButton;
/**
* Constructor for objects of class BananaCreamPie
*/
public BananaCreamPie(KitchenPanel kit)
//Extends food and calls super(kit)
{
super(kit);
foodName = "assets/bananacreampie.png";
ImageIcon pieIMG = new ImageIcon(getClass().getResource("assets/pie1.png"));
ImageIcon creamIMG = new ImageIcon(getClass().getResource("assets/cream.png"));
ImageIcon crustIMG = new ImageIcon(getClass().getResource("assets/piecrust.png"));
ImageIcon bananaIMG = new ImageIcon(getClass().getResource("assets/bananaslices.png"));
ImageIcon fillingIMG = new ImageIcon(getClass().getResource("assets/bananafilling.png"));
ImageIcon returnIMG = new ImageIcon(getClass().getResource("assets/done.png"));
pie = new JLabel();
cream = new JLabel();
crust = new JLabel();
banana = new JLabel();
filling = new JLabel();
pie.setIcon(pieIMG);
pie.setBounds(0,0, 1000,750);
cream.setIcon(creamIMG);
cream.setBounds(100,25,200,169);
crust.setIcon(crustIMG);
crust.setBounds(150, 250, 305,229);
banana.setIcon(bananaIMG);
banana.setBounds(500,25,240,205);
filling.setIcon(fillingIMG);
filling.setBounds(500, 220, 244,124);
ingredients = new ArrayList<JLabel>();
ingredients.add(crust);
ingredients.add(filling);
ingredients.add(banana);
ingredients.add(cream);
pieimgs = new ArrayList<ImageIcon>();
pieimgs.add(new ImageIcon(getClass().getResource("assets/pie2.png")));
pieimgs.add(new ImageIcon(getClass().getResource("assets/pie3.png")));
pieimgs.add(new ImageIcon(getClass().getResource("assets/pie4.png")));
pieimgs.add(new ImageIcon(getClass().getResource("assets/pie5.png")));
ClickListener clickListener = new ClickListener();
DragListener dragListener = new DragListener();
foodKitchen.addMouseListener(clickListener);
foodKitchen.addMouseMotionListener(dragListener);
collision = false;
crustTouch = false;
fillingTouch = false;
creamTouch = false;
bananaTouch = false;
returnButton = new JButton();
returnButton.setFocusable(false);
returnButton.setBounds(600,450,350,350);
returnButton.addActionListener(foodKitchen);
returnButton.setIcon(returnIMG);
returnButton.setBackground(new Color(0xFFFFFF));
returnButton.setContentAreaFilled(false);
returnButton.setBorder(null);
previousPoint = new Point(0, 0);
}
public BananaCreamPie()
// Extends food and calls super()
{
super();
foodName = "assets/Banana Cream Pie Speech Bubble.png";
}
public class ClickListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
//Assigns focusLabel based on coordinates
{
previousPoint = e.getPoint(); // updating the previous point to new place that is clicked
for(JLabel img: ingredients){
boolean withinBounds = previousPoint.getX() > img.getX() && previousPoint.getX()<(img.getX()+img.getWidth()) && previousPoint.getY() > img.getY() && previousPoint.getY() <(img.getY()+img.getHeight());
if(withinBounds){
focusLabel = img;
}
}
}
}
public void addComponents(){
foodKitchen.add(filling);
foodKitchen.add(crust);
foodKitchen.add(banana);
foodKitchen.add(cream);
foodKitchen.add(pie);
}
public void removeComponents(){
foodKitchen.remove(pie);
}
public void intersects ()
//Returns whether objects have collided
{
boolean inBounds = focusLabel.getX() > 250 && focusLabel.getX()<770 && focusLabel.getY()>480 && focusLabel.getY()<610;
if(inBounds){
if(focusLabel == crust && crustTouch == false && fillingTouch == false && bananaTouch == false && creamTouch == false){
foodKitchen.remove(crust);
pie.setIcon(pieimgs.remove(0));
crustTouch = true;
}else if(focusLabel == filling && crustTouch == true && fillingTouch == false && bananaTouch == false && creamTouch == false ){
foodKitchen.remove(filling);
pie.setIcon(pieimgs.remove(0));
fillingTouch = true;
}else if (focusLabel == banana && crustTouch == true && fillingTouch == true && bananaTouch == false && creamTouch == false){
foodKitchen.remove(banana);
pie.setIcon(pieimgs.remove(0));
bananaTouch = true;
}else if (focusLabel == cream && crustTouch == true && fillingTouch == true && bananaTouch == true && creamTouch == false){
foodKitchen.remove(cream);
pie.setIcon(pieimgs.remove(0));
creamTouch = true;
foodKitchen.add(returnButton);
foodKitchen.moveToFront(returnButton);
foodKitchen.getFrame().repaint();
}else{
foodKitchen.updateScore(-1);
}
foodKitchen.getFrame().repaint();
}
}
public JButton getRestaurantButton(){
return returnButton;
}
public class DragListener extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
//Updates location of focusLabel based on mouse drag action
{
Point currentPoint = e.getPoint(); // gets the point where the mouse is currently
focusLabel.setLocation((int)(currentPoint.getX()-50),(int)(currentPoint.getY())-50);
previousPoint = currentPoint;
intersects();
foodKitchen.getFrame().repaint();
}
}
}