-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
220 lines (164 loc) · 7.06 KB
/
Main.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
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;
public class Main {
private JPanel panel1;
private JLabel title;
private JList simsList;
private JList antsList;
private JButton addSim;
private JButton delSim;
private JButton addAnt;
private JButton delAnt;
private JButton toggleSim;
private JButton toggleFourmi;
private JButton simSettings;
private JButton antSettings;
private SimSettings simsSettingsMake = new SimSettings();
private SimSettings simsSettingsEdit = new SimSettings();
private AntSettings antSettingsMake;
private AntSettings antSettingsEdit;
public static void main(String[] args) {
JFrame frame = new JFrame("Main");
frame.setContentPane(new Main().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
Affichage.afficherSimulations();
Utils.pause(500, 0);
while (Simulation.simulations.size() == 0) { Utils.pause(0,0); }
while (Simulation.simulations.size() != 0) {
for (Simulation sim : Simulation.simulations)
if (sim.isActive())
for (int s = 0; s < sim.getPlateau().getMaxStepVelocity(); s++)
for (Fourmi f : sim.getPlateau().getFourmis())
if (f != null && f.isActive() && s < f.getStepVelocity())
sim.getPlateau().bougeFourmi(f);
Affichage.afficherSimulations();
Utils.pause(5, 1);
}
}
public Main() {
Timer updater = new Timer();
updater.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateLists();
}
}, 0, 100);
updater.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateButtons();
}
}, 0, 1000);
addSim.addActionListener(e -> simsSettingsMake.pop());
delSim.addActionListener(e -> {if (!simsList.isSelectionEmpty()) {
Simulation sim = Simulation.simulations.get(simsList.getSelectedIndex());
sim.delete();
simsList.clearSelection();
updateLists();
}});
addAnt.addActionListener(e -> {
if (!simsList.isSelectionEmpty() && simsList.getModel().getSize() < Simulation.simulations.get(simsList.getSelectedIndex()).getPlateau().getFourmis().length)
(antSettingsMake = new AntSettings(Simulation.simulations.get(simsList.getSelectedIndex()))).pop();
});
delAnt.addActionListener(e -> {
if (!simsList.isSelectionEmpty() && !antsList.isSelectionEmpty()) {
int simIndex = simsList.getSelectedIndex();
Simulation sim = Simulation.simulations.get(simIndex);
sim.getPlateau().getFourmis()[antsList.getSelectedIndex()] = null;
simsList.clearSelection();
updateLists();
simsList.setSelectedIndex(simIndex);
}
});
antSettings.addActionListener(e -> {
if (!simsList.isSelectionEmpty() && !antsList.isSelectionEmpty()) (antSettingsEdit = new AntSettings(Simulation.simulations.get(simsList.getSelectedIndex()).getPlateau().getFourmis()[antsList.getSelectedIndex()])).pop();
});
simSettings.addActionListener(e -> {if (!simsList.isSelectionEmpty()) (simsSettingsEdit = new SimSettings(Simulation.simulations.get(simsList.getSelectedIndex()))).pop();});
toggleSim.addActionListener(e -> {
if (simsList.isSelectionEmpty()) return;
Simulation sim = Simulation.simulations.get(simsList.getSelectedIndex());
if (sim.isActive()) {
toggleSim.setText(">");
sim.stop();
} else {
toggleSim.setText("| |");
sim.start();
}
});
toggleFourmi.addActionListener(e -> {
if (simsList.isSelectionEmpty() || antsList.isSelectionEmpty()) return;
Simulation sim = Simulation.simulations.get(simsList.getSelectedIndex());
Fourmi f = sim.getPlateau().getFourmis()[antsList.getSelectedIndex()];
if (f == null) return;
if (f.isActive()){
toggleFourmi.setText(">");
f.stop();
} else {
toggleFourmi.setText("| |");
f.start();
}
});
simsList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JList list = (JList)e.getSource();
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
Simulation sim = Simulation.simulations.get(index);
sim.getAffichage().setVisible(true);
}
updateButtons();
}
});
antsList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
updateButtons();
}
});
}
public void updateButtons() {
if (!simsList.isSelectionEmpty()) {
int currentIndex = simsList.getSelectedIndex();
Simulation currentSim = Simulation.simulations.get(currentIndex);
if (currentSim != null && currentSim.isActive())
toggleSim.setText("| |");
else
toggleSim.setText(">");
if (!antsList.isSelectionEmpty()) {
int currentAIndex = antsList.getSelectedIndex();
Fourmi currentAnt = currentSim.getPlateau().getFourmis()[currentAIndex];
if (currentAnt != null && currentAnt.isActive())
toggleFourmi.setText("| |");
else
toggleFourmi.setText(">");
}
}
}
public void updateLists() {
int selected = simsList.getSelectedIndex();
String[] sims = new String[Simulation.simulations.size()];
for (int i = 0; i < sims.length; ++i)
sims[i] = Simulation.simulations.get(i).toString();
simsList.setListData(sims);
simsList.setSelectedIndex(selected);
if (!simsList.isSelectionEmpty()) {
selected = antsList.getSelectedIndex();
Simulation current = Simulation.simulations.get(simsList.getSelectedIndex());
String[] fourms = new String[current.getPlateau().getFourmis().length];
for (int i = 0; i < fourms.length; ++i)
if (current.getPlateau().getFourmis()[i] != null)
fourms[i] = current.getPlateau().getFourmis()[i].toString();
antsList.setListData(fourms);
antsList.setSelectedIndex(selected);
} else {
antsList.setListData(new Object[] {});
}
}
}