practica1.Babage.java Source code

Java tutorial

Introduction

Here is the source code for practica1.Babage.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package practica1;

import java.awt.BorderLayout;
import java.awt.Color;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import java.util.*;

/**
 *
 * @author user
 */
public class Babage extends javax.swing.JFrame {

    private String[][] alfabetoIngles;
    private String[][] alfabeto;
    private String clave;
    private List<String> listaRepeticiones;
    private final ArrayList<Integer> listaEspacios = new ArrayList<>();
    private int[] listaOcurrencias;
    private int posicionClaveActual;

    /**
     * Creates new form Babage
     */
    public Babage(Menu menu) {
        this.listaRepeticiones = new ArrayList<>();
        this.alfabetoIngles = new String[2][26];
        this.alfabeto = new String[2][26];
        this.posicionClaveActual = 0;
        this.clave = "";
        this.listaRepeticiones = new ArrayList<>();
        this.listaOcurrencias = new int[] {};
        initComponents();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        salirBabage = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textoEncriptado = new javax.swing.JTextArea();
        analizar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        textoRepeticiones = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        cantidadClave = new javax.swing.JTextField();
        continuarAnalisis = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        grafica1 = new javax.swing.JPanel();
        restarLetra = new javax.swing.JButton();
        sumarLetra = new javax.swing.JButton();
        continuarLetra = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        grafica2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        txtClave = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        desencriptar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtMensaje = new javax.swing.JTextArea();
        btnAtras = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Mtodo de babage");
        jLabel2.setAutoscrolls(true);

        salirBabage.setText("salir");
        salirBabage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirBabageActionPerformed(evt);
            }
        });

        jLabel1.setText("Ingrese el texto encriptado:");

        textoEncriptado.setColumns(20);
        textoEncriptado.setLineWrap(true);
        textoEncriptado.setRows(5);
        textoEncriptado.setWrapStyleWord(true);
        textoEncriptado.setAutoscrolls(false);
        textoEncriptado.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        textoEncriptado.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textoEncriptadoKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(textoEncriptado);

        analizar.setText("analizar");
        analizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                analizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 472,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel1).addGap(246, 246, 246)
                                .addComponent(analizar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(analizar).addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 85,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        jLabel3.setText("Repeticiones:");

        jScrollPane2.setAutoscrolls(true);

        textoRepeticiones.setEditable(false);
        textoRepeticiones.setColumns(20);
        textoRepeticiones.setLineWrap(true);
        textoRepeticiones.setRows(5);
        textoRepeticiones.setWrapStyleWord(true);
        jScrollPane2.setViewportView(textoRepeticiones);

        jLabel4.setText("Longitud:");

        cantidadClave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantidadClaveActionPerformed(evt);
            }
        });
        cantidadClave.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cantidadClaveKeyTyped(evt);
            }
        });

        continuarAnalisis.setText("continuar");
        continuarAnalisis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                continuarAnalisisActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3).addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(cantidadClave, javax.swing.GroupLayout.PREFERRED_SIZE, 102,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128,
                                                Short.MAX_VALUE)
                                        .addComponent(continuarAnalisis))
                                .addComponent(jScrollPane2))
                        .addContainerGap()));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3).addComponent(jScrollPane2,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 82,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(
                                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(continuarAnalisis, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(jPanel2Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(cantidadClave).addComponent(jLabel4)))
                        .addContainerGap(18, Short.MAX_VALUE)));

        jPanel3.setEnabled(false);

        grafica1.setBackground(new java.awt.Color(51, 153, 255));
        grafica1.setLayout(new java.awt.BorderLayout());

        restarLetra.setText("<");
        restarLetra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                restarLetraActionPerformed(evt);
            }
        });

        sumarLetra.setText(">");
        sumarLetra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sumarLetraActionPerformed(evt);
            }
        });

        continuarLetra.setText("ok");
        continuarLetra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                continuarLetraActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(jPanel3Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGap(29, 29, 29)
                        .addComponent(restarLetra, javax.swing.GroupLayout.PREFERRED_SIZE, 49,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(grafica1, javax.swing.GroupLayout.PREFERRED_SIZE, 306,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(sumarLetra, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(continuarLetra))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(grafica1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 156,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createSequentialGroup().addGap(49, 49, 49).addComponent(restarLetra,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createSequentialGroup().addGap(49, 49, 49)
                                .addComponent(sumarLetra, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(continuarLetra, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap(20, Short.MAX_VALUE)));

        jPanel4.setEnabled(false);

        grafica2.setBackground(new java.awt.Color(51, 153, 255));
        grafica2.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout
                .setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createSequentialGroup().addGap(89, 89, 89)
                                .addComponent(grafica2, javax.swing.GroupLayout.PREFERRED_SIZE, 306,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(72, Short.MAX_VALUE)));
        jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout
                        .createSequentialGroup().addContainerGap().addComponent(grafica2,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(19, Short.MAX_VALUE)));

        txtClave.setToolTipText("");
        txtClave.setEnabled(false);

        jLabel5.setText("Clave:");

        desencriptar.setText("Desencriptar");
        desencriptar.setEnabled(false);
        desencriptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                desencriptarActionPerformed(evt);
            }
        });

        txtMensaje.setEditable(false);
        txtMensaje.setColumns(20);
        txtMensaje.setLineWrap(true);
        txtMensaje.setRows(5);
        jScrollPane3.setViewportView(txtMensaje);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(jPanel5Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane3).addComponent(txtClave)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        jPanel5Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(desencriptar))
                                .addGroup(jPanel5Layout.createSequentialGroup().addComponent(jLabel5).addGap(0, 0,
                                        Short.MAX_VALUE)))
                        .addContainerGap()));
        jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup().addGap(20, 20, 20).addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtClave, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(desencriptar).addGap(18, 18, 18).addComponent(jScrollPane3)
                        .addContainerGap()));

        btnAtras.setText("Atras");
        btnAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtrasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup().addContainerGap()
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup().addComponent(
                                                        jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(salirBabage).addGap(9, 9, 9))
                                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(jPanel4,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGap(37, 37, 37).addComponent(jPanel5,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)))))
                        .addContainerGap())
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(btnAtras)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(btnAtras)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(salirBabage, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGap(18, 18, 18).addComponent(jPanel4,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(21, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //boton para cerrar el metodo de babage
    private void salirBabageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirBabageActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_salirBabageActionPerformed

    //boton del grafico para mover las letras a la derecha
    private void sumarLetraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sumarLetraActionPerformed
        String ultimoLetras = alfabeto[0][25];
        String ultimoValor = alfabeto[1][25];
        int x;
        for (x = 25; x > 0; x--) {
            alfabeto[0][x] = alfabeto[0][x - 1];
            alfabeto[1][x] = alfabeto[1][x - 1];
        }
        alfabeto[0][x] = ultimoLetras;
        alfabeto[1][x] = ultimoValor;
        cargarData(alfabeto, null);
    }//GEN-LAST:event_sumarLetraActionPerformed

    //boton del grafico para mover las letras a la izquierda
    private void restarLetraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_restarLetraActionPerformed
        String primeroLetras = alfabeto[0][0];
        String primeroValor = alfabeto[1][0];
        int x;
        for (x = 0; x < 25; x++) {
            alfabeto[0][x] = alfabeto[0][x + 1];
            alfabeto[1][x] = alfabeto[1][x + 1];
        }
        alfabeto[0][x] = primeroLetras;
        alfabeto[1][x] = primeroValor;
        cargarData(alfabeto, null);
    }//GEN-LAST:event_restarLetraActionPerformed

    //metodo del grafico para continuar representado con un ok
    private void continuarLetraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continuarLetraActionPerformed
        if (clave.equals("")) {
            clave = alfabeto[0][0];
        } else {
            clave = clave + alfabeto[0][0];
        }

        txtClave.setText(clave + " - ");
        if (this.posicionClaveActual >= Integer.parseInt(cantidadClave.getText())) {
            continuarLetra.setEnabled(false);
            sumarLetra.setEnabled(false);
            restarLetra.setEnabled(false);
            desencriptar.setEnabled(true);
            txtClave.setText(clave);
        } else {
            continuarLetra.setEnabled(true);
            this.posicionClaveActual++;
        }
    }//GEN-LAST:event_continuarLetraActionPerformed

    private void analizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_analizarActionPerformed
        consultarDatosIngles();
        this.posicionClaveActual = 1;
        this.listaRepeticiones.clear();
        this.listaEspacios.clear();
        buscarRepetidos(textoEncriptado.getText());

        //textoRepeticiones.setText(repeticiones.toString());
        //TODO Analizar clave

    }//GEN-LAST:event_analizarActionPerformed

    private void continuarAnalisisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continuarAnalisisActionPerformed
        //grafica el primer cuadro
        consultarDatosNuevoAlfabeto();
        jPanel3.setEnabled(true);
        jPanel4.setEnabled(true);
    }//GEN-LAST:event_continuarAnalisisActionPerformed

    private void btnAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtrasActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnAtrasActionPerformed

    private void cantidadClaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidadClaveActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cantidadClaveActionPerformed

    private void cantidadClaveKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadClaveKeyTyped
        char c = evt.getKeyChar();
        if (Character.isLetter(c)) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_cantidadClaveKeyTyped

    private void textoEncriptadoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textoEncriptadoKeyTyped
        char c = evt.getKeyChar();
        if (Character.isDigit(c)) {
            getToolkit().beep();
            evt.consume();
        }
    }//GEN-LAST:event_textoEncriptadoKeyTyped

    private void desencriptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_desencriptarActionPerformed
        // TODO add your handling code here:
        BabageDesencriptar bg = new BabageDesencriptar();
        String alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String[][] matriz = new String[27][26];
        matriz = bg.llenarMatriz(matriz, alfabeto);
        String texto = bg.desencriptar(clave, matriz, textoEncriptado.getText());
        txtMensaje.setText(texto);
    }//GEN-LAST:event_desencriptarActionPerformed

    private void consultarDatosIngles() {
        double longitudTxtEncriptado = textoEncriptado.getText().length();
        alfabetoIngles = new String[][] {
                { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
                        "T", "U", "V", "W", "X", "Y", "Z" },
                { "6", "1", "2", "3", "9", "2", "1", "5", "5", "0", "1", "3", "2", "5", "6", "1", "0", "4", "5",
                        "7", "2", "1", "2", "0", "1", "0" } };
        double[] vProbabilidad = new double[] { 8.167, 1.492, 2.182, 4.253, 12.702, 2.228, 2.015, 6.094, 6.966,
                0.153, 0.772, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360,
                0.250, 1.974, 0.074 };
        for (int i = 0; i < 26; i++) {
            alfabetoIngles[1][i] = String.valueOf((int) ((vProbabilidad[i] * longitudTxtEncriptado) / 100));
        }
        cargarData(alfabetoIngles, "AlfabetoIngles");
    }

    private void consultarDatosNuevoAlfabeto() {
        alfabeto = new String[][] {
                { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
                        "T", "U", "V", "W", "X", "Y", "Z" },
                { "6", "5", "3", "5", "5", "1", "9", "5", "5", "6", "1", "10", "5", "5", "5", "5", "5", "2", "5",
                        "8", "1", "5", "4", "5", "4", "5", "5" } };

        // String abecedario = "abcdefghijklmnopqrstuvwxyz";
        int lonAlfabeto = 26;
        int multiplo = Integer.parseInt(cantidadClave.getText());
        String cifrado = textoEncriptado.getText();
        for (int i = 0; i < lonAlfabeto; i++) {
            int frecuencia = 0;
            for (int j = 0; j < cifrado.length(); j = j + multiplo) {
                if (alfabeto[0][i].equals(String.valueOf(cifrado.charAt(j)))) {
                    frecuencia++;
                }
            }
            alfabeto[1][i] = String.valueOf(frecuencia);
        }

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < lonAlfabeto; j++) {
                System.out.print(alfabeto[i][j]);
            }

        }

        cargarData(alfabeto, null);
    }

    private void cargarData(String[][] valores, String tipoGrafica) {
        DefaultCategoryDataset barchartdata = new DefaultCategoryDataset();
        for (int i = 0; i < 26; i++) {
            barchartdata.setValue(Integer.parseInt(valores[1][i]), "Cantidad", valores[0][i]);
        }
        cargarGraficos(barchartdata, tipoGrafica);
    }

    private void cargarGraficos(DefaultCategoryDataset barchartdata, String tipoGrafica) {
        String titulo;
        titulo = (tipoGrafica != null) ? "Distribucin de frecuencias en ingls"
                : "Distribucin de frecuencias L" + String.valueOf(this.posicionClaveActual);

        JFreeChart barchart = ChartFactory.createBarChart(titulo, "letra", "cantidad", barchartdata,
                PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot barchrt = barchart.getCategoryPlot();
        barchrt.setRangeGridlinePaint(Color.ORANGE);
        ChartPanel barPanel = new ChartPanel(barchart);

        if (tipoGrafica != null) {
            grafica2.removeAll();
            grafica2.add(barPanel, BorderLayout.CENTER);
            grafica2.validate();
        } else {
            grafica1.removeAll();
            grafica1.add(barPanel, BorderLayout.CENTER);
            grafica1.validate();
        }

    }

    public void buscarRepetidos(String textoInicial) {

        int cantMax = textoInicial.length() / 2;
        int cantMin = 4;

        String palabraActual;
        for (int cant = cantMax; cant >= cantMin; cant--) {

            for (int j = 0; j < textoInicial.length() - cant + 1; j++) {

                palabraActual = textoInicial.substring(j, j + cant);
                if (verificarRepeticiones(palabraActual) && contarOcurrencias(textoInicial, palabraActual) > 1) {

                    List<Integer> listaIndicesOcurrenciasPalabra = new ArrayList<>();

                    for (int i = -1; (i = textoInicial.indexOf(palabraActual, i + 1)) != -1;) {
                        listaIndicesOcurrenciasPalabra.add(i);
                    }

                    List<Integer> listaDiferencias = new ArrayList<>();
                    listaDiferencias = obtenerLista(listaIndicesOcurrenciasPalabra);

                    for (Integer integer : listaDiferencias) {
                        listaRepeticiones.add(palabraActual);
                        listaEspacios.add(integer);
                    }

                }

            }

        }
        StringBuilder repeticiones = new StringBuilder();

        for (int i = 0; i < listaRepeticiones.size(); i++) {
            repeticiones.append("seq: ");
            repeticiones.append(listaRepeticiones.get(i) + " ");
            repeticiones.append(" N: ");
            repeticiones.append(listaEspacios.get(i));
            repeticiones.append(" factores: ");
            repeticiones.append(factores(listaEspacios.get(i)));
            repeticiones.append(System.getProperty("line.separator"));
        }

        textoRepeticiones.setText(repeticiones.toString());

    }

    // Mtodo que recibe una lista de enteros y devuelve una lista de diferencias entre ellos, 
    // incluyendo todas las combinaciones posibles sin repetir.
    public static List<Integer> obtenerLista(List<Integer> lista) {
        List<Integer> listaDiferencia = new ArrayList<>();
        for (int i = 0; i < lista.size(); i++) {
            listaDiferencia.addAll(obtenerDiferencias(i, lista, listaDiferencia));
        }
        return listaDiferencia;
    }

    public static List<Integer> obtenerDiferencias(int posicion, List<Integer> lista,
            List<Integer> listaDiferencia) {
        List<Integer> nuevaLista = new ArrayList<>();
        int numeroComparar = lista.get(posicion);

        for (int i = 0; i < lista.size(); i++) {
            if (i > posicion && i < lista.size()) {
                int numero = lista.get(i) - numeroComparar;
                if (!numeroRepetido(numero, listaDiferencia)) {
                    nuevaLista.add(lista.get(i) - numeroComparar);
                }
            }
        }
        return nuevaLista;
    }

    public static boolean numeroRepetido(int numero, List<Integer> lista) {
        for (Integer integer : lista) {
            if (numero == integer) {
                return true;
            }
        }
        return false;
    }

    private static String factores(int numero) {
        StringBuilder factores = new StringBuilder();

        for (int w = 1; w <= numero; w++) {
            if (numero % w == 0) {
                factores.append(String.valueOf(w)).append(", ");
            }
        }

        if (factores.charAt(factores.length() - 1) == ' ') {
            factores.setLength(factores.length() - 2);
        }

        return factores.toString();
    }

    private boolean verificarRepeticiones(String repeticionActual) {
        for (String repeticion : listaRepeticiones) {
            if (repeticionActual.equals(repeticion)) {
                return false;
            }
        }
        return true;
    }

    private static int contarOcurrencias(String sTexto, String sTextoBuscado) {

        int contador = 0;
        while (sTexto.contains(sTextoBuscado)) {
            sTexto = sTexto.substring(sTexto.indexOf(sTextoBuscado) + sTextoBuscado.length(), sTexto.length());
            contador++;
        }
        return contador;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton analizar;
    private javax.swing.JButton btnAtras;
    private javax.swing.JTextField cantidadClave;
    private javax.swing.JButton continuarAnalisis;
    private javax.swing.JButton continuarLetra;
    private javax.swing.JButton desencriptar;
    private javax.swing.JPanel grafica1;
    private javax.swing.JPanel grafica2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton restarLetra;
    private javax.swing.JButton salirBabage;
    private javax.swing.JButton sumarLetra;
    private javax.swing.JTextArea textoEncriptado;
    private javax.swing.JTextArea textoRepeticiones;
    private javax.swing.JTextField txtClave;
    private javax.swing.JTextArea txtMensaje;
    // End of variables declaration//GEN-END:variables
}