metodosnumericos.VentanaBiseccion.java Source code

Java tutorial

Introduction

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

import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jfree.chart.ChartPanel;
import javax.swing.*;

/**
 *
 * @author Juan Fernando Zuluaga <jzulua50@eafit.edu.co>
 */
public class VentanaBiseccion extends javax.swing.JFrame {

    /**
     * Creates new form VentanaBiseccion
     */
    public VentanaBiseccion() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setTitle("Biseccion");
    }

    /**
     * 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();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtXi = new javax.swing.JTextField();
        txtXs = new javax.swing.JTextField();
        txtTolerancia = new javax.swing.JTextField();
        txtIteraciones = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        butCalcular = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        panelGrafica = new javax.swing.JPanel();
        canvas1 = new java.awt.Canvas();
        panelTabla = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Xi");

        jLabel3.setText("Xs:");

        jLabel4.setText("Tolerancia:");

        jLabel5.setText("Iteraciones:");

        txtXi.setText("Xi");
        txtXi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtXiActionPerformed(evt);
            }
        });

        txtXs.setText("Xs");
        txtXs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtXsActionPerformed(evt);
            }
        });

        txtTolerancia.setText("Tolerancia");

        txtIteraciones.setText("Iteraciones");

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

        butCalcular.setText("Calcular");
        butCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butCalcularActionPerformed(evt);
            }
        });

        panelGrafica.add(canvas1);

        jTabbedPane1.addTab("Grafica", panelGrafica);

        panelTabla.setLayout(new java.awt.BorderLayout());
        jTabbedPane1.addTab("Tabla", panelTabla);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/info.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(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().addGap(33, 33, 33).addComponent(jButton1)
                                .addGap(60, 60, 60).addComponent(butCalcular))
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel3).addComponent(jLabel2).addComponent(jLabel4)
                                        .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtTolerancia, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtXs, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtXi, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtIteraciones, javax.swing.GroupLayout.DEFAULT_SIZE, 90,
                                                Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jButton2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 391,
                                javax.swing.GroupLayout.PREFERRED_SIZE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup().addComponent(jButton2).addGap(23, 23, 23)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel2).addComponent(txtXi,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel3)
                                                .addComponent(txtXs, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel4).addComponent(txtTolerancia,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel5).addComponent(txtIteraciones,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jButton1).addComponent(butCalcular)))
                                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap()));

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

    String funcion;

    public void RecibirFuncion(String f) {
        funcion = f;
    }

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

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

    private void butCalcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butCalcularActionPerformed

        if (txtIteraciones.getText().isEmpty() || txtTolerancia.getText().isEmpty() || txtXi.getText().isEmpty()
                || txtXs.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Verifique que los campos no esten vacios", "Mensaje",
                    JOptionPane.INFORMATION_MESSAGE);

        } else {
            try {
                Graficador t = new Graficador();
                double tolerancia = Double.parseDouble(txtTolerancia.getText());
                int iteraciones = Integer.parseInt(txtIteraciones.getText());
                double xi = Double.parseDouble(txtXi.getText());
                double xs = Double.parseDouble(txtXs.getText());

                ChartPanel panel = t.series(funcion, xi, xs);
                panelGrafica.removeAll();
                panelGrafica.add(panel);
                panelGrafica.updateUI();

                Metodos m = new Metodos();

                JOptionPane.showMessageDialog(null, m.Biseccion(xs, xi, tolerancia, iteraciones, funcion, true),
                        "Resultado", JOptionPane.INFORMATION_MESSAGE);
                GeneradorTablas g = new GeneradorTablas();
                JTable tabla = g.tablaBiseccion(m.getBiseccionXi(), m.getBiseccionXs(), m.getBiseccionXm(),
                        m.getBiseccionFxm(), m.getBiseccionEa(), m.getBiseccionEr());

                panelTabla.removeAll();
                panelTabla.add(new JScrollPane(tabla));
                panelTabla.updateUI();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Verifique que los campos sean Numeros", "Mensaje",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_butCalcularActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        VentanaUnaVariable vuv = new VentanaUnaVariable();
        this.setVisible(false);
        vuv.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JOptionPane.showMessageDialog(this,
                "El objetivo de este mtodo es converger a una raz en un intervalo [xi,xs].\n"
                        + "Esto lo logra al pararse en la mitad del intervalo y encontrar en cul de los intervalos [xi,xm] o [xm,xs] \n"
                        + "se encuentra la raz ((f(xs) * f(m) < 0) y (f(xi) * f(m) < 0)). Al detectar una raz en [xi,xm] o [xm,xs] \n"
                        + "se reemplaza xs o xi con xm, dependiendo de donde se encuentre la raz. Luego de obtener el nuevo intervalo \n"
                        + "[xi,xs] se repite el proceso hasta que llegue a una raz, se llegue a la tolerancia buscada o falle en encontrar una raz.");
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @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(VentanaBiseccion.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentanaBiseccion.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentanaBiseccion.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentanaBiseccion.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 VentanaBiseccion().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butCalcular;
    private java.awt.Canvas canvas1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel panelGrafica;
    private javax.swing.JPanel panelTabla;
    private javax.swing.JTextField txtIteraciones;
    private javax.swing.JTextField txtTolerancia;
    private javax.swing.JTextField txtXi;
    private javax.swing.JTextField txtXs;
    // End of variables declaration//GEN-END:variables
}