Java tutorial
/* * 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 cmsc105_mp2; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; /** * * @author czarinarae */ public class Panels extends javax.swing.JPanel { BufferedImage bImage1; ImageIcon imageIcon; public Panels() { initComponents(); } /** * Creates new form Panels * @param d * @param window * @param threshold */ public Panels(Data d, float window, Double threshold) { initComponents(); createGraph(d, window, threshold); //this.setVisible(true); } /** * 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(); jButton1.setText("Save as PNG"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 698, Short.MAX_VALUE).addComponent(jButton1))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE) .addGap(18, 18, 18).addComponent(jButton1).addContainerGap())); }// </editor-fold>//GEN-END:initComponents public void createGraph(Data d, float window, Double threshold) { XYSeries data = new XYSeries("data"); double max = Double.MIN_VALUE; for (double num : d.plots) { if (max < num) max = num; } max += 1; ArrayList<XYSeries> xy = new ArrayList(); ArrayList<Integer> points = new ArrayList(); for (int j = (int) (window / 2); j < d.plots.length - (window / 2); j++) { data.add(j, d.plots[j]); //System.out.println(points.size()); if (d.plots[j] > threshold) { points.add(j); } else { if (points.size() >= window) { //System.out.println("MIN!"); XYSeries series = new XYSeries("trend"); for (int n : points) { series.add(n, max); } xy.add(series); } points = new ArrayList(); } } if (points.size() >= window) { XYSeries series = new XYSeries("trend"); for (int n : points) { series.add(n, max); } xy.add(series); } XYSeriesCollection my_data_series = new XYSeriesCollection(); my_data_series.addSeries(data); for (XYSeries x : xy) { my_data_series.addSeries(x); } XYSeries thresh = new XYSeries("threshold"); for (int j = 0; j < d.plots.length; j++) { thresh.add(j, threshold); } my_data_series.addSeries(thresh); //System.out.println(d.name); JFreeChart XYLineChart = ChartFactory.createXYLineChart(d.name, "Position", "Average", my_data_series, PlotOrientation.VERTICAL, true, true, false); bImage1 = (BufferedImage) XYLineChart.createBufferedImage(690, 337); imageIcon = new ImageIcon(bImage1); jLabel1.setIcon(imageIcon); jLabel1.revalidate(); jLabel1.repaint(); jButton1.setText("Save as PNG"); jButton1.repaint(); jButton1.revalidate(); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { 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) { System.out.println("Unable to Print!"); } } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables }