javaapplication1.Graph.java Source code

Java tutorial

Introduction

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

import org.jfree.ui.RefineryUtilities;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Sagar Bhatt
 */
public class Graph extends javax.swing.JFrame {

    /**
     * Creates new form Graph
     */
    int totalElement, count = 0, genValue;
    public static int values[];

    public Graph() {
        initComponents();
        btnSubmit.setEnabled(false);
    }

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

        txtElement = new javax.swing.JTextField();
        btnElement = new javax.swing.JButton();
        lblElement = new javax.swing.JLabel();
        lblValue = new javax.swing.JLabel();
        btnSubmit = new javax.swing.JButton();
        lblGen = new javax.swing.JLabel();
        txtGenValue = new javax.swing.JTextField();
        txtElementValue = new javax.swing.JTextField();
        lblCounter = new javax.swing.JLabel();

        setTitle("Data");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                formFocusLost(evt);
            }
        });

        txtElement.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtElementFocusLost(evt);
            }
        });

        btnElement.setText("OK");
        btnElement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnElementActionPerformed(evt);
            }
        });

        lblElement.setText("Enter how many seed values that you want to enter?");

        lblValue.setText("Enter seed values.");

        btnSubmit.setText("Genrate");
        btnSubmit.setName("btnSubmit"); // NOI18N
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        lblGen.setText("Enter how many element that you want to generate?");

        txtGenValue.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtGenValueFocusLost(evt);
            }
        });

        lblCounter.setText("0");

        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(22, 22, 22)
                                        .addComponent(lblElement, javax.swing.GroupLayout.PREFERRED_SIZE, 304,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(38, 38, 38).addComponent(txtElement,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 232,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGap(22, 22, 22)
                                        .addComponent(lblGen, javax.swing.GroupLayout.PREFERRED_SIZE, 304,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(38, 38, 38).addComponent(txtGenValue,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 232,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGap(22, 22, 22)
                                        .addComponent(lblValue, javax.swing.GroupLayout.PREFERRED_SIZE, 304,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(38, 38, 38)
                                        .addComponent(txtElementValue, javax.swing.GroupLayout.PREFERRED_SIZE, 167,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(10, 10, 10).addComponent(lblCounter,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 55,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGap(364, 364, 364).addComponent(
                                        btnElement, javax.swing.GroupLayout.PREFERRED_SIZE, 232,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup().addGap(364, 364, 364).addComponent(
                                        btnSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 232,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(45, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addGap(3, 3, 3).addComponent(lblElement))
                                .addComponent(txtElement, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addGap(3, 3, 3).addComponent(lblGen))
                                .addComponent(txtGenValue, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtElementValue, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup().addGap(3, 3, 3)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblValue).addComponent(lblCounter))))
                        .addGap(18, 18, 18).addComponent(btnElement).addGap(11, 11, 11).addComponent(btnSubmit)
                        .addContainerGap(18, Short.MAX_VALUE)));

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

    private void btnElementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnElementActionPerformed
        // TODO add your handling code here:
        try {

            int temp = Integer.parseInt(txtElementValue.getText());
            if (count != totalElement) {
                values[count] = temp;
                count++;
                lblCounter.setText(String.valueOf(totalElement - count));
                txtElementValue.setText("");

            } else {
                txtElementValue.setText("You cant enter more elements");
                txtElementValue.enable(false);
                btnSubmit.setEnabled(true);
            }

            if ((totalElement - count) == 0) {
                btnSubmit.setEnabled(true);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_btnElementActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        // TODO add your handling code here:
        try {
            storeInitial();
            drawchart();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }//GEN-LAST:event_btnSubmitActionPerformed

    private void txtElementFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtElementFocusLost
        // TODO add your handling code here:
        try {
            totalElement = Integer.parseInt(txtElement.getText());
            lblCounter.setText(String.valueOf(totalElement));
        } catch (Exception e) {
            totalElement = 0;
        }

    }//GEN-LAST:event_txtElementFocusLost

    private void txtGenValueFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtGenValueFocusLost
        // TODO add your handling code here:
        try {
            genValue = Integer.parseInt(txtGenValue.getText());
            values = new int[genValue];
        } catch (Exception e) {
            genValue = 0;
        }

    }//GEN-LAST:event_txtGenValueFocusLost

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        // TODO add your handling code here:

    }//GEN-LAST:event_formFocusGained

    private void formFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusLost
        // TODO add your handling code here:

    }//GEN-LAST:event_formFocusLost

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        // this .show(true);
    }//GEN-LAST:event_formWindowClosing

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

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnElement;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel lblCounter;
    private javax.swing.JLabel lblElement;
    private javax.swing.JLabel lblGen;
    private javax.swing.JLabel lblValue;
    private javax.swing.JTextField txtElement;
    private javax.swing.JTextField txtElementValue;
    private javax.swing.JTextField txtGenValue;
    // End of variables declaration//GEN-END:variables

    void storeInitial() {
        try {
            // int limit=count;
            boolean stop = true;
            int j = 1;
            int limit = count;
            //int loop=totalElement-1;
            do {
                int flag = j;
                for (int i = j; i < limit; i++) {
                    if (count != genValue) {
                        values[count] = values[flag];
                        System.out.println(values[count]);
                        count++;
                        flag++;
                    } else {
                        stop = false;
                        break;
                    }
                }
                j++;
                limit = count;
                //loop++;
            } while (stop);
        } catch (Exception e) {
        }
        export();
        //fout=new FileWriter("data.xls");
        /*  try (
               PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("data.xls", true)))) {
              //fout=new FileWriter("data.xls");
              fout.write("\n");
              for(int k=0;k<values.length;k++){
                  //fout.write();
                      
                  fout.write(String.valueOf(values[k]));
                  fout.write("\t");
              }   
          }
        catch(IOException e){
        System.out.println("Error in  opening file ");
        System.out.println(e.getMessage());
        }*/

    }

    void drawchart() {
        Linechart chart;
        chart = new Linechart("Vibrations", "Total Samples");
        chart.pack();
        RefineryUtilities.centerFrameOnScreen(chart);

        chart.setVisible(true);

    }

    void export() {

        XSSFWorkbook workbook = new XSSFWorkbook();
        //Create a blank sheet
        XSSFSheet sheet = workbook.createSheet("data");
        //This data needs to be written (Object[])
        Map<String, Object[]> data = new TreeMap<String, Object[]>();
        for (int i = 1; i <= values.length; i++)
            data.put("1", new Object[] { values });
        //Iterate over data and write to sheet
        int rownum = 0;
        Row row = sheet.createRow(rownum++);

        int cellnum = 0;

        for (int i : values) {
            Cell cell = row.createCell(cellnum++);
            cell.setCellValue((Integer) i);
        }

        try {
            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File("data.xlsx"));
            workbook.write(out);
            out.close();

            System.out.println("data.xlsx written successfully on disk.");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    void countmax() {

    }
}