diff --git a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java index f0d3776..9540bec 100644 --- a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java +++ b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/Main.java @@ -1,14 +1,13 @@ package com.utp.clsEstructuraDiscretas.pry4; -import javax.swing.BorderFactory; -import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; -import javax.swing.JRadioButton; -import javax.swing.JTextField; +import com.utp.clsEstructuraDiscretas.pry4.componentes.Entrada_JTextField; +import com.utp.clsEstructuraDiscretas.pry4.componentes.Preguntas; +import com.utp.clsEstructuraDiscretas.pry4.componentes.SiNoRadioButton; import com.utp.utils.Result; import java.awt.Color; @@ -17,8 +16,6 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; -import java.awt.Rectangle; -import java.awt.event.ActionListener; import java.math.BigInteger; import java.util.ArrayList; @@ -156,205 +153,6 @@ public void SendCommand(Commands command) { } } -class Entrada_JTextField { - public final JTextField n; - public final JTextField r; - final JLabel n_label; - public final JLabel r_label; - - public Entrada_JTextField(int columns) { - this.n_label = new JLabel("n"); - this.r_label = new JLabel("r"); - this.n = new JTextField(); - this.r = new JTextField(); - this.n.setColumns(columns); - this.r.setColumns(columns); - } - - JPanel panel_de(JTextField field, JLabel label) { - JPanel panel = new JPanel(new GridBagLayout()); - GridBagConstraints constraints = new GridBagConstraints(); - constraints.insets = new Insets(0, 5, 0, 5); - constraints.gridx = 0; - panel.add(label, constraints); - constraints.gridx++; - panel.add(field, constraints); - return panel; - } - - public JPanel as_panel() { - JPanel panel = new JPanel(new GridBagLayout()); - GridBagConstraints constraints = new GridBagConstraints(); - constraints.insets = new Insets(0, 5, 0, 5); - constraints.gridx = 0; - constraints.gridy = 0; - constraints.anchor = GridBagConstraints.EAST; - panel.add(panel_de(n, n_label), constraints); - constraints.gridy++; - panel.add(panel_de(r, r_label), constraints); - return panel; - } - - public void addActionListener(ActionListener l) { - var key_adapter = new java.awt.event.KeyAdapter() { - public void keyReleased(java.awt.event.KeyEvent evt) { - l.actionPerformed(null); - } - }; - n.addKeyListener(key_adapter); - r.addKeyListener(key_adapter); - } - -} - -class Preguntas { - public final SiNoRadioButton orden; - public final SiNoRadioButton repetible; - public final SiNoRadioButton exhaustivo; - - public Preguntas(SiNoRadioButton orden, SiNoRadioButton exhaustivo, SiNoRadioButton repetible) { - this.orden = orden; - this.repetible = repetible; - this.exhaustivo = exhaustivo; - - exhaustivo.setEnabled(orden.is_yes()); - orden.addActionListener(e -> { - exhaustivo.setEnabled(orden.is_yes()); - }); - } - - public VarianteFormula tipo_formula() { - if (orden.is_yes()) { - if (exhaustivo.is_yes()) { - if (repetible.is_yes()) { - return VarianteFormula.PERMUTACIONES_REPETIDAS; - } else { - return VarianteFormula.PERMUTACIONES; - } - } else { - if (repetible.is_yes()) { - return VarianteFormula.VARIANZA_REPETIDAS; - } else { - return VarianteFormula.VARIANZA; - } - } - } else { - if (repetible.is_yes()) { - return VarianteFormula.COMBINACIONES_REPETIDAS; - } else { - return VarianteFormula.COMBINACIONES; - } - } - } - - public JPanel as_panel() { - JPanel panel = new JPanel(new GridBagLayout()); - // Acoplamos los RadioButtons - GridBagConstraints constraints = new GridBagConstraints(); - constraints.insets = new Insets(0, 5, 0, 5); - constraints.gridx = 0; - panel.add(orden.as_panel("¿Importa el orden?"), constraints); - constraints.gridx++; - panel.add(exhaustivo.as_panel("¿Se usan todos?"), constraints); - constraints.gridx++; - panel.add(repetible.as_panel("¿Se pueden repetir los elementos?"), constraints); - - // Acoplamos el Titulo del contenedor - var titulo = BorderFactory.createTitledBorder("Tipo de Problema: " + this.tipo_formula().toString()); - this.addActionListener(e -> { - VarianteFormula tipo_de_formula = this.tipo_formula(); - titulo.setTitleColor(tipo_de_formula.as_color()); - titulo.setTitle("Tipo de Problema: " + tipo_de_formula.toString()); - panel.repaint(); - }); - panel.setBorder(titulo); - - return panel; - } - - public void addActionListener(ActionListener l) { - orden.addActionListener(l); - repetible.addActionListener(l); - exhaustivo.addActionListener(l); - - } -} - -class SiNoRadioButton { - public final JRadioButton si; - public final JRadioButton no; - public final ButtonGroup group; - - public SiNoRadioButton() { - this.si = new JRadioButton("Si"); - this.no = new JRadioButton("No"); - this.group = new ButtonGroup(); - group.add(si); - group.add(no); - Rectangle bounds = new Rectangle(20, 20); - si.setBounds(bounds); - } - - public JPanel as_panel(String msg) { - JPanel panel = new JPanel(new GridBagLayout()); - var border = BorderFactory.createTitledBorder(msg); - border.setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 14)); - panel.setBorder(border); - - GridBagConstraints constraints = new GridBagConstraints(); - constraints.insets = new Insets(0, 20, 0, 20); - constraints.anchor = GridBagConstraints.CENTER; - constraints.gridx = 0; - constraints.gridy = 0; - panel.add(si, constraints); - constraints.gridx++; - panel.add(no, constraints); - - // Arreglar que el titulo no se corte - Dimension border_size = border.getMinimumSize(panel); - Dimension prefered_size = panel.getPreferredSize(); - Insets panel_insets = panel.getInsets(); - int border_width = panel_insets.left + border_size.width + panel_insets.right + 20; - int prefered_width = Math.max(prefered_size.width, border_width); - panel.setPreferredSize(new Dimension(prefered_width, prefered_size.height)); - - // repintar el panel - Color dark_green = new Color(1, 113, 72); - Runnable repaint = () -> { - if (is_yes()) { - border.setTitleColor(dark_green); - } else { - border.setTitleColor(Color.RED); - } - panel.repaint(); - }; - - this.no.addActionListener(e -> { - repaint.run(); - }); - this.si.addActionListener(e -> { - repaint.run(); - }); - - this.no.setSelected(true); - repaint.run(); - return panel; - } - - public void addActionListener(ActionListener l) { - si.addActionListener(l); - no.addActionListener(l); - } - - public void setEnabled(boolean enabled) { - si.setEnabled(enabled); - no.setEnabled(enabled); - } - - public boolean is_yes() { - return si.isSelected(); - } -} // @formatter:off sealed interface Commands { @@ -362,71 +160,3 @@ public record CalculateCombinatoria(String n, String r) implements Commands {} public record RepaintAll() implements Commands {} } // @formatter:on - -enum VarianteFormula { - PERMUTACIONES, COMBINACIONES, VARIANZA, PERMUTACIONES_REPETIDAS, COMBINACIONES_REPETIDAS, VARIANZA_REPETIDAS; - - static final Color Azul = new Color(1, 75, 160); - static final Color Morado = new Color(163, 73, 164); - static final Color Dorado = new Color(239, 184, 16); - static final Color Chocolate = new Color(105, 63, 38); - static final Color AzulClaro = new Color(0, 171, 240); - static final Color Magenta = new Color(200, 0, 167); - - public Color as_color() { - switch (this) { - case PERMUTACIONES -> { - return Azul; - } - case COMBINACIONES -> { - return Morado; - } - case VARIANZA -> { - return Dorado; - } - case PERMUTACIONES_REPETIDAS -> { - return Chocolate; - } - case COMBINACIONES_REPETIDAS -> { - return AzulClaro; - } - case VARIANZA_REPETIDAS -> { - return Magenta; - } - default -> { - return Color.BLACK; - } - } - - } - - public Result calcular(String n, String r) { - switch (this) { - case PERMUTACIONES -> { - return Formulas.Permutacion_sin_repeticion(new String[] { n, r }); - } - case COMBINACIONES -> { - return Formulas.Comb_sin_repeticion(new String[] { n, r }); - } - case VARIANZA -> { - return Formulas.Varianza_sin_repeticion(new String[] { n, r }); - } - case PERMUTACIONES_REPETIDAS -> { - var split_r = r.split(","); - for (int i = 0; i < split_r.length; i++) - split_r[i] = split_r[i].trim(); - String[] args = new String[split_r.length + 1]; - args[0] = n; - System.arraycopy(split_r, 0, args, 1, split_r.length); - return Formulas.Permutacion_con_repeticion(args); - } - case COMBINACIONES_REPETIDAS -> { - return Formulas.Comb_con_repeticion(new String[] { n, r }); - } - case VARIANZA_REPETIDAS -> { - return Formulas.Varianza_con_repeticion(new String[] { n, r }); - } - } - return Result.error(new UnsupportedOperationException("No se ha implementado la formula")); - } -} diff --git a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/VarianteFormula.java b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/VarianteFormula.java new file mode 100644 index 0000000..f55562e --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/VarianteFormula.java @@ -0,0 +1,74 @@ +package com.utp.clsEstructuraDiscretas.pry4; + +import java.awt.Color; +import java.math.BigInteger; + +import com.utp.utils.Result; + +public enum VarianteFormula { + PERMUTACIONES, COMBINACIONES, VARIANZA, PERMUTACIONES_REPETIDAS, COMBINACIONES_REPETIDAS, VARIANZA_REPETIDAS; + + static final Color Azul = new Color(1, 75, 160); + static final Color Morado = new Color(163, 73, 164); + static final Color Dorado = new Color(239, 184, 16); + static final Color Chocolate = new Color(105, 63, 38); + static final Color AzulClaro = new Color(0, 171, 240); + static final Color Magenta = new Color(200, 0, 167); + + public Color as_color() { + switch (this) { + case PERMUTACIONES -> { + return Azul; + } + case COMBINACIONES -> { + return Morado; + } + case VARIANZA -> { + return Dorado; + } + case PERMUTACIONES_REPETIDAS -> { + return Chocolate; + } + case COMBINACIONES_REPETIDAS -> { + return AzulClaro; + } + case VARIANZA_REPETIDAS -> { + return Magenta; + } + default -> { + return Color.BLACK; + } + } + + } + + public Result calcular(String n, String r) { + switch (this) { + case PERMUTACIONES -> { + return Formulas.Permutacion_sin_repeticion(new String[] { n, r }); + } + case COMBINACIONES -> { + return Formulas.Comb_sin_repeticion(new String[] { n, r }); + } + case VARIANZA -> { + return Formulas.Varianza_sin_repeticion(new String[] { n, r }); + } + case PERMUTACIONES_REPETIDAS -> { + var split_r = r.split(","); + for (int i = 0; i < split_r.length; i++) + split_r[i] = split_r[i].trim(); + String[] args = new String[split_r.length + 1]; + args[0] = n; + System.arraycopy(split_r, 0, args, 1, split_r.length); + return Formulas.Permutacion_con_repeticion(args); + } + case COMBINACIONES_REPETIDAS -> { + return Formulas.Comb_con_repeticion(new String[] { n, r }); + } + case VARIANZA_REPETIDAS -> { + return Formulas.Varianza_con_repeticion(new String[] { n, r }); + } + } + return Result.error(new UnsupportedOperationException("No se ha implementado la formula")); + } +} diff --git a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/Entrada_JTextField.java b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/Entrada_JTextField.java new file mode 100644 index 0000000..c6679a5 --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/Entrada_JTextField.java @@ -0,0 +1,61 @@ +package com.utp.clsEstructuraDiscretas.pry4.componentes; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionListener; + +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; + +public class Entrada_JTextField { + public final JTextField n; + public final JTextField r; + final JLabel n_label; + public final JLabel r_label; + + public Entrada_JTextField(int columns) { + this.n_label = new JLabel("n"); + this.r_label = new JLabel("r"); + this.n = new JTextField(); + this.r = new JTextField(); + this.n.setColumns(columns); + this.r.setColumns(columns); + } + + JPanel panel_de(JTextField field, JLabel label) { + JPanel panel = new JPanel(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets = new Insets(0, 5, 0, 5); + constraints.gridx = 0; + panel.add(label, constraints); + constraints.gridx++; + panel.add(field, constraints); + return panel; + } + + public JPanel as_panel() { + JPanel panel = new JPanel(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets = new Insets(0, 5, 0, 5); + constraints.gridx = 0; + constraints.gridy = 0; + constraints.anchor = GridBagConstraints.EAST; + panel.add(panel_de(n, n_label), constraints); + constraints.gridy++; + panel.add(panel_de(r, r_label), constraints); + return panel; + } + + public void addActionListener(ActionListener l) { + var key_adapter = new java.awt.event.KeyAdapter() { + public void keyReleased(java.awt.event.KeyEvent evt) { + l.actionPerformed(null); + } + }; + n.addKeyListener(key_adapter); + r.addKeyListener(key_adapter); + } + +} diff --git a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/Preguntas.java b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/Preguntas.java new file mode 100644 index 0000000..bea2a85 --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/Preguntas.java @@ -0,0 +1,84 @@ +package com.utp.clsEstructuraDiscretas.pry4.componentes; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionListener; + +import javax.swing.BorderFactory; +import javax.swing.JPanel; + +import com.utp.clsEstructuraDiscretas.pry4.VarianteFormula; + +public class Preguntas { + public final SiNoRadioButton orden; + public final SiNoRadioButton repetible; + public final SiNoRadioButton exhaustivo; + + public Preguntas(SiNoRadioButton orden, SiNoRadioButton exhaustivo, SiNoRadioButton repetible) { + this.orden = orden; + this.repetible = repetible; + this.exhaustivo = exhaustivo; + + exhaustivo.setEnabled(orden.is_yes()); + orden.addActionListener(e -> { + exhaustivo.setEnabled(orden.is_yes()); + }); + } + + public VarianteFormula tipo_formula() { + if (orden.is_yes()) { + if (exhaustivo.is_yes()) { + if (repetible.is_yes()) { + return VarianteFormula.PERMUTACIONES_REPETIDAS; + } else { + return VarianteFormula.PERMUTACIONES; + } + } else { + if (repetible.is_yes()) { + return VarianteFormula.VARIANZA_REPETIDAS; + } else { + return VarianteFormula.VARIANZA; + } + } + } else { + if (repetible.is_yes()) { + return VarianteFormula.COMBINACIONES_REPETIDAS; + } else { + return VarianteFormula.COMBINACIONES; + } + } + } + + public JPanel as_panel() { + JPanel panel = new JPanel(new GridBagLayout()); + // Acoplamos los RadioButtons + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets = new Insets(0, 5, 0, 5); + constraints.gridx = 0; + panel.add(orden.as_panel("¿Importa el orden?"), constraints); + constraints.gridx++; + panel.add(exhaustivo.as_panel("¿Se usan todos?"), constraints); + constraints.gridx++; + panel.add(repetible.as_panel("¿Se pueden repetir los elementos?"), constraints); + + // Acoplamos el Titulo del contenedor + var titulo = BorderFactory.createTitledBorder("Tipo de Problema: " + this.tipo_formula().toString()); + this.addActionListener(e -> { + VarianteFormula tipo_de_formula = this.tipo_formula(); + titulo.setTitleColor(tipo_de_formula.as_color()); + titulo.setTitle("Tipo de Problema: " + tipo_de_formula.toString()); + panel.repaint(); + }); + panel.setBorder(titulo); + + return panel; + } + + public void addActionListener(ActionListener l) { + orden.addActionListener(l); + repetible.addActionListener(l); + exhaustivo.addActionListener(l); + + } +} \ No newline at end of file diff --git a/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/SiNoRadioButton.java b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/SiNoRadioButton.java new file mode 100644 index 0000000..4cb1d5a --- /dev/null +++ b/src/main/java/com/utp/clsEstructuraDiscretas/pry4/componentes/SiNoRadioButton.java @@ -0,0 +1,91 @@ +package com.utp.clsEstructuraDiscretas.pry4.componentes; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.Rectangle; +import java.awt.event.ActionListener; + +import javax.swing.BorderFactory; +import javax.swing.ButtonGroup; +import javax.swing.JPanel; +import javax.swing.JRadioButton; + +public class SiNoRadioButton { + public final JRadioButton si; + public final JRadioButton no; + public final ButtonGroup group; + + public SiNoRadioButton() { + this.si = new JRadioButton("Si"); + this.no = new JRadioButton("No"); + this.group = new ButtonGroup(); + group.add(si); + group.add(no); + Rectangle bounds = new Rectangle(20, 20); + si.setBounds(bounds); + } + + public JPanel as_panel(String msg) { + JPanel panel = new JPanel(new GridBagLayout()); + var border = BorderFactory.createTitledBorder(msg); + border.setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 14)); + panel.setBorder(border); + + GridBagConstraints constraints = new GridBagConstraints(); + constraints.insets = new Insets(0, 20, 0, 20); + constraints.anchor = GridBagConstraints.CENTER; + constraints.gridx = 0; + constraints.gridy = 0; + panel.add(si, constraints); + constraints.gridx++; + panel.add(no, constraints); + + // Arreglar que el titulo no se corte + Dimension border_size = border.getMinimumSize(panel); + Dimension prefered_size = panel.getPreferredSize(); + Insets panel_insets = panel.getInsets(); + int border_width = panel_insets.left + border_size.width + panel_insets.right + 20; + int prefered_width = Math.max(prefered_size.width, border_width); + panel.setPreferredSize(new Dimension(prefered_width, prefered_size.height)); + + // repintar el panel + Color dark_green = new Color(1, 113, 72); + Runnable repaint = () -> { + if (is_yes()) { + border.setTitleColor(dark_green); + } else { + border.setTitleColor(Color.RED); + } + panel.repaint(); + }; + + this.no.addActionListener(e -> { + repaint.run(); + }); + this.si.addActionListener(e -> { + repaint.run(); + }); + + this.no.setSelected(true); + repaint.run(); + return panel; + } + + public void addActionListener(ActionListener l) { + si.addActionListener(l); + no.addActionListener(l); + } + + public void setEnabled(boolean enabled) { + si.setEnabled(enabled); + no.setEnabled(enabled); + } + + public boolean is_yes() { + return si.isSelected(); + } +}