-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlantAlmanac.java
More file actions
189 lines (153 loc) · 4.38 KB
/
PlantAlmanac.java
File metadata and controls
189 lines (153 loc) · 4.38 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
// PlantAlmanac.java
// David Wang
// Displays all the plants in the plant almanac
// User can interact with the different seeds
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class PlantAlmanac extends JFrame implements ActionListener{
MainMenu menu;
AlmanacMenu almanac;
PlantAlmanacPanel plantpanel;
PvZ pvz;
String comingfrom;
javax.swing.Timer myTimer;
public PlantAlmanac(MainMenu menu,String comingfrom,PvZ pvz){
super();
this.menu = menu;
this.pvz = pvz;
this.comingfrom = comingfrom; // must know if coming from pause or main menu
setLayout(null); // to know what to set back to visible when almanac is closed
plantpanel = new PlantAlmanacPanel(this);
plantpanel.setSize(818,647);
plantpanel.setLocation(0,0);
add(plantpanel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(818,647);
myTimer = new javax.swing.Timer(100,this);
myTimer.start();
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
plantpanel.draw();
}
public void mainMenu(){
if(comingfrom.equals("menu")){
menu.stopAlmanacMusic();
menu.playMusic();
menu.setVisible(true);
setVisible(false);
}
if(comingfrom.equals("game")){
pvz.setVisible(true);
pvz.getPanel().setInAlmanac(false);
pvz.getPanel().setMusicLoop();
setVisible(false);
}
}
public void backToAlmanac(){
almanac = new AlmanacMenu(menu,comingfrom,pvz);
setVisible(false);
}
public static void main(String[]args){
PlantAlmanac menu = new PlantAlmanac(null,null,null);
}
}
class PlantAlmanacPanel extends JPanel implements MouseListener,MouseMotionListener{
PlantAlmanac menu;
Image PlantsAlmanacPic, AlmanacPic, QuitPic;
int[] pos;
boolean clicked;
ArrayList<Image> plants = new ArrayList<Image>();
Rectangle[] plantCards = new Rectangle[20];
boolean[] displayPlants = new boolean[20];
public void mouseClicked(MouseEvent evt){
clicked = true;
}
public void mouseExited(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){
clicked = true;
}
public void mousePressed(MouseEvent evt){
clicked = false;
}
public void mouseMoved(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public void mouseDragged(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public PlantAlmanacPanel(PlantAlmanac menu){
super();
setFocusable(true);
grabFocus();
addMouseListener(this);
addMouseMotionListener(this);
this.menu = menu;
pos = new int[]{0,0};
clicked = false;
PlantsAlmanacPic = new ImageIcon("Images/Menus/PlantsAlmanac.png").getImage();
AlmanacPic = new ImageIcon("Images/Menus/ReturnToAlmanac.png").getImage();
QuitPic = new ImageIcon("Images/Menus/ExitAlmanac.png").getImage();
for(int i = 1; i < 19; i ++){
plants.add(new ImageIcon(String.format("Images/Menus/Plants Almanac Entries/plant%s.png",i)).getImage());
}
for(int i = 0; i < 8; i ++){
plantCards[i] = new Rectangle(26+(i*52),92,50,70);
}
for(int i = 8; i < 16; i ++){
plantCards[i] = new Rectangle(26+((i%8)*52),170,50,70);
}
for(int i = 16; i < 18; i ++){
plantCards[i] = new Rectangle(26+((i%8)*52),248,50,70);
}
// set all plants display to false at first
for(int i = 0; i < displayPlants.length; i ++){
displayPlants[i] = false;
}
}
public void checkPlantRectCollide(){
for(int i = 0; i < plantCards.length; i ++){
if(plantCards[i] != null){
if(plantCards[i].contains(pos[0],pos[1])){
for(int j = 0; j < displayPlants.length; j ++){
displayPlants[j] = false;
}
displayPlants[i] = true;
}
}
}
}
public void draw(){
repaint();
}
public void paintComponent(Graphics g){
g.drawImage(PlantsAlmanacPic,0,0,this);
Rectangle exit = new Rectangle(35,570,157,20);
Rectangle menuexit = new Rectangle(678,570,85,22);
if(exit.contains(pos[0],pos[1])){ // go back to almanac menu
g.drawImage(AlmanacPic,0,0,this);
if(clicked){
menu.backToAlmanac();
}
}
if(menuexit.contains(pos[0],pos[1])){ // go back to main menu or pause screen
g.drawImage(QuitPic,0,0,this);
if(clicked){
menu.mainMenu();
}
}
if(clicked){
checkPlantRectCollide();
clicked = false;
}
for(int i=0; i < plants.size(); i++){
if(displayPlants[i]){
g.drawImage(plants.get(i),450,90,this);
}
}
}
}