Result3.java Source code

Java tutorial

Introduction

Here is the source code for Result3.java

Source

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/*
 * 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.
 */

/**
 *
 * @author czarinarae
 */
public class Result3 extends javax.swing.JFrame {

    AnsLab1_201340347 ans;
    String[] input;
    int[] a;
    int[] c;
    int[] g;
    int[] t;

    /**
     * Creates new form Result3
     */

    public Result3(String[] s, AnsLab1_201340347 ans) {
        initComponents();
        input = s;
        this.ans = ans;
        input = removeSpaces(input);
        getFrequencies();
        this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    public String[] removeSpaces(String[] s) {
        for (int i = 0; i < s.length; i++) {
            s[i] = s[i].replaceAll(" ", "");
        }
        return s;
    }

    public void getFrequencies() {
        int max = 0;
        for (int i = 0; i < input.length; i++) {
            if (max < input[i].length()) {
                max = input[i].length();
            }
            input[i] = input[i].toUpperCase();
        }
        max -= 1;
        a = new int[max];
        c = new int[max];
        g = new int[max];
        t = new int[max];
        for (int i = 0; i < max; i++) { // position
            for (int j = 0; j < input.length; j++) { // get char at i from input j
                if (i < input[j].length()) {
                    switch (input[j].charAt(i)) {
                    case 'A':
                        a[i]++;
                        break;
                    case 'C':
                        c[i]++;
                        break;
                    case 'G':
                        g[i]++;
                        break;
                    case 'T':
                        t[i]++;
                        break;
                    }
                }
            }
        }
        createGraphs();
    }

    public void createImage() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("Documents"));
        int retrival = chooser.showSaveDialog(null);
        if (retrival == JFileChooser.APPROVE_OPTION) {
            try {
                ImageIO.write(bImage1, "png", new File(chooser.getSelectedFile() + ".png"));
            } catch (IOException ex) {
                Logger.getLogger(Result1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void createImageJPG() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("Documents"));
        int retrival = chooser.showSaveDialog(null);
        if (retrival == JFileChooser.APPROVE_OPTION) {
            try {
                ImageIO.write(bImage1, "jpg", new File(chooser.getSelectedFile() + ".jpg"));
            } catch (IOException ex) {
                Logger.getLogger(Result1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    BufferedImage bImage1 = new BufferedImage(1, 1, 1);

    public void createGraphs() {
        XYSeries a_data = new XYSeries("A");
        XYSeries c_data = new XYSeries("C");
        XYSeries g_data = new XYSeries("G");
        XYSeries t_data = new XYSeries("T");
        for (int i = 0; i < a.length; i++) {
            a_data.add(i, a[i]);
            c_data.add(i, c[i]);
            g_data.add(i, g[i]);
            t_data.add(i, t[i]);
        }

        XYSeriesCollection my_data_series = new XYSeriesCollection();
        my_data_series.addSeries(a_data);
        my_data_series.addSeries(c_data);
        my_data_series.addSeries(g_data);
        my_data_series.addSeries(t_data);

        JFreeChart XYLineChart = ChartFactory.createXYLineChart("Frequency Table", "Position", "No. of Occurences",
                my_data_series, PlotOrientation.VERTICAL, true, true, false);
        bImage1 = (BufferedImage) XYLineChart.createBufferedImage(690, 337);
        ImageIcon imageIcon1 = new ImageIcon(bImage1);
        jLabel1.setIcon(imageIcon1);

    }

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

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        jButton2.setText("New Input");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Save as PNG");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Save as JPG");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(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().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 691, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addComponent(jButton3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jButton2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton1)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 338,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton1).addComponent(jButton2).addComponent(jButton3)
                                .addComponent(jButton4))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        createImage();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ans.removeText();
        ans.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

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

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        createImageJPG();
    }//GEN-LAST:event_jButton4ActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}