-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddAnimalDialog.java
More file actions
143 lines (129 loc) · 5.16 KB
/
AddAnimalDialog.java
File metadata and controls
143 lines (129 loc) · 5.16 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
package q3;
//ofri rom:208891804
//avigail shekasta:209104314
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// add animal dialog class this is small window that pop up and allows us to choose the animal that we want to add
public class AddAnimalDialog extends JDialog implements ActionListener {
// fields of this class
JSlider size;
JSlider Horizontally;
JSlider Vertically;
JSlider f;
JComboBox<String> cmb;
JButton fish;
JButton jellyfish;
JButton ok;
String[] names = {"Black", "Red", "Blue", "Green", "Cyan", "Orange", "Yellow", "Magenta", "Pink"};
Swimmable temp;
AnimalFactory obj;
Listener aqua_panel=AquaFrame.Panel;
// add animal dialog constructor init the component
public AddAnimalDialog() {
obj=new AnimalFactory();
JPanel panel1 = new JPanel();
cmb = new JComboBox<String>();
for (int i = 0; i < names.length; i++)
cmb.addItem(names[i]);
if(DuplicateAnima.flag==false){
size = new JSlider(20, 320);
Horizontally = new JSlider(1, 10);
Vertically = new JSlider(1, 10);
f = new JSlider(100, 1000);
fish = new JButton("דג");
fish.addActionListener(this);
panel1.add(fish, BorderLayout.SOUTH);
jellyfish = new JButton("מדוזה");
jellyfish.addActionListener(this);
panel1.add(jellyfish, BorderLayout.SOUTH);
}
else {
size = new JSlider(20, 320,DuplicateAnima.temp.getSize());
Horizontally = new JSlider(1, 10,DuplicateAnima.temp.getHorSpeed());
Vertically = new JSlider(1, 10,DuplicateAnima.temp.getVerSpeed());
f = new JSlider(100, 1000,DuplicateAnima.temp.getf());
cmb.setSelectedIndex(DuplicateAnima.temp.getColorInt());
ok = new JButton("אישור");
ok.addActionListener(this);
panel1.add(ok, BorderLayout.SOUTH);
}
size.setMinorTickSpacing(1);
size.setMajorTickSpacing(50);
size.setPaintTicks(true);
size.setPaintLabels(true);
size.setPaintTrack(true);
JLabel size1 = new JLabel("גודל:");
panel1.add(size);
panel1.add(size1);
pack();
Horizontally.setMinorTickSpacing(1);
Horizontally.setMajorTickSpacing(2);
Horizontally.setPaintTicks(true);
Horizontally.setPaintLabels(true);
Horizontally.setPaintTrack(true);
JLabel Horizontally1 = new JLabel("מהירות אופקית:");
panel1.add(Horizontally);
panel1.add(Horizontally1);
pack();
Vertically.setMinorTickSpacing(1);
Vertically.setMajorTickSpacing(2);
Vertically.setPaintTicks(true);
Vertically.setPaintLabels(true);
Vertically.setPaintTrack(true);
JLabel Vertically1 = new JLabel("מהירות אנכית:");
panel1.add(Vertically);
panel1.add(Vertically1);
pack();
f.setMinorTickSpacing(1);
f.setMajorTickSpacing(500);
f.setPaintTicks(true);
f.setPaintLabels(true);
f.setPaintTrack(true);
JLabel f1 = new JLabel("תדירות אכילה:");
panel1.add(f);
panel1.add(f1);
pack();
JLabel Color1 = new JLabel("צבע:");
panel1.add(cmb);
panel1.add(Color1);
panel1.setSize(100, 100);
add(panel1);
setSize(350, 300);
setTitle("הוספת חיה");
panel1.setLayout(new GridLayout(0, 2));
}
// the action listener of this class
@Override
public void actionPerformed(ActionEvent e) {
/**
* parameters event
* this methods perform the relevant task according to the source that received
* in general this method add animals to hashset in aqua panel
*/
// we can add animal just if we have less then 5 Swimable objects
if (AquaPanel.set.size() < 5) {
// add fish to the hash set
if (e.getSource().equals(fish))
temp=(Swimmable) obj.produceSeaCreature("Fish");
// add jelly fish to the hash set
else if (e.getSource().equals(jellyfish))
temp=(Swimmable) obj.produceSeaCreature("Jellyfish");
else if (e.getSource().equals(ok))
temp=DuplicateAnima.temp;
temp.set_obj(size.getValue(), 2, Horizontally.getValue(), Vertically.getValue(), cmb.getSelectedIndex(),f.getValue());
AquaPanel.set.add(temp);
// start the current object threads
temp.registerObserver(aqua_panel);
temp.start();
// set the window unvisible
this.dispose();
// if there are already five Swimable objects we pop up message
} else{
JOptionPane.showMessageDialog(this, "הגעת למכסה,\n קיימים במערכת 5 חיות");
this.dispose();
}
DuplicateAnima.flag=false;
}
}