Underc0de

Programación General => Java => Mensaje iniciado por: rush en Julio 29, 2015, 04:05:35 PM

Título: Conversor de temperatura
Publicado por: rush en Julio 29, 2015, 04:05:35 PM
Bueno, me agarro un momento de oscio y me puse a hacer un conversor de temperatura asi que aqui les dejo el jar y el source, creo que se puede mejorar pero solo fue porque no tenia nada que hacer.

(http://i.imgur.com/Gl9LVR4.png)

Código (java) [Seleccionar]
/*
* 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 temperatura;

import javax.swing.ButtonGroup;

/**
*
* @author rush
*/
public class Ventana extends javax.swing.JFrame {

    /**
     * Creates new form Ventana
     */
    public Ventana() {
        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">                         
    private void initComponents() {

        grupo1 = new javax.swing.ButtonGroup();
        grupo2 = new javax.swing.ButtonGroup();
        jRadioButton1 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        grupo1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Celsius");

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

        grupo1.add(jRadioButton2);
        jRadioButton2.setText("Fahrenheit");

        grupo1.add(jRadioButton3);
        jRadioButton3.setText("Kelvin");

        jLabel1.setText("Temperatura");

        jLabel2.setText("Conversion");

        jTextField2.setEditable(false);

        grupo2.add(jRadioButton4);
        jRadioButton4.setText("Celsius");

        grupo2.add(jRadioButton5);
        jRadioButton5.setSelected(true);
        jRadioButton5.setText("Fahrenheit");

        grupo2.add(jRadioButton6);
        jRadioButton6.setText("Kelvin");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("De");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("A");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jRadioButton1)
                                    .addComponent(jRadioButton3)
                                    .addComponent(jRadioButton2))
                                .addGap(18, 18, 18))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(80, 80, 80)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jRadioButton6)
                            .addComponent(jRadioButton5)
                            .addComponent(jRadioButton4))
                        .addGap(50, 50, 50))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jRadioButton2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jRadioButton3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Conv conv = new Conv();
        float r;
        //Celicus a celcius
        if(jRadioButton1.isSelected() && jRadioButton4.isSelected()){
            jTextField2.setText(jTextField1.getText());
        }
        //Celicus a Fahrenheit
        else if(jRadioButton1.isSelected() && jRadioButton5.isSelected()){
            r = conv.CaF(Float.parseFloat(jTextField1.getText()));
            jTextField2.setText(Float.toString(r));
        }
        //Celcius a Kelvin
        else if(jRadioButton1.isSelected() && jRadioButton6.isSelected()){
            r = conv.CaK(Float.parseFloat(jTextField1.getText()));
            jTextField2.setText(Float.toString(r));
        }
        //Fahrenheit a Celcius
        else if(jRadioButton2.isSelected() && jRadioButton4.isSelected()){
            r = conv.FaC(Float.parseFloat(jTextField1.getText()));
            jTextField2.setText(Float.toString(r));
        }
        //Fahrenheit a Fahrenheit
        else if(jRadioButton2.isSelected() && jRadioButton5.isSelected()){
            jTextField2.setText(jTextField1.getText());
        }
        //Fahrenheit a Kelvin
        else if(jRadioButton2.isSelected() && jRadioButton6.isSelected()){
            r = conv.FaK(Float.parseFloat(jTextField1.getText()));
            jTextField2.setText(Float.toString(r));
        }
        //Kelvin a Celicus
        else if(jRadioButton3.isSelected() && jRadioButton4.isSelected()){
            r = conv.KaC(Float.parseFloat(jTextField1.getText()));
            jTextField2.setText(Float.toString(r));
        }
        //Kelvin a Fahrenheit
        else if(jRadioButton3.isSelected() && jRadioButton5.isSelected()){
            r = conv.KaF(Float.parseFloat(jTextField1.getText()));
            jTextField2.setText(Float.toString(r));
        }
        //Kelvin a Kelvin
        else if(jRadioButton3.isSelected() && jRadioButton6.isSelected()){
            jTextField2.setText(jTextField1.getText());
        }
    }                                       

    /**
     * @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(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Ventana.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 Ventana().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup grupo1;
    private javax.swing.ButtonGroup grupo2;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   
}


Código (java) [Seleccionar]
/*
* 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 temperatura;

/**
*
* @author rush
*/
public class Conv {
    float FaC(float temp){
        float c=0;
        c = (5*(temp - 32))/9;
        return c;
    }
   
    float CaF(float temp){
        float f=0;
        f = ((9*temp)/5)+32;
        return f;
    }
   
    float CaK(float temp){
        float k=0;
        k = temp + 273.15f;
        return k;
    }
   
    float FaK(float temp){
        float k=0;
        k = ((5*(temp - 32))/9)+273.15f;
        return k;
    }
   
    float KaC(float temp){
        float k=0;
        k = temp - 273.15f;
        return k;
    }
   
    float KaF(float temp){
        float k=0;
        k = ((9*(temp - 273.15f))/5)+32;
        return k;
    }
}


Temperatura.jar (https://mega.co.nz/#!XVkiGBKL!XiwyPVm_79VJRN-RnZf8sB2dxtzw0PF8ycmX7ZQCsTw)
Título: Re:Conversor de temperatura
Publicado por: Gus Garsaky en Julio 29, 2015, 05:10:14 PM
Buen aporte, rush.