-
Notifications
You must be signed in to change notification settings - Fork 0
/
AquaPanel.java
520 lines (417 loc) · 13.5 KB
/
AquaPanel.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/**
* AquaPanel includes all the buttons at the bottom of the frame and also all the aquarium space
* where the swimmable swimm
* @author Adir Zoari 203002753
* @author Idan Levi 305562431
*
*/
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.*;
import java.util.concurrent.CyclicBarrier;
public class AquaPanel extends JPanel implements ActionListener,Observer{
private static final long serialVersionUID = 1L;
private int totalEatCounter=0;
private boolean flagTable=false,flagWorm=false;
protected Image back;
private JPanel buttonsPanel,tablePanel;
private JButton addAnimal,addPlanet,duplicateAnimal,decorator,sleep,wakeUp,reset,food,info,exit;
private JDialog dialogTable,changeColorDialog,colorChooser;
private HashSet<Swimmable> animalsSet=new HashSet<Swimmable>(); //hash set for swimmable
private HashSet<Immobile> planetsSet=new HashSet<Immobile>(); //hash set for immotible
private Iterator <Swimmable>itrAnimals;
private Iterator <Immobile>itrPlants;
private CyclicBarrier barrier;
private Singleton wormInstance=null; //instance of the worm
private CareTaker carTakerMemento;
private SwimmableReport swReport;
private PlanetReport plReport;
public AquaPanel(CareTaker carTakeMemento)
{
//setting panel
super();
setLayout(new BorderLayout());
setBackground(Color.white);
//general intialize
this.carTakerMemento=carTakeMemento;
//itrAnimals=animalsSet.iterator();
//set the buttons panel
setButtonPanel();
add(buttonsPanel,BorderLayout.SOUTH); // add buttonsPanel to the panel/aquaPanel
//create events
addAnimal.addActionListener(this);
addPlanet.addActionListener(this);
duplicateAnimal.addActionListener(this);
decorator.addActionListener(this);
sleep.addActionListener(this);
wakeUp.addActionListener(this);
reset.addActionListener(this);
food.addActionListener(this);
info.addActionListener(this);
exit.addActionListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(this.back != null) {
Graphics2D g1 = (Graphics2D) g;
g1.drawImage(this.back,0,0,getWidth(),getHeight(),this);
}
itrAnimals= animalsSet.iterator(); //intialzie iterator
itrPlants=planetsSet.iterator();
// pass with the iterator and draw the animals
while(itrAnimals.hasNext()){
itrAnimals.next().drawCreatrue(g);
}
while(itrPlants.hasNext()){
itrPlants.next().drawCreatrue(g);
}
//draw the worm
if(wormInstance!=null){ // check if user press on food button to create the worm
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.red);
g2.drawArc(getWidth()/2, getHeight()/2-5, 10, 10, 30, 210);
g2.drawArc(getWidth()/2, getHeight()/2+5, 10, 10, 180, 270);
g2.setStroke(new BasicStroke(1));
}
}
/**set buttonsPanel and create buttons **/
public void setButtonPanel()
{
//create buttonsPanel
buttonsPanel=new JPanel();
buttonsPanel.setLayout(new GridLayout(0,10,0,0)); // set gridLayout to the panel
//intialize buttons
addAnimal=new JButton("Add Animal");
addPlanet=new JButton("Add Planet");
duplicateAnimal=new JButton("Duplicate Animal");
decorator=new JButton("Decorator");
sleep=new JButton("Sleep");
wakeUp=new JButton("Wake up");
reset=new JButton("Reset");
food=new JButton("Food");
info=new JButton("Info");
exit=new JButton("Exit");
//prefferedSize
addAnimal.setPreferredSize(new Dimension(109,27));
addPlanet.setPreferredSize(new Dimension(109,27));
duplicateAnimal.setPreferredSize(new Dimension(109,27));
decorator.setPreferredSize(new Dimension(109,27));
sleep.setPreferredSize(new Dimension(109,27));
wakeUp.setPreferredSize(new Dimension(109,27));
reset.setPreferredSize(new Dimension(109,27));
food.setPreferredSize(new Dimension(109,27));
info.setPreferredSize(new Dimension(109,27));
exit.setPreferredSize(new Dimension(109,27));
// add buttons to the buttonsPanel
buttonsPanel.add(addAnimal);
buttonsPanel.add(addPlanet);
buttonsPanel.add(duplicateAnimal);
buttonsPanel.add(decorator);
buttonsPanel.add(sleep);
buttonsPanel.add(wakeUp);
buttonsPanel.add(reset);
buttonsPanel.add(food);
buttonsPanel.add(info);
buttonsPanel.add(exit);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==addAnimal)
{
try{
if(animalsSet.size()>=5)
throw new Exception("The Aquarium can contains only 5 animals!"); // the aquarium limits to only 5 swimmable
else{
AddAnimalDialog dialog=new AddAnimalDialog(this); // open to dialog to fill the details of the swimmable
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
else if(e.getSource()==addPlanet)
{
try{
if(planetsSet.size()>=5)
throw new Exception("The Aquarium can contains only 5 plantes!"); // the aquarium limits to only 5 swimmable
else{
AddPlanetDialog dialog=new AddPlanetDialog(this); // open to dialog to fill the details of the swimmable
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
else if(e.getSource()==duplicateAnimal)
{
try{
if(animalsSet.size()>=5)
throw new Exception("You can't duplicate animals, The Aquarium already contains 5 animals!"); // the aquarium limits to only 5 swimmable
else if(animalsSet.size()==0)
throw new Exception("no animals in aquarium to duplicate"); // the aquarium limits to only 5 swimmable
else{
AddDuplicateAnimal duplicateDialog=new AddDuplicateAnimal(this,animalsSet);
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
else if(e.getSource()==decorator)
{
try{
if(animalsSet.size()==0)
throw new Exception("no animals in aquarium to change their colors"); // the aquarium limits to only 5 swimmable
else{
changeColorDialog=new JDialog();
changeColorDialog.setSize(650,300);
changeColorDialog.setLayout(new BorderLayout());
changeColorDialog.setLocationRelativeTo(null);
changeColorDialog.setVisible(true);
changeColorDialog.setTitle("Decorator Design Pattern - Change Color");
JPanelDecorator cp=new JPanelDecorator(this,animalsSet,changeColorDialog);
changeColorDialog.add(cp);
}
}
catch(Exception e1){
JOptionPane.showMessageDialog(null,e1.getMessage());
}
}
else if(e.getSource()==sleep)
{
itrAnimals= animalsSet.iterator();
while(itrAnimals.hasNext()){
itrAnimals.next().setSuspend(); // suspend all the swimmable in the aquarium
}
}
else if(e.getSource()==wakeUp){
itrAnimals= animalsSet.iterator();
while(itrAnimals.hasNext()){
itrAnimals.next().setResume(); // cancel the suspend to all simmable
}
}
else if(e.getSource()==reset)
{
itrAnimals= animalsSet.iterator();
while(itrAnimals.hasNext())
{
Swimmable objSwim=itrAnimals.next();
//new Thread(objSwim).interrupt();
objSwim.resetSwimmable();
}
flagWorm=false;
animalsSet.clear();// remove all items from the hashset
carTakerMemento.getPlantesMementoList().clear();
carTakerMemento.getSwimmableMementoList().clear();
repaint(); // call to function PaintCompoment to repaint the panel
}
else if(e.getSource()==food)
{
setFlagFood();
}
else if(e.getSource()==info)
{
if(!flagTable){
flagTable=true;
fillTable();
}
else{
dialogTable.dispose();
flagTable=false;
}
}
else if(e.getSource()==exit)
{
System.exit(0);
}
}
public HashSet<Swimmable> getSwimmableSet(){return animalsSet;}
public HashSet<Immobile> getPlanetSet(){return planetsSet;}
public int checkSwimmableSwim()
{
int count=0;
itrAnimals=animalsSet.iterator();
while(itrAnimals.hasNext())
{
Swimmable objSwim = (Swimmable) itrAnimals.next();
if(objSwim.getState() instanceof Hungry){
//if(!(((Swimmable) itrAnimals.next()).getSuspend())){
count++;
}
}
return count;
}
public void barrierCall()
{
int countHungry = 0,countSwimmable=0;
itrAnimals=animalsSet.iterator();
while(itrAnimals.hasNext())
{
Swimmable objSwim = (Swimmable) itrAnimals.next();
if(!(objSwim.getSuspend())){
System.out.println(objSwim.getColor());
countSwimmable++;
if(objSwim.getState() instanceof Hungry){
countHungry++;
}
}
}
if(countHungry>0)
barrier=new CyclicBarrier(countHungry);
if(countSwimmable>0){
flagWorm=true; //delete the flag
wormInstance=Singleton.getInstance();
itrAnimals=animalsSet.iterator();
while (itrAnimals.hasNext()){
Swimmable objSwim = (Swimmable) itrAnimals.next();
if(!objSwim.getSuspend())
objSwim.setBarrier(barrier);
}
}
}
/*** add to Hashset function **/
/**add animal to Hashset
* @param newSwimm- object that inheriate from simmable**/
public void addAnimal(Swimmable newSwimm)
{
newSwimm.addObserver(this);
animalsSet.add(newSwimm); //add the new simmable object
repaint(); // call the paintCompoment function
new Thread(newSwimm).start(); // start the thread
}
/** add plant to Hashset **/
public void addPlanet(Immobile newSwimm)
{
planetsSet.add(newSwimm);
repaint();
}
/** create table and fill with details of the simmable **/
public void fillTable()
{
dialogTable=new JDialog(); // the table is kind of JDialog, it pop and dispose after click
tablePanel=new JPanel(); // create panel to the table
String[] types = {"Swimmable types","Planets types"};
JComboBox cmb_types=new JComboBox<String>(types);
JPanel comboPanel=new JPanel(new FlowLayout());
comboPanel.setSize(new Dimension(300,100));
JPanel reportsPanel=new JPanel();
swReport=new SwimmableReport(this,animalsSet,1);
plReport=new PlanetReport(this,planetsSet,1);
reportsPanel.add(swReport);
reportsPanel.add(plReport);
plReport.setVisible(false);
swReport.setVisible(true);
comboPanel.add(cmb_types);
tablePanel.add(comboPanel,BorderLayout.PAGE_START);
tablePanel.add(reportsPanel);
cmb_types.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e2) {
if(e2.getItem()=="Swimmable types")
{
swReport.setVisible(true);
plReport.setVisible(false);
}
if(e2.getItem()=="Planets types")
{
swReport.setVisible(false);
plReport.setVisible(true);
}
}
});
dialogTable.add(tablePanel); // add the panel to the JDialog dialogTable
dialogTable.setVisible(true);// show the table
dialogTable.setSize(650,300);// set size
/*
totalEatCounter=0;
itrAnimals= animalsSet.iterator();
tablePanel=new JPanel(); // create panel to the table
dialogTable=new JDialog(); // the table is kind of JDialog, it pop and dispose after click
String[] columns=new String[]{"Animal","Color","Size","Hor.Speed","Ver.speed","Eat counter"}; // columns
DefaultTableModel tableModel = new DefaultTableModel(columns,0);
JTable table=new JTable (tableModel);
table.setPreferredScrollableViewportSize(new Dimension(400,100));
while(itrAnimals.hasNext()){
Swimmable sw = (Swimmable) itrAnimals.next();
String[] swimm=new String[]{sw.getAnimalName(),sw.getColor(),String.valueOf(sw.getSize()),String.valueOf(sw.getHorSpeed()),String.valueOf(sw.getVerSpeed()),String.valueOf(sw.getEatCount())};
totalEatCounter+=sw.getEatCount(); //sum all eat count of the swimmable
tableModel.addRow(swimm);
}
if(animalsSet.size()>0) // display the eat counter
tableModel.addRow(new String[]{"table","","","","",String.valueOf(totalEatCounter)});
JScrollPane pane=new JScrollPane(table);
tablePanel.add(pane); // add the table to the panel
dialogTable.add(tablePanel); // add the panel to the JDialog dialogTable
dialogTable.setVisible(true);// show the table
dialogTable.setSize(420,200);// set size
*/
}
/** get object who ate the worm and increament in 1 his counter
* @param swimm- object of swimmable
*/
public synchronized void eatFood(Swimmable swimm)
{
swimm.eatInc();//increament the eat counter of the swimm object
}
/**set the flag worm to false after the worm ate by swimmable**/
public void setFlagWorm(){
flagWorm=false;
Singleton.set();
}
public void setFlagFood()
{
if (Singleton.get()==null){
if(wormInstance==null){
try{
barrierCall();
}
catch(IllegalArgumentException err){
System.out.println("add");
flagWorm=false;
repaint();
return;
}
}
}
}
/***---------- Singelton design Pattern ----------- ***/
/**set worm instance to null **/
public void setWormInstance()
{
Singleton.set();
wormInstance=null;
}
/**get the worm Instance **/
public Singleton getWormInstance(){return wormInstance;}
public boolean getFlagWorm(){return flagWorm;}
@Override
public void update(java.util.Observable arg0, Object arg1) {
// TODO Auto-generated method stub
//setFlagFood();
JOptionPane.showMessageDialog(null,arg1 +" wants to eat! BloBlo ","Notify Food",JOptionPane.PLAIN_MESSAGE);
}
/*
public MarineAnimal getObjectById(int idObject,String typeAnimal)
{
itrAnimals= animalsSet.iterator();
while(itrAnimals.hasNext()){
Swimmable sw=itrAnimals.next();
if(sw.getID()==idObject){
if(typeAnimal=="Fish")
return (Fish)sw;
if(typeAnimal=="Jelly Fish")
return (Jellyfish)sw;
}
}
return null;
}
*/
}