Gui.Pantalla.java Source code

Java tutorial

Introduction

Here is the source code for Gui.Pantalla.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 Gui;

import Facade.facade;
import java.awt.BorderLayout;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
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 org.jfree.data.category.DefaultCategoryDataset;
import org.json.simple.JSONArray;
import logica.Array;

/**
 *
 * @author ellioth
 */
public class Pantalla extends javax.swing.JFrame {
    private DefaultCategoryDataset cuadro;
    private DefaultCategoryDataset cuadroPC;
    private int tipoTrabajo;
    private String total;
    private Array arreglo;
    private int CantMax;
    private facade fac;
    private boolean ABB;
    private boolean ASP;
    private boolean AVL;
    private boolean ARN;
    private long TABB;
    private long TASP;
    private long TAVL;
    private long TARN;
    private long TABBPC;
    private long TASPPC;
    private long TAVLPC;
    private long TARNPC;
    private Tutorial tut;

    /**
     * Creates new form Pantalla
     */
    public Pantalla() {
        total = "";
        tipoTrabajo = 0;
        CantMax = 0;
        fac = new facade();
        arreglo = new Array();
        cuadro = new DefaultCategoryDataset();
        cuadroPC = new DefaultCategoryDataset();
        initComponents();
    }

    /**
     * agarra el dato de la cantidad maxima que se eligio en el jTextField
     * lo convierte en int y lo devuelve a donde se le llame.
     * @return CantMax
     */
    private int getCant() {
        total = InText.getText();
        CantMax = Integer.parseInt(total);
        return CantMax;
    }

