From 83a589d8b7d51ca810e15f7c35a04df170c07dd6 Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Sun, 11 Jun 2023 07:01:57 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#1=20GUI=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Java Swing을 이용한 GUI 구현 --- src/GUI.java | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 src/GUI.java diff --git a/src/GUI.java b/src/GUI.java new file mode 100644 index 0000000..525c6f7 --- /dev/null +++ b/src/GUI.java @@ -0,0 +1,255 @@ + +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 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 timeline) + { + this.timeline = timeline; + repaint(); + } + } +} From 6ff7aa37e85912d77aae1c2f67a3be25a96cf53e Mon Sep 17 00:00:00 2001 From: Ogu1208 Date: Sun, 11 Jun 2023 17:24:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[fix]=20:=20=EA=B0=84=ED=8A=B8=20=EC=B0=A8?= =?UTF-8?q?=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 간트 차트가 각 프로세스의 burstTime에 맞게 with가 설정되도록 함 --- src/GUI.java | 66 ++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/src/GUI.java b/src/GUI.java index 525c6f7..4d9a3c6 100644 --- a/src/GUI.java +++ b/src/GUI.java @@ -16,8 +16,7 @@ import javax.swing.JTable; import javax.swing.table.DefaultTableModel; -public class GUI -{ +public class GUI { private JFrame frame; private JPanel mainPanel; private CustomPanel chartPanel; @@ -36,8 +35,7 @@ public class GUI private JComboBox option; private DefaultTableModel model; - public GUI() - { + public GUI() { model = new DefaultTableModel(new String[]{"Process", "AT", "BT", "Priority", "WT", "TAT", "RT"}, 0); table = new JTable(model); @@ -48,7 +46,7 @@ public GUI() addBtn = new JButton("Add"); addBtn.setBounds(300, 280, 85, 25); addBtn.setFont(new Font("Segoe UI", Font.PLAIN, 11)); - addBtn.addActionListener(new ActionListener(){ + addBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { model.addRow(new String[]{"", "", "", "", "", "", ""}); @@ -58,7 +56,7 @@ public void actionPerformed(ActionEvent e) { removeBtn = new JButton("Remove"); removeBtn.setBounds(390, 280, 85, 25); removeBtn.setFont(new Font("Segoe UI", Font.PLAIN, 11)); - removeBtn.addActionListener(new ActionListener(){ + removeBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); @@ -94,7 +92,7 @@ public void actionPerformed(ActionEvent e) { computeBtn = new JButton("Compute"); computeBtn.setBounds(390, 450, 85, 25); computeBtn.setFont(new Font("Segoe UI", Font.PLAIN, 11)); - computeBtn.addActionListener(new ActionListener(){ + computeBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String selected = (String) option.getSelectedItem(); @@ -136,26 +134,19 @@ public void actionPerformed(ActionEvent e) { return; } - for (int i = 0; i < model.getRowCount(); i++) - { + 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("")) - { + if (selected.equals("PSN") || selected.equals("PSP")) { + if (!model.getValueAt(i, 3).equals("")) { pl = Integer.parseInt((String) model.getValueAt(i, 3)); - } - else - { + } else { pl = 1; } - } - else - { + } else { pl = 1; } @@ -164,8 +155,7 @@ public void actionPerformed(ActionEvent e) { scheduler.process(); - for (int i = 0; i < model.getRowCount(); i++) - { + 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); @@ -204,39 +194,34 @@ public void actionPerformed(ActionEvent e) { frame.pack(); } - public static void main(String[] args) - { + public static void main(String[] args) { new GUI(); } - class CustomPanel extends JPanel - { + class CustomPanel extends JPanel { private List timeline; @Override - protected void paintComponent(Graphics g) - { + protected void paintComponent(Graphics g) { super.paintComponent(g); - if (timeline != null) - { + if (timeline != null) { + int x = 30; + int y = 20; // int width = 30; - - for (int i = 0; i < timeline.size(); i++) - { + for (int i = 0; i < timeline.size(); i++) { Event event = timeline.get(i); - int x = 30 * (i + 1); - int y = 20; + int width = event.getFinishTime() - event.getStartTime(); - g.drawRect(x, y, 30, 30); + g.drawRect(x, y, width * 4, 30); g.setFont(new Font("Segoe UI", Font.BOLD, 13)); - g.drawString(event.getProcessName(), x + 10, y + 20); + 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); - if (i == timeline.size() - 1) - { - g.drawString(Integer.toString(event.getFinishTime()), x + 27, y + 45); + x += width * 4; + if (i == timeline.size() - 1) { + g.drawString(Integer.toString(event.getFinishTime()), x, y + 45); } // width += 30; @@ -246,8 +231,7 @@ protected void paintComponent(Graphics g) } } - public void setTimeline(List timeline) - { + public void setTimeline(List timeline) { this.timeline = timeline; repaint(); }