-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#1 - GUI 구현
- Loading branch information
Showing
1 changed file
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
|
||
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 JLabel rtLabel; | ||
private JLabel rtResultLabel; | ||
private JComboBox option; | ||
private DefaultTableModel model; | ||
|
||
public GUI() { | ||
model = new DefaultTableModel(new String[]{"Process", "AT", "BT", "Priority", "WT", "TAT", "RT"}, 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); | ||
rtLabel = new JLabel("Average Response Time:"); | ||
rtLabel.setBounds(25, 475, 180, 25); | ||
wtResultLabel = new JLabel(); | ||
wtResultLabel.setBounds(215, 425, 180, 25); | ||
tatResultLabel = new JLabel(); | ||
tatResultLabel.setBounds(215, 450, 180, 25); | ||
rtResultLabel = new JLabel(); | ||
rtResultLabel.setBounds(215, 475, 180, 25); | ||
|
||
option = new JComboBox(new String[]{"FCFS", "SJF", "HRN", "PSN", "PSP", "RR", "SRT"}); | ||
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(); | ||
CPUSchedulingAlgorithm scheduler; | ||
|
||
switch (selected) { | ||
case "FCFS": | ||
scheduler = new FCFS(); | ||
break; | ||
case "SJF": | ||
scheduler = new SJF(); | ||
break; | ||
case "HRN": | ||
scheduler = new HRN(); | ||
break; | ||
case "PSN": | ||
scheduler = new PriorityNonPreemptive(); | ||
break; | ||
case "PSP": | ||
scheduler = new PriorityPreemtive(); | ||
break; | ||
case "SRT": | ||
String tq = JOptionPane.showInputDialog("Time Quantum"); | ||
if (tq == null) { | ||
return; | ||
} | ||
scheduler = new SRT(); | ||
scheduler.setTimeQuantum(Integer.parseInt(tq)); | ||
break; | ||
case "RR": | ||
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.addRow(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); | ||
model.setValueAt(row.getResponseTime(), i, 6); | ||
} | ||
|
||
wtResultLabel.setText(Double.toString(scheduler.getAverageWaitingTime())); | ||
tatResultLabel.setText(Double.toString(scheduler.getAverageTurnAroundTime())); | ||
rtResultLabel.setText(Double.toString(scheduler.getAverageResponseTime())); | ||
|
||
chartPanel.setTimeline(scheduler.getTimeline()); | ||
} | ||
}); | ||
|
||
mainPanel = new JPanel(null); | ||
mainPanel.setPreferredSize(new Dimension(500, 525)); | ||
mainPanel.add(tablePane); | ||
mainPanel.add(addBtn); | ||
mainPanel.add(removeBtn); | ||
mainPanel.add(chartPane); | ||
mainPanel.add(wtLabel); | ||
mainPanel.add(tatLabel); | ||
mainPanel.add(rtLabel); | ||
mainPanel.add(wtResultLabel); | ||
mainPanel.add(tatResultLabel); | ||
mainPanel.add(rtResultLabel); | ||
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 x = 30; | ||
int y = 20; | ||
// int width = 30; | ||
for (int i = 0; i < timeline.size(); i++) { | ||
Event event = timeline.get(i); | ||
int width = event.getFinishTime() - event.getStartTime(); | ||
|
||
g.drawRect(x, y, width * 4, 30); | ||
g.setFont(new Font("Segoe UI", Font.BOLD, 13)); | ||
g.drawString(event.getProcessName(), x + width * 2 - 3, y + 20); | ||
g.setFont(new Font("Segoe UI", Font.PLAIN, 11)); | ||
g.drawString(Integer.toString(event.getStartTime()), x - 5, y + 45); | ||
|
||
x += width * 4; | ||
if (i == timeline.size() - 1) { | ||
g.drawString(Integer.toString(event.getFinishTime()), x, y + 45); | ||
} | ||
|
||
// width += 30; | ||
} | ||
|
||
// this.setPreferredSize(new Dimension(width, 75)); | ||
} | ||
} | ||
|
||
public void setTimeline(List<Event> timeline) { | ||
this.timeline = timeline; | ||
repaint(); | ||
} | ||
} | ||
} |