-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemorySimulatorGUI.java
More file actions
419 lines (359 loc) · 14.9 KB
/
Copy pathMemorySimulatorGUI.java
File metadata and controls
419 lines (359 loc) · 14.9 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
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
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class MemorySimulatorGUI extends JFrame {
public JPanel leftPanel;
public JPanel centerPanel;
public JTextArea logArea;
public JScrollPane logScroll;
public JLabel timeLabel;
public JLabel runningLabel;
public JLabel waitingLabel;
public JLabel doneLabel;
public JLabel rejectedLabel;
public JLabel algorithmLabel;
public JButton startFirstFitButton;
public JButton startBestFitButton;
public JButton pauseButton;
public JButton stepButton;
public Timer timer;
public ArrayList<BlockPanel> blockPanels;
public MemorySimulator firstFitSim; // First-fit simulator
public BestFitMemorySimulator bestFitSim; // Best-fit simulator
public Object currentSim; // Reference to currently active simulator
public String currentAlgorithm; // Track which algorithm is running
public MemorySimulatorGUI() {
firstFitSim = new MemorySimulator();
bestFitSim = new BestFitMemorySimulator();
currentSim = null;
currentAlgorithm = "None";
blockPanels = new ArrayList<>();
// Window settings
setTitle("Memory Allocation Simulator - First-Fit vs Best-Fit");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create all parts
makeLeftPanel();
makeCenterPanel();
makeLogPanel();
// Add to window
add(leftPanel, BorderLayout.WEST);
add(centerPanel, BorderLayout.CENTER);
add(logScroll, BorderLayout.SOUTH);
// Make block visuals
makeBlockPanels();
// Make timer (2 seconds per tick)
timer = new Timer(2000, e -> {
step();
if (isCurrentSimDone()) {
timer.stop();
startFirstFitButton.setEnabled(true);
startBestFitButton.setEnabled(true);
pauseButton.setEnabled(false);
logArea.append("\n=== SIMULATION COMPLETE ===\n");
showStats();
}
});
// Show window
pack();
setLocationRelativeTo(null);
setVisible(true);
}
// Create left control panel
public void makeLeftPanel() {
leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setBorder(BorderFactory.createTitledBorder("Controls"));
leftPanel.setPreferredSize(new Dimension(220, 400));
// Create labels
algorithmLabel = new JLabel("Algorithm: None");
algorithmLabel.setFont(new Font("SansSerif", Font.BOLD, 12));
timeLabel = new JLabel("Time: 0");
runningLabel = new JLabel("Running: 0");
waitingLabel = new JLabel("Waiting: 0");
doneLabel = new JLabel("Done: 0");
rejectedLabel = new JLabel("Rejected: 0");
// Create buttons
startFirstFitButton = new JButton("Start First-Fit");
startBestFitButton = new JButton("Start Best-Fit");
pauseButton = new JButton("Pause");
stepButton = new JButton("Step");
pauseButton.setEnabled(false);
stepButton.setEnabled(false);
// Button actions
startFirstFitButton.addActionListener(e -> startFirstFit());
startBestFitButton.addActionListener(e -> startBestFit());
pauseButton.addActionListener(e -> pause());
stepButton.addActionListener(e -> step());
// Add everything
leftPanel.add(Box.createVerticalStrut(10));
leftPanel.add(algorithmLabel);
leftPanel.add(Box.createVerticalStrut(10));
leftPanel.add(timeLabel);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(runningLabel);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(waitingLabel);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(doneLabel);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(rejectedLabel);
leftPanel.add(Box.createVerticalStrut(20));
leftPanel.add(startFirstFitButton);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(startBestFitButton);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(pauseButton);
leftPanel.add(Box.createVerticalStrut(5));
leftPanel.add(stepButton);
leftPanel.add(Box.createVerticalGlue());
}
// Create center panel for memory blocks
public void makeCenterPanel() {
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(2, 5, 10, 10)); // 2 rows, 5 columns
centerPanel.setBorder(BorderFactory.createTitledBorder("Memory Blocks"));
centerPanel.setBackground(Color.LIGHT_GRAY);
}
// Create log area at bottom
public void makeLogPanel() {
logArea = new JTextArea(8, 50);
logArea.setEditable(false);
logArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
logArea.setLineWrap(true);
logArea.setWrapStyleWord(true);
logScroll = new JScrollPane(logArea);
logScroll.setBorder(BorderFactory.createTitledBorder("Log"));
logScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
// Create visual blocks
public void makeBlockPanels() {
int[][] blocks = {
{1, 9500}, {2, 7000}, {3, 4500}, {4, 8500}, {5, 3000},
{6, 9000}, {7, 1000}, {8, 5500}, {9, 1500}, {10, 500}
};
for (int i = 0; i < blocks.length; i++) {
BlockPanel bp = new BlockPanel(blocks[i][0], blocks[i][1]);
blockPanels.add(bp);
centerPanel.add(bp);
}
}
// Start First-Fit simulation
public void startFirstFit() {
// Reset simulation if needed
if (currentSim != null) {
resetSimulation();
}
currentSim = firstFitSim;
currentAlgorithm = "First-Fit";
algorithmLabel.setText("Algorithm: First-Fit");
if (!firstFitSim.isStarted()) {
firstFitSim.setup();
logArea.append("=== FIRST-FIT SIMULATION STARTED ===\n");
logArea.append("=====================================\n");
}
startSimulation();
}
// Start Best-Fit simulation
public void startBestFit() {
// Reset simulation if needed
if (currentSim != null) {
resetSimulation();
}
currentSim = bestFitSim;
currentAlgorithm = "Best-Fit";
algorithmLabel.setText("Algorithm: Best-Fit");
if (!bestFitSim.isStarted()) {
bestFitSim.setup();
logArea.append("=== BEST-FIT SIMULATION STARTED ===\n");
logArea.append("====================================\n");
}
startSimulation();
}
// Common start logic
private void startSimulation() {
timer.start();
startFirstFitButton.setEnabled(false);
startBestFitButton.setEnabled(false);
pauseButton.setEnabled(true);
stepButton.setEnabled(true);
}
// Reset simulation state
private void resetSimulation() {
timer.stop();
firstFitSim = new MemorySimulator();
bestFitSim = new BestFitMemorySimulator();
logArea.setText("");
// Clear visual blocks
for (BlockPanel panel : blockPanels) {
panel.clearJob();
}
updateDisplay();
}
// Pause button clicked
public void pause() {
timer.stop();
startFirstFitButton.setEnabled(true);
startBestFitButton.setEnabled(true);
pauseButton.setEnabled(false);
}
// Step button clicked (or timer tick)
public void step() {
if (currentSim == null) return;
String log = "";
if (currentSim instanceof MemorySimulator) {
log = ((MemorySimulator) currentSim).runOneTick();
} else if (currentSim instanceof BestFitMemorySimulator) {
log = ((BestFitMemorySimulator) currentSim).runOneTick();
}
logArea.append(log);
updateDisplay();
logArea.setCaretPosition(logArea.getDocument().getLength());
}
// Check if current simulation is done
private boolean isCurrentSimDone() {
if (currentSim == null) return true;
if (currentSim instanceof MemorySimulator) {
return ((MemorySimulator) currentSim).isDone();
} else if (currentSim instanceof BestFitMemorySimulator) {
return ((BestFitMemorySimulator) currentSim).isDone();
}
return true;
}
// Update all displays
public void updateDisplay() {
if (currentSim == null) {
timeLabel.setText("Time: 0");
runningLabel.setText("Running: 0");
waitingLabel.setText("Waiting: 0");
doneLabel.setText("Done: 0");
rejectedLabel.setText("Rejected: 0");
return;
}
// Update labels based on current simulator
if (currentSim instanceof MemorySimulator) {
MemorySimulator sim = (MemorySimulator) currentSim;
timeLabel.setText("Time: " + sim.getTime());
runningLabel.setText("Running: " + sim.getRunningCount());
waitingLabel.setText("Waiting: " + sim.getWaitingCount());
doneLabel.setText("Done: " + sim.getDoneCount());
rejectedLabel.setText("Rejected: " + sim.getRejectedCount());
// Update blocks
ArrayList<MemoryBlock> blocks = sim.getBlocks();
updateBlockPanels(blocks);
} else if (currentSim instanceof BestFitMemorySimulator) {
BestFitMemorySimulator sim = (BestFitMemorySimulator) currentSim;
timeLabel.setText("Time: " + sim.getTime());
runningLabel.setText("Running: " + sim.getRunningCount());
waitingLabel.setText("Waiting: " + sim.getWaitingCount());
doneLabel.setText("Done: " + sim.getDoneCount());
rejectedLabel.setText("Rejected: " + sim.getRejectedCount());
// Update blocks
ArrayList<MemoryBlock> blocks = sim.getBlocks();
updateBlockPanels(blocks);
}
repaint();
}
// Helper method to update block panels
private void updateBlockPanels(ArrayList<MemoryBlock> blocks) {
for (int i = 0; i < blocks.size() && i < blockPanels.size(); i++) {
MemoryBlock block = blocks.get(i);
BlockPanel panel = blockPanels.get(i);
if (!block.isEmpty) {
panel.setJob(block.currentJob);
} else {
panel.clearJob();
}
}
}
// Show final stats
public void showStats() {
String stats = "\n=== FINAL STATISTICS ===\n";
stats += "Algorithm Used: " + currentAlgorithm + "\n";
if (currentSim instanceof MemorySimulator) {
MemorySimulator sim = (MemorySimulator) currentSim;
stats += sim.getStats();
} else if (currentSim instanceof BestFitMemorySimulator) {
BestFitMemorySimulator sim = (BestFitMemorySimulator) currentSim;
stats += sim.getStats();
}
stats += "========================\n";
logArea.append(stats);
}
// Panel for one memory block
class BlockPanel extends JPanel {
public int blockNum;
public int blockSize;
public Job currentJob;
public boolean hasJob;
public BlockPanel(int num, int size) {
this.blockNum = num;
this.blockSize = size;
this.hasJob = false;
this.currentJob = null;
setPreferredSize(new Dimension(120, 80));
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
}
public void setJob(Job job) {
this.currentJob = job;
this.hasJob = true;
repaint();
}
public void clearJob() {
this.currentJob = null;
this.hasJob = false;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Choose color
Color bgColor;
if (hasJob && currentJob != null) {
// Red to green based on progress
float done = 1.0f - ((float)currentJob.timeLeft / currentJob.totalTime);
int red = (int)(255 * (1.0f - done));
int green = (int)(255 * done);
bgColor = new Color(red, green, 0);
} else {
bgColor = Color.LIGHT_GRAY;
}
// Fill background
g.setColor(bgColor);
g.fillRect(0, 0, getWidth(), getHeight());
// Draw text
g.setFont(new Font("SansSerif", Font.BOLD, 12));
FontMetrics fm = g.getFontMetrics();
// Block info
String line1 = "Block " + blockNum;
String line2 = "Size: " + blockSize;
g.setColor(Color.BLACK);
g.drawString(line1, (getWidth() - fm.stringWidth(line1)) / 2, 20);
g.drawString(line2, (getWidth() - fm.stringWidth(line2)) / 2, 35);
// Job info
if (hasJob && currentJob != null) {
// Pick text color based on background
float done = 1.0f - ((float)currentJob.timeLeft / currentJob.totalTime);
if (done > 0.5f) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
String line3 = "Job " + currentJob.jobNumber;
String line4 = currentJob.timeLeft + "/" + currentJob.totalTime;
g.drawString(line3, (getWidth() - fm.stringWidth(line3)) / 2, 50);
g.drawString(line4, (getWidth() - fm.stringWidth(line4)) / 2, 65);
} else {
g.setColor(Color.DARK_GRAY);
String line3 = "FREE";
g.drawString(line3, (getWidth() - fm.stringWidth(line3)) / 2, 55);
}
}
}
// Main method
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new MemorySimulatorGUI();
});
}
}