Wednesday, 10 February 2016

Java GUI - Love Calculator Application


This is a java GUI program which calculates the love percentage between two people.Here , we used java SWING to implement this desktop application .
The logic is it converts the letters in the name to its ASCII equivalent and then sums up all the letters and perform a modulus operation with 100 to get the result.



Example  :  If 2 names are Kete and Rade
                    ASCII value of K   =     75
                    ASCII value of E    =     69
                    ASCII value of T    =     84
                    ASCII value of E    =     69
                    ASCII value of R    =     72
                    ASCII value of A    =     65
                    ASCII value of D    =     68
                    ASCII value of E    =     69

                    Sum of  ASCII values of K+E+T+E + R + A + D + E =     581
                    now 581 % 100 = 81

                  So, the love % between Kete and  Rade= 81%
Java GUI - Love Calculator



Initially in we use 2 lables for name and crushname and two testfields for entering names , a button for calculating and a label to display result.
We design the layout using drag and drop java swing palette.

When we click on calculate after entering names , first we check whether only alphabets are entered
String n = nameField.getText().toUpperCase();
        String cn = crushnameField.getText().toUpperCase();
        if (!(Pattern.matches("^[a-zA-Z]+$", n)) || !(Pattern.matches("^[a-zA-Z]+$", cn))) {
            JOptionPane.showMessageDialog(null, "Please enter alphabets", "Error", JOptionPane.ERROR_MESSAGE);
            nameField.setText("");
            crushnameField.setText("");
            resLabel.setText("");
        }

After that we will concact two names and convert the letters to their corresponding ASCII values , sum those values and perform a modulus operation with 100 to get the result.

String concat = n.concat(cn);
            int sum = 0;
            for (int i = 0; i < concat.length(); i++) {
                char character = concat.charAt(i);
                int ascii = (int) character;
                sum += ascii;
            }
            String result = "The love between " + n + " and " + cn + " is " + sum % 100 + " % ";
            String html1 = "<html><body style='width: ";
            String html2 = "px'>";
            resLabel.setText(html1 + "400" + html2 + result);
Download Application

COMPLETE PROGRAM : 

/*
 * 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 lovecalculatorgui;

import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author innovate
 */
public class LoveCalculator extends javax.swing.JFrame {

    /**
     * Creates new form LoveCalculator
     */
    public LoveCalculator() {

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

        nameLabel = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        crushnameLabel = new javax.swing.JLabel();
        crushnameField = new javax.swing.JTextField();
        resButton = new javax.swing.JButton();
        resLabel = new javax.swing.JLabel();
        bckLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Love Calculator");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        nameLabel.setFont(new java.awt.Font("Buxton Sketch", 1, 18)); // NOI18N
        nameLabel.setForeground(new java.awt.Color(255, 255, 255));
        nameLabel.setText("Enter your name");
        getContentPane().add(nameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 120, -1, -1));

        nameField.setFont(new java.awt.Font("Buxton Sketch", 1, 16)); // NOI18N
        nameField.setForeground(new java.awt.Color(204, 0, 51));
        getContentPane().add(nameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 120, 140, -1));

        crushnameLabel.setFont(new java.awt.Font("Buxton Sketch", 1, 18)); // NOI18N
        crushnameLabel.setForeground(new java.awt.Color(255, 255, 255));
        crushnameLabel.setText("Enter your crush name");
        getContentPane().add(crushnameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 180, -1, -1));

        crushnameField.setFont(new java.awt.Font("Buxton Sketch", 1, 14)); // NOI18N
        crushnameField.setForeground(new java.awt.Color(204, 0, 51));
        getContentPane().add(crushnameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 180, 140, -1));

        resButton.setBackground(new java.awt.Color(255, 255, 255));
        resButton.setFont(new java.awt.Font("Buxton Sketch", 1, 24)); // NOI18N
        resButton.setForeground(new java.awt.Color(204, 0, 51));
        resButton.setText("Check");
        resButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resButtonActionPerformed(evt);
            }
        });
        getContentPane().add(resButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 240, 130, -1));

        resLabel.setFont(new java.awt.Font("Buxton Sketch", 1, 24)); // NOI18N
        resLabel.setForeground(new java.awt.Color(255, 255, 255));
        resLabel.setAutoscrolls(true);
        resLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        getContentPane().add(resLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 300, 570, 100));

        bckLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lovecalculatorgui/bk.jpg"))); // NOI18N
        getContentPane().add(bckLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 600, 410));

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


    private void resButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String n = nameField.getText().toUpperCase();
        String cn = crushnameField.getText().toUpperCase();
        if (!(Pattern.matches("^[a-zA-Z]+$", n)) || !(Pattern.matches("^[a-zA-Z]+$", cn))) {
            JOptionPane.showMessageDialog(null, "Please enter alphabets", "Error", JOptionPane.ERROR_MESSAGE);
            nameField.setText("");
            crushnameField.setText("");
            resLabel.setText("");
        } else {
            String concat = n.concat(cn);
            int sum = 0;
            for (int i = 0; i < concat.length(); i++) {
                char character = concat.charAt(i);
                int ascii = (int) character;
                sum += ascii;
            }
            String result = "The love between " + n + " and " + cn + " is " + sum % 100 + " % ";
            String html1 = "<html><body style='width: ";
            String html2 = "px'>";
            resLabel.setText(html1 + "400" + html2 + result);
        }
    }                                         
    /**
     * @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(LoveCalculator.class.getName())
.
log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(LoveCalculator.class.getName())
.
log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(LoveCalculator.class.getName())
.
log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(LoveCalculator.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 LoveCalculator().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JLabel bckLabel; private javax.swing.JTextField crushnameField; private javax.swing.JLabel crushnameLabel; private javax.swing.JTextField nameField; private javax.swing.JLabel nameLabel; private javax.swing.JButton resButton; private javax.swing.JLabel resLabel; // End of variables declaration }

Download Application

OUTPUT :

Java GUI - Love Calculator