You are on page 1of 9

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template

*/

package imc;

import Arduino.Arduino;

import gnu.io.SerialPortEvent;

import gnu.io.SerialPortEventListener;

import java.util.concurrent.TimeUnit;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

/**

* @author danie

*/

public class imcgui extends javax.swing.JFrame {

Arduino Arduino=new Arduino();

SerialPortEventListener evento;

/**

* Creates new form imcgui

*/

public imcgui() {

this.evento = new SerialPortEventListener(){


@Override

public void serialEvent(SerialPortEvent spe) {

if(Arduino.MessageAvailable()==true){

System.out.println(Arduino.PrintMessage());

};

initComponents();

try{

Arduino.ArduinoRXTX("COM3", 2000, 9600, evento);

}catch(Exception ex){

/**

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

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

txtPeso = new javax.swing.JTextField();


txtEstatura = new javax.swing.JTextField();

jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

jLabel5 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Peso:");

jLabel2.setText("Estatura:");

txtEstatura.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtEstaturaActionPerformed(evt);

});

jLabel3.setText("Kg");

jLabel4.setText("Mts");

jButton1.setText("Calcular IMC");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

jLabel5.setText("Calculadora de IMC");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(104, 104, 104)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel5)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 126,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G, false)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(txtEstatura, javax.swing.GroupLayout.PREFERRED_SIZE, 79,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(43, 43, 43)

.addComponent(txtPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 79,


javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGap(40, 40, 40)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)

.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17,


javax.swing.GroupLayout.PREFERRED_SIZE))))

.addContainerGap(90, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel5)

.addGap(65, 65, 65)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(txtPeso, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel3))

.addGap(27, 27, 27)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(txtEstatura, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel4))

.addGap(40, 40, 40)

.addComponent(jButton1)

.addContainerGap(72, Short.MAX_VALUE))

);

pack();

}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String p = txtPeso.getText();

String e = txtEstatura.getText();

double peso = Double.parseDouble(p);

double est = Double.parseDouble(e);

double IMC = peso/(est*est);

JOptionPane.showMessageDialog(null, "Su IMC es: "+IMC);

if(IMC<=15){

JOptionPane.showMessageDialog(null, "Se encuentra en degadez severa");

try {

Arduino.SendData("1");

} catch (Exception ex) {

} if(IMC>15 && IMC<=18.4){

JOptionPane.showMessageDialog(null, "Se encuentra en degadez");

try {

Arduino.SendData("2");

} catch (Exception ex) {

} if(IMC>18.4 && IMC<=24.9){

JOptionPane.showMessageDialog(null, "Se encuentra en peso saludable");

try {

Arduino.SendData("3");

} catch (Exception ex) {

}
} if(IMC>24.9 && IMC<=29.9){

JOptionPane.showMessageDialog(null, "Se encuentra en sobrepeso");

try {

Arduino.SendData("2");

} catch (Exception ex) {

} if(IMC>=30){

JOptionPane.showMessageDialog(null, "Se encuentra en Obesidad");

try {

Arduino.SendData("1");

} catch (Exception ex) {

private void txtEstaturaActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

/**

* @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(imcgui.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(imcgui.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(imcgui.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(imcgui.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 imcgui().setVisible(true);
}

});

// Variables declaration - do not modify

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.JLabel jLabel5;

private javax.swing.JTextField txtEstatura;

private javax.swing.JTextField txtPeso;

// End of variables declaration

You might also like