Untitled
package com.mycompany.fcfs_algoritmo; /** * * @author Jefferson E & Fabian Perilla */ import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class FCFS_Algoritmo { private JFrame frame; private JTextField txtProceso, txtTiempoLlegada, txtRafaga; private DefaultTableModel tableModel; private JTable table; private JPanel ganttPanel; private Timer timer; private int currentProcess = 0; private int currentColumn = 3; private int currentTime = 0; private int[] tiempoComienzo, tiempoFinalizacion, tiempoRetorno, tiempoEspera; private int tiempoActual = 0; public static void main(String[] args) { SwingUtilities.invokeLater(FCFS_Algoritmo::new); } public FCFS_Algoritmo() { frame = new JFrame("Algoritmo FCFS"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(900, 600); frame.setLayout(new BorderLayout()); JPanel inputPanel = new JPanel(); inputPanel.setLayout(new GridLayout(4, 2)); inputPanel.add(new JLabel("Nombre del Proceso:")); txtProceso = new JTextField(); inputPanel.add(txtProceso); inputPanel.add(new JLabel("Tiempo de Llegada:")); txtTiempoLlegada = new JTextField(); inputPanel.add(txtTiempoLlegada); inputPanel.add(new JLabel("Ráfaga:")); txtRafaga = new JTextField(); inputPanel.add(txtRafaga); JButton btnAgregar = new JButton("Agregar Proceso"); inputPanel.add(btnAgregar); frame.add(inputPanel, BorderLayout.NORTH); tableModel = new DefaultTableModel(new String[]{"Proceso", "Tiempo Llegada", "Ráfaga", "Tiempo Comienzo", "Tiempo Final", "Tiempo Retorno", "Tiempo Espera"}, 0); table = new JTable(tableModel); JScrollPane scrollPane = new JScrollPane(table); frame.add(scrollPane, BorderLayout.CENTER); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BorderLayout()); JButton btnCalcular = new JButton("Calcular Resultados"); bottomPanel.add(btnCalcular, BorderLayout.NORTH); ganttPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); dibujarDiagramaGantt(g); } }; ganttPanel.setPreferredSize(new Dimension(800, 200)); bottomPanel.add(ganttPanel, BorderLayout.CENTER); frame.add(bottomPanel, BorderLayout.SOUTH); btnAgregar.addActionListener(e -> agregarProceso()); btnCalcular.addActionListener(e -> calcularResultados()); JButton btnSalir = new JButton("Salir"); btnSalir.addActionListener(e -> System.exit(0)); bottomPanel.add(btnSalir, BorderLayout.SOUTH); frame.setVisible(true); } private void agregarProceso() { try { String proceso = txtProceso.getText().trim(); int tiempoLlegada = Integer.parseInt(txtTiempoLlegada.getText().trim()); int rafaga = Integer.parseInt(txtRafaga.getText().trim()); tableModel.addRow(new Object[]{proceso, tiempoLlegada, rafaga, "", "", "", ""}); txtProceso.setText(""); txtTiempoLlegada.setText(""); txtRafaga.setText(""); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(frame, "Por favor, ingrese valores válidos.", "Error", JOptionPane.ERROR_MESSAGE); } } private void calcularResultados() { int rowCount = tableModel.getRowCount(); if (rowCount == 0) { JOptionPane.showMessageDialog(frame, "No hay procesos para calcular.", "Error", JOptionPane.ERROR_MESSAGE); return; } tiempoComienzo = new int[rowCount]; tiempoFinalizacion = new int[rowCount]; tiempoRetorno = new int[rowCount]; tiempoEspera = new int[rowCount]; currentProcess = 0; currentColumn = 3; currentTime = 0; tiempoActual = 0; timer = new Timer(500, e -> { if (currentProcess < rowCount) { int tiempoLlegada = (int) tableModel.getValueAt(currentProcess, 1); int rafaga = (int) tableModel.getValueAt(currentProcess, 2); if (tiempoActual < tiempoLlegada) { tiempoActual = tiempoLlegada; } switch (currentColumn) { case 3: tiempoComienzo[currentProcess] = tiempoActual; tableModel.setValueAt(tiempoComienzo[currentProcess], currentProcess, 3); currentColumn++; break; case 4: tiempoFinalizacion[currentProcess] = tiempoComienzo[currentProcess] + rafaga; tableModel.setValueAt(tiempoFinalizacion[currentProcess], currentProcess, 4); // Dibujar el proceso en el diagrama de Gantt inmediatamente currentTime = tiempoFinalizacion[currentProcess]; ganttPanel.repaint(); currentColumn++; break; case 5: tiempoRetorno[currentProcess] = tiempoFinalizacion[currentProcess] - tiempoLlegada; tableModel.setValueAt(tiempoRetorno[currentProcess], currentProcess, 5); currentColumn++; break; case 6: tiempoEspera[currentProcess] = tiempoRetorno[currentProcess] - rafaga; tableModel.setValueAt(tiempoEspera[currentProcess], currentProcess, 6); currentColumn = 3; tiempoActual += rafaga; currentProcess++; break; } } else { timer.stop(); } }); timer.start(); } private void dibujarDiagramaGantt(Graphics g) { int rowCount = tableModel.getRowCount(); if (rowCount == 0) return; int tiempoMaximo = 0; for (int i = 0; i < rowCount; i++) { if (i < tiempoFinalizacion.length) { tiempoMaximo = Math.max(tiempoMaximo, tiempoFinalizacion[i]); } } int width = ganttPanel.getWidth(); int height = ganttPanel.getHeight(); int unidadTiempo = width / (tiempoMaximo + 1); int alturaProceso = height / (rowCount + 1); g.setColor(Color.BLACK); for (int i = 0; i <= tiempoMaximo; i++) { int x = i * unidadTiempo; g.drawString(String.valueOf(i), x, alturaProceso / 2); } for (int i = 0; i < currentProcess && i < rowCount; i++) { int tiempoComienzo = this.tiempoComienzo[i]; int rafaga = (int) tableModel.getValueAt(i, 2); String proceso = (String) tableModel.getValueAt(i, 0); int y = (i + 1) * alturaProceso; g.setColor(Color.BLACK); g.drawString(proceso, 10, y + alturaProceso / 2 - 5); Color colorProceso = Color.getHSBColor((i * 1.0f) / rowCount, 1.0f, 1.0f); g.setColor(colorProceso); for (int j = 0; j < rafaga; j++) { int x = (tiempoComienzo + j) * unidadTiempo; g.fillRect(x, y, unidadTiempo - 2, alturaProceso - 2); } } } }
Leave a Comment