-
Notifications
You must be signed in to change notification settings - Fork 25
/
GUI.java
237 lines (206 loc) · 8.28 KB
/
GUI.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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class GUI
{
private JFrame frame;
private JPanel mainPanel;
private CustomPanel chartPanel;
private JScrollPane tablePane;
private JScrollPane chartPane;
private JTable table;
private JButton addBtn;
private JButton removeBtn;
private JButton computeBtn;
private JLabel wtLabel;
private JLabel wtResultLabel;
private JLabel tatLabel;
private JLabel tatResultLabel;
private JComboBox option;
private DefaultTableModel model;
public GUI()
{
model = new DefaultTableModel(new String[]{"Process", "AT", "BT", "Priority", "WT", "TAT"}, 0);
table = new JTable(model);
table.setFillsViewportHeight(true);
tablePane = new JScrollPane(table);
tablePane.setBounds(25, 25, 450, 250);
addBtn = new JButton("Add");
addBtn.setBounds(300, 280, 85, 25);
addBtn.setFont(new Font("Segoe UI", Font.PLAIN, 11));
addBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
model.addRow(new String[]{"", "", "", "", "", ""});
}
});
removeBtn = new JButton("Remove");
removeBtn.setBounds(390, 280, 85, 25);
removeBtn.setFont(new Font("Segoe UI", Font.PLAIN, 11));
removeBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row > -1) {
model.removeRow(row);
}
}
});
chartPanel = new CustomPanel();
// chartPanel.setPreferredSize(new Dimension(700, 10));
chartPanel.setBackground(Color.WHITE);
chartPane = new JScrollPane(chartPanel);
chartPane.setBounds(25, 310, 450, 100);
wtLabel = new JLabel("Average Waiting Time:");
wtLabel.setBounds(25, 425, 180, 25);
tatLabel = new JLabel("Average Turn Around Time:");
tatLabel.setBounds(25, 450, 180, 25);
wtResultLabel = new JLabel();
wtResultLabel.setBounds(215, 425, 180, 25);
tatResultLabel = new JLabel();
tatResultLabel.setBounds(215, 450, 180, 25);
option = new JComboBox(new String[]{"FCFS", "SJF", "SRT", "PSN", "PSP", "RR"});
option.setBounds(390, 420, 85, 20);
computeBtn = new JButton("Compute");
computeBtn.setBounds(390, 450, 85, 25);
computeBtn.setFont(new Font("Segoe UI", Font.PLAIN, 11));
computeBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String selected = (String) option.getSelectedItem();
CPUScheduler scheduler;
switch (selected) {
case "FCFS":
scheduler = new FirstComeFirstServe();
break;
case "SJF":
scheduler = new ShortestJobFirst();
break;
case "SRT":
scheduler = new ShortestRemainingTime();
break;
case "PSN":
scheduler = new PriorityNonPreemptive();
break;
case "PSP":
scheduler = new PriorityPreemptive();
break;
case "RR":
String tq = JOptionPane.showInputDialog("Time Quantum");
if (tq == null) {
return;
}
scheduler = new RoundRobin();
scheduler.setTimeQuantum(Integer.parseInt(tq));
break;
default:
return;
}
for (int i = 0; i < model.getRowCount(); i++)
{
String process = (String) model.getValueAt(i, 0);
int at = Integer.parseInt((String) model.getValueAt(i, 1));
int bt = Integer.parseInt((String) model.getValueAt(i, 2));
int pl;
if (selected.equals("PSN") || selected.equals("PSP"))
{
if (!model.getValueAt(i, 3).equals(""))
{
pl = Integer.parseInt((String) model.getValueAt(i, 3));
}
else
{
pl = 1;
}
}
else
{
pl = 1;
}
scheduler.add(new Row(process, at, bt, pl));
}
scheduler.process();
for (int i = 0; i < model.getRowCount(); i++)
{
String process = (String) model.getValueAt(i, 0);
Row row = scheduler.getRow(process);
model.setValueAt(row.getWaitingTime(), i, 4);
model.setValueAt(row.getTurnaroundTime(), i, 5);
}
wtResultLabel.setText(Double.toString(scheduler.getAverageWaitingTime()));
tatResultLabel.setText(Double.toString(scheduler.getAverageTurnAroundTime()));
chartPanel.setTimeline(scheduler.getTimeline());
}
});
mainPanel = new JPanel(null);
mainPanel.setPreferredSize(new Dimension(500, 500));
mainPanel.add(tablePane);
mainPanel.add(addBtn);
mainPanel.add(removeBtn);
mainPanel.add(chartPane);
mainPanel.add(wtLabel);
mainPanel.add(tatLabel);
mainPanel.add(wtResultLabel);
mainPanel.add(tatResultLabel);
mainPanel.add(option);
mainPanel.add(computeBtn);
frame = new JFrame("CPU Scheduler Simulator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.add(mainPanel);
frame.pack();
}
public static void main(String[] args)
{
new GUI();
}
class CustomPanel extends JPanel
{
private List<Event> timeline;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (timeline != null)
{
// int width = 30;
for (int i = 0; i < timeline.size(); i++)
{
Event event = timeline.get(i);
int x = 30 * (i + 1);
int y = 20;
g.drawRect(x, y, 30, 30);
g.setFont(new Font("Segoe UI", Font.BOLD, 13));
g.drawString(event.getProcessName(), x + 10, y + 20);
g.setFont(new Font("Segoe UI", Font.PLAIN, 11));
g.drawString(Integer.toString(event.getStartTime()), x - 5, y + 45);
if (i == timeline.size() - 1)
{
g.drawString(Integer.toString(event.getFinishTime()), x + 27, y + 45);
}
// width += 30;
}
// this.setPreferredSize(new Dimension(width, 75));
}
}
public void setTimeline(List<Event> timeline)
{
this.timeline = timeline;
repaint();
}
}
}