    /**
     * grupo de cuatro metodos utitilitarios para tomar el tiempo de duracin aproximada 
     * en cada accion tomada.
     * @param json
     * @param jsonPC
     * @param clase
     * @param dato 
     */
    private void setTABB(JSONArray json, JSONArray jsonPC, int clase, int dato1, int dato2) {
        try {
            if (tipoTrabajo == 0) {
                TABB = fac.comI(json, clase);
                TABBPC = fac.comIPC(jsonPC, clase);
            } else if (tipoTrabajo == 1) {
                TABB = fac.comD(json, clase, dato1);
                TABBPC = fac.comDPC(jsonPC, clase, dato2);
            } else {
                TABB = fac.comF(json, clase, dato1);
                TABBPC = fac.comFPC(jsonPC, clase, dato2);
            }
            cuadro.addValue(TABB, "Binario", "ABB");
            cuadroPC.addValue(TABBPC, "Binario", "ABB");
        } catch (IOException ex) {
            Logger.getLogger(Pantalla.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("se cayo en el metodo de Arbol binario");
        }
    }

    private void setTASP(JSONArray json, JSONArray jsonPC, int clase, int dato1, int dato2) {
        try {
            if (tipoTrabajo == 0) {
                TASP = fac.comI(json, clase);
                TASPPC = fac.comIPC(jsonPC, clase);
            } else if (tipoTrabajo == 1) {
                TASP = fac.comD(json, clase, dato1);
                TASPPC = fac.comDPC(jsonPC, clase, dato2);
            } else {
                TASP = fac.comF(json, clase, dato1);
                TASPPC = fac.comFPC(jsonPC, clase, dato2);
            }
            cuadro.addValue(TASP, "Splay", "ASP");
            cuadroPC.addValue(TASPPC, "Splay", "ASP");
        } catch (IOException ex) {
            Logger.getLogger(Pantalla.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setTAVL(JSONArray json, JSONArray jsonPC, int clase, int dato1, int dato2) {
        try {
            if (tipoTrabajo == 0) {
                TAVL = fac.comI(json, clase);
                TAVLPC = fac.comIPC(jsonPC, clase);
            } else if (tipoTrabajo == 1) {
                TAVL = fac.comD(json, clase, dato1);
                TAVLPC = fac.comDPC(jsonPC, clase, dato2);
            } else {
                TAVL = fac.comF(json, clase, dato1);
                TAVLPC = fac.comFPC(jsonPC, clase, dato2);
            }
            cuadro.addValue(TAVL, "AVL", "AVL");
            cuadroPC.addValue(TAVLPC, "AVL", "AVL");
        } catch (IOException ex) {
            Logger.getLogger(Pantalla.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void setTARN(JSONArray json, JSONArray jsonPC, int clase, int dato1, int dato2) {
        try {
            if (tipoTrabajo == 0) {
                TARN = fac.comI(json, clase);
                TARNPC = fac.comIPC(jsonPC, clase);
            } else if (tipoTrabajo == 1) {
                TARN = fac.comD(json, clase, dato1);
                TARNPC = fac.comDPC(jsonPC, clase, dato2);
            } else {
                TARN = fac.comF(json, clase, dato1);
                TARNPC = fac.comFPC(jsonPC, clase, dato2);
            }
            cuadro.addValue(TARN, "Rojo-Negro", "ARN");
            cuadroPC.addValue(TARNPC, "Rojo-Negro", "ARN");
        } catch (IOException ex) {
            Logger.getLogger(Pantalla.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * recibe el parametro i que sirve para verificar que tipo de operacion se va a
     * realizar, verfica si se valida el arbol que queremos.
     * @param i 
     */
    private void AddGraf() {
        arreglo.llenar(getCant());
        arreglo.llenarPC(getCant());
        JSONArray json = arreglo.crearJson(0);
        JSONArray jsonPC = arreglo.crearJson(1);
        Random rnd = new Random();
        if (ABB == true)
            setTABB(json, jsonPC, 1, (int) json.get(rnd.nextInt(json.size() - 1)),
                    (int) jsonPC.get(rnd.nextInt(jsonPC.size() - 1)));
        if (ASP == true) {
            setTASP(json, jsonPC, 3, (int) json.get(rnd.nextInt(json.size() - 1)),
                    (int) jsonPC.get(rnd.nextInt(jsonPC.size() - 1)));
        }
        if (ARN == true) {
            setTARN(json, jsonPC, 4, (int) json.get(rnd.nextInt(json.size() - 1)),
                    (int) jsonPC.get(rnd.nextInt(jsonPC.size() - 1)));
        }
        if (AVL == true) {
            setTAVL(json, jsonPC, 2, (int) json.get(rnd.nextInt(json.size() - 1)),
                    (int) jsonPC.get(rnd.nextInt(jsonPC.size() - 1)));
        }
    }

    /**
     * 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() {

        jToggleButton1 = new javax.swing.JToggleButton();
        InText = new javax.swing.JTextField();
        Find = new javax.swing.JButton();
        Insert = new javax.swing.JButton();
        Delete = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        BtnAVL = new javax.swing.JButton();
        BtnRN = new javax.swing.JButton();
        BtnBin = new javax.swing.JButton();
        BtnSplay = new javax.swing.JButton();
        Btnclear = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        RecurText = new javax.swing.JTextField();
        PanChart = new javax.swing.JPanel();
        PanChart1 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        MenOP = new javax.swing.JMenu();
        Out = new javax.swing.JMenuItem();
        MenAyuda = new javax.swing.JMenu();
        tuto = new javax.swing.JMenuItem();

        jToggleButton1.setText("jToggleButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));
        setBounds(new java.awt.Rectangle(0, 22, 716, 530));
        setMinimumSize(new java.awt.Dimension(1000, 670));
        getContentPane().setLayout(null);
        getContentPane().add(InText);
        InText.setBounds(350, 50, 200, 28);

        Find.setText("Op. Buscar");
        Find.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindActionPerformed(evt);
            }
        });
        getContentPane().add(Find);
        Find.setBounds(580, 30, 97, 29);

        Insert.setText("Op. Insertar");
        Insert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertActionPerformed(evt);
            }
        });
        getContentPane().add(Insert);
        Insert.setBounds(700, 30, 110, 29);

        Delete.setText("Op. Borrado");
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });
        getContentPane().add(Delete);
        Delete.setBounds(580, 70, 110, 29);

        jLabel1.setText("Cantidad de datos a analizar");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(350, 20, 190, 20);

        jLabel2.setText("Tipos de Arboles");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(80, 30, 110, 16);

        BtnAVL.setText("AVL");
        BtnAVL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAVLActionPerformed(evt);
            }
        });
        getContentPane().add(BtnAVL);
        BtnAVL.setBounds(190, 100, 75, 29);

        BtnRN.setText("Rojo-Negros");
        BtnRN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnRNActionPerformed(evt);
            }
        });
        getContentPane().add(BtnRN);
        BtnRN.setBounds(70, 100, 110, 29);

        BtnBin.setText("Binario");
        BtnBin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBinActionPerformed(evt);
            }
        });
        getContentPane().add(BtnBin);
        BtnBin.setBounds(70, 60, 87, 29);

        BtnSplay.setText("Splay");
        BtnSplay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnSplayActionPerformed(evt);
            }
        });
        getContentPane().add(BtnSplay);
        BtnSplay.setBounds(190, 60, 77, 29);

        Btnclear.setText("Limpiar memoria");
        Btnclear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnclearActionPerformed(evt);
            }
        });
        getContentPane().add(Btnclear);
        Btnclear.setBounds(700, 70, 130, 29);

        jLabel3.setText("Cantidad de recursiones");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(350, 90, 180, 16);
        getContentPane().add(RecurText);
        RecurText.setBounds(350, 110, 200, 28);

        PanChart.setBackground(new java.awt.Color(255, 255, 153));
        PanChart.setLayout(new java.awt.BorderLayout());
        getContentPane().add(PanChart);
        PanChart.setBounds(40, 160, 420, 340);

        PanChart1.setBackground(new java.awt.Color(255, 255, 153));
        PanChart1.setLayout(new java.awt.BorderLayout());
        getContentPane().add(PanChart1);
        PanChart1.setBounds(520, 160, 420, 340);

        MenOP.setText("Opciones");

        Out.setText("Salir");
        Out.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OutActionPerformed(evt);
            }
        });
        MenOP.add(Out);

        jMenuBar1.add(MenOP);

        MenAyuda.setText("Ayuda");

        tuto.setText("Como usar");
        tuto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tutoActionPerformed(evt);
            }
        });
        MenAyuda.add(tuto);

        jMenuBar1.add(MenAyuda);

        setJMenuBar(jMenuBar1);

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

    private void OutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OutActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_OutActionPerformed

    private void tutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tutoActionPerformed
        // TODO add your handling code here:
        tut = new Tutorial();
        tut.setVisible(true);
    }//GEN-LAST:event_tutoActionPerformed

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
        // TODO add your handling code here:
        try {
            tipoTrabajo = 1;
            AddGraf();
            JFreeChart grafica = ChartFactory.createBarChart("Redimiento en borrado de elementos", "promedio",
                    "Tiempo en milisegundos", cuadro, PlotOrientation.VERTICAL, true, true, false);
            CategoryPlot grfic = grafica.getCategoryPlot();
            ChartPanel barras = new ChartPanel(grafica);
            JFreeChart grafica1 = ChartFactory.createBarChart("Redimiento en borrado de elementos",
                    "peor de los casos", "Tiempo en milisegundos", cuadro, PlotOrientation.VERTICAL, true, true,
                    false);
            CategoryPlot grfic1 = grafica1.getCategoryPlot();
            ChartPanel barras1 = new ChartPanel(grafica1);
            PanChart.removeAll();
            PanChart.add(barras, BorderLayout.CENTER);
            PanChart.validate();
            PanChart1.removeAll();
            PanChart1.add(barras1, BorderLayout.CENTER);
            PanChart1.validate();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Error: los espacios de cantidad de datos y recursiones no deben de estar vacios");
        }
    }//GEN-LAST:event_DeleteActionPerformed

    private void InsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertActionPerformed
        // TODO add your handling code here:
        try {
            tipoTrabajo = 0;
            AddGraf();
            JFreeChart grafica = ChartFactory.createBarChart("Redimiento en insercin de elementos", "promedio",
                    "Tiempo en milisegundos", cuadro, PlotOrientation.VERTICAL, true, true, false);
            CategoryPlot grfic = grafica.getCategoryPlot();
            ChartPanel barras = new ChartPanel(grafica);
            JFreeChart grafica1 = ChartFactory.createBarChart("Redimiento en insercin de elementos",
                    "peor de los casos", "Tiempo en milisegundos", cuadroPC, PlotOrientation.VERTICAL, true, true,
                    false);
            CategoryPlot grfic1 = grafica1.getCategoryPlot();
            ChartPanel barras1 = new ChartPanel(grafica1);
            PanChart.removeAll();
            PanChart.add(barras, BorderLayout.CENTER);
            PanChart.validate();
            PanChart1.removeAll();
            PanChart1.add(barras1, BorderLayout.CENTER);
            PanChart1.validate();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Error: los espacios de cantidad de datos y recursiones no deben de estar vacios");
        }
    }//GEN-LAST:event_InsertActionPerformed

    private void FindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindActionPerformed
        // TODO add your handling code here:
        try {
            tipoTrabajo = 2;
            AddGraf();
            JFreeChart grafica = ChartFactory.createBarChart("Redimiento en busqueda de elementos", "promedio",
                    "Tiempo en milisegundos", cuadro, PlotOrientation.VERTICAL, true, true, false);
            CategoryPlot grfic = grafica.getCategoryPlot();
            ChartPanel barras = new ChartPanel(grafica);
            JFreeChart grafica1 = ChartFactory.createBarChart("Redimiento en busqueda de elementos",
                    "peor de los casos", "Tiempo en milisegundos", cuadroPC, PlotOrientation.VERTICAL, true, true,
                    false);
            CategoryPlot grfic1 = grafica1.getCategoryPlot();
            ChartPanel barras1 = new ChartPanel(grafica1);
            PanChart.removeAll();
            PanChart.add(barras, BorderLayout.CENTER);
            PanChart.validate();
            PanChart1.removeAll();
            PanChart1.add(barras1, BorderLayout.CENTER);
            PanChart1.validate();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Error: los espacios de cantidad de datos y recursiones no deben de estar vacios");
        }
    }//GEN-LAST:event_FindActionPerformed

    private void BtnBinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBinActionPerformed
        // TODO add your handling code here:
        ABB = true;
        System.out.println("Btnbinario");
    }//GEN-LAST:event_BtnBinActionPerformed

    private void BtnSplayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSplayActionPerformed
        // TODO add your handling code here:
        ASP = true;
        System.out.println("BtnSplay");
    }//GEN-LAST:event_BtnSplayActionPerformed

    private void BtnRNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnRNActionPerformed
        // TODO add your handling code here:
        AVL = true;
        System.out.println("BtnRojo-Negro");
    }//GEN-LAST:event_BtnRNActionPerformed

    private void BtnAVLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAVLActionPerformed
        // TODO add your handling code here:
        ARN = true;
        System.out.println("BtnAvl");
    }//GEN-LAST:event_BtnAVLActionPerformed

    private void BtnclearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnclearActionPerformed
        // TODO add your handling code here:
        ABB = false;
        ASP = false;
        AVL = false;
        ARN = false;
        cuadro.clear();
        cuadroPC.clear();
    }//GEN-LAST:event_BtnclearActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Pantalla().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAVL;
    private javax.swing.JButton BtnBin;
    private javax.swing.JButton BtnRN;
    private javax.swing.JButton BtnSplay;
    private javax.swing.JButton Btnclear;
    private javax.swing.JButton Delete;
    private javax.swing.JButton Find;
    private javax.swing.JTextField InText;
    private javax.swing.JButton Insert;
    private javax.swing.JMenu MenAyuda;
    private javax.swing.JMenu MenOP;
    private javax.swing.JMenuItem Out;
    private javax.swing.JPanel PanChart;
    private javax.swing.JPanel PanChart1;
    private javax.swing.JTextField RecurText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JMenuItem tuto;
    // End of variables declaration//GEN-END:variables
}