From c7887c032676dc0c549f1951d15bf44e2759ff74 Mon Sep 17 00:00:00 2001 From: Laifsyn <99366187+Laifsyn@users.noreply.github.com> Date: Sat, 15 Jun 2024 11:50:18 +0000 Subject: [PATCH] stage changes - App + Visual Output --- .../Estructuras/colas/AbstractCola.java | 124 ++++++++++++++ .../laboratorio_3/Main.java | 156 ++++++++++++++++++ 2 files changed, 280 insertions(+) create mode 100644 src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java index 2a58b81..7fc1102 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java @@ -1,5 +1,12 @@ package com.utp.clsEstructuraDatos.Estructuras.colas; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; + +import javax.swing.JLabel; +import javax.swing.JPanel; + public abstract class AbstractCola { /** * Indice donde se puede extraer el siguiente elemento @@ -28,6 +35,10 @@ public AbstractCola(int capacidad) { this.inner = (T[]) new Object[capacidad]; } + public int capacity() { + return this.capacidad; + } + /** * Evalua si la cola está vacía al comparar la longitud con 0 */ @@ -82,6 +93,119 @@ public String toString() { return sb.toString(); } + public JPanel as_Jpanel() { + return new PanelDrawer(this).as_panel(); + } + + private class PanelDrawer { + final JPanel panel = new JPanel(new GridBagLayout()); + final AbstractCola cola; + final JLabel front_surplus = new JLabel(); + final JLabel frente_item = new JLabel(); + final JLabel middle = new JLabel(); + final JLabel cola_item = new JLabel(); + final JLabel cola_surplus = new JLabel(); + + /** + * ?["{frente-1}"] + * ["{this[frente]}"] + * [..."{longitud}"...] + * ["...{this[cola-1]}] + * ?[cola-1 - capacidad] + */ + PanelDrawer(AbstractCola cola) { + this.cola = cola; + var c = new GridBagConstraints(0, 0, 1, 5, 0, 0, GridBagConstraints.CENTER, 0, new Insets(5, 5, 5, 5), 0, + 0); + panel.add(front_surplus, c); + c.gridy = 1; + panel.add(frente_item, c); + c.gridy = 2; + panel.add(middle, c); + c.gridy = 3; + panel.add(cola_item, c); + c.gridy = 4; + panel.add(cola_surplus, c); + } + + JPanel as_panel() { + int frente = this.cola.frente; + int cola = this.cola.cola; + int capacidad = this.cola.capacity(); + int longitud = this.cola.longitud; + + // ****************************************** + + if (frente > cola) + wrapped(frente, cola, capacidad, longitud); + else + aligned(frente, cola, capacidad, longitud); + + // ****************************************** + return this.panel; + } + + /** + * llamado cuando frente (donde se retira de la fila) es menor o igual que el + * indice de la cola + */ + void aligned(int frente, int cola, int capacidad, int longitud) { + + if (frente > 0) + this.front_surplus.setText("[" + (frente - 1) + "]"); + else + this.front_surplus.setText(""); + + // ****************************************** + + this.frente_item.setText("[" + this.cola.inner[frente] + "]"); + // ****************************************** + + if (longitud > 0) + this.middle.setText("[..." + longitud + "...]"); + else + this.middle.setText("[0]"); + + // ****************************************** + this.cola_item.setText("[" + this.cola.inner[cola] + "]"); + + // ****************************************** + + if (cola < capacidad - 1) + this.cola_surplus.setText("[" + (cola + 1 - capacidad) + "]"); + else + this.cola_surplus.setText(""); + } + + /** + * llamado cuando frente (donde se retira de la fila) es mayor que el indice de + * la cola + */ + void wrapped(int frente, int cola, int capacidad, int longitud) { + + this.front_surplus.setText("(" + frente + ")"); + + // ****************************************** + + this.frente_item.setText("[" + this.cola.inner[frente] + "]"); + + // ****************************************** + + if (longitud > 0) + this.middle.setText("[..." + longitud + "...]"); + else + this.middle.setText("[0]"); + + // ****************************************** + this.cola_item.setText("[" + this.cola.inner[cola] + "]"); + + // ****************************************** + + this.cola_surplus.setText("[" + (cola + 1 - capacidad) + "]"); + } + + } + // @formatter:off abstract public Result insertar(T elemento); abstract public Option quitar(); diff --git a/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java b/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java new file mode 100644 index 0000000..e26406e --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java @@ -0,0 +1,156 @@ +package com.utp.clsEstructuraDatos.laboratorio_3; + +import javax.management.RuntimeErrorException; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JPanel; + +import com.utp.clsEstructuraDatos.Estructuras.colas.AbstractCola; +import com.utp.clsEstructuraDatos.Estructuras.colas.ColaCircular; +import com.utp.clsEstructuraDatos.Estructuras.colas.ColaSimple; + +import static com.utp.clsEstructuraDatos.laboratorio_3.ColasApp.CMD.*; + +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; + +public class Main { + public static void main(String[] args) { + ColasApp app = new ColasApp(); + app.run_app(); + } +} + +class ColasApp { + static public String LABORATORIO = "Laboratorio 3"; + final JButton btn_crear_cola = new JButton("Crear Cola"); + final JButton btn_insertar = new JButton("Insertar"); + final JButton btn_quitar = new JButton("Quitar"); + final JButton btn_limpiar = new JButton("Limpiar"); + final JButton btn_mostrar = new JButton("Mostrar"); + final JFrame frame = new JFrame(LABORATORIO); + AbstractCola cola; + + static int COLA_CIRCULAR = 0; + static int COLA_SIMPLE = 1; + + ColasApp() {} + + public void run_app() { + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + frame.setLayout(null); + frame.add(content()); + this.send_command(null); + } + + JPanel content() { + JPanel content = new JPanel(); + content.setLayout(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(0, 0, 2, 3, 0, 0, GridBagConstraints.CENTER, 0, + new Insets(5, 5, 5, 5), 0, 0); + + c.gridwidth = 2; + content.add(this.btn_crear_cola, c); + + c.gridy = 1; + c.gridwidth = 1; + content.add(this.btn_insertar, c); + c.gridx = 1; + content.add(this.btn_quitar, c); + + c.gridy = 2; + c.gridx = 0; + content.add(this.btn_limpiar, c); + c.gridx = 1; + content.add(this.btn_mostrar, c); + + c.gridy = 3; + c.gridx = 0; + + + return content; + } + + // Packs the frame to the preferred size, updating if undersized + public void try_pack() { + Dimension current_size = frame.getSize(); + Dimension preferred_size = frame.getPreferredSize(); + int new_width = Math.max(current_size.width, preferred_size.width); + int new_height = Math.max(current_size.height, preferred_size.height); + if (new_width != current_size.width || new_height != current_size.height) + frame.setSize(new_width, new_height); + } + + public int prompt_colas_type() { + int num = JOptionPane.showOptionDialog(frame, "Seleccione el tipo de cola a utilizar", + LABORATORIO + ": Tipo de Cola", + JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, + new String[] { "Cola Circular", "Cola Simple" }, "Cola Circular"); + System.out.println("Seleccion del tipo: " + num); + return num; + } + + void switch_buttons(boolean value) { + this.btn_insertar.setEnabled(value); + this.btn_quitar.setEnabled(value); + this.btn_limpiar.setEnabled(value); + this.btn_mostrar.setEnabled(value); + } + + // Event Manager + public void send_command(CMD command) { + switch (command) { + case Pack() -> { + this.try_pack(); + } + case CrearCola(int capacidad_cola) -> { + int cola = prompt_colas_type(); + if (cola == COLA_CIRCULAR) { + this.cola = new ColaCircular<>(capacidad_cola); + } else { + this.cola = new ColaSimple<>(capacidad_cola); + } + String new_text = String.format("[%s (c:%d)]Recrear Cola", this.cola.getClass().getSimpleName(), + this.cola.capacity()); + this.btn_crear_cola.setText(new_text); + switch_buttons(true); + } + case Insertar(Integer elemento) -> { + this.cola.insertar(elemento); + } + case Quitar() -> { + this.cola.quitar(); + } + case Limpiar() -> { + this.cola.limpiar(); + } + case Mostrar() -> { + throw new RuntimeErrorException(null, "Not Implemented"); + } + case null -> { + } + default -> { + throw new RuntimeErrorException(null, "Unsopported Command " + command.getClass().getName()); + } + } + if (this.cola == null) { + this.btn_crear_cola.setText("Crear Cola"); + switch_buttons(false); + } + } + + public static sealed interface CMD { + // @formatter:off + public static record Pack() implements CMD {} + public static record CrearCola(int capacidad_cola) implements CMD {} + public static record Insertar(T elemento) implements CMD {} + public static record Quitar() implements CMD {} + public static record Limpiar() implements CMD {} + public static record Mostrar() implements CMD {} + // @formatter:on + } +}