api.window.Histogram.java Source code

Java tutorial

Introduction

Here is the source code for api.window.Histogram.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 api.window;

import api.utils.BufferedImageHelper;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Aga
 */
public class Histogram extends javax.swing.JFrame {

    XYPlot xyplot;
    XYItemRenderer renderer;
    boolean autoUpdate = false;
    ChartPanel chartpanel;
    BufferedImage image;

    /**
     * Creates new form Histogram
     */
    public Histogram(BufferedImage image) {
        initComponents();
        this.image = image;
        MakeChart();
    }

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

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

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

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

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

        jButton4.setText("I");
        jButton4.setToolTipText("");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 69,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 61,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18).addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(56, 56, 56)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1).addComponent(jButton2).addComponent(jButton3)
                        .addComponent(jButton4)));

        jPanel2.setPreferredSize(new java.awt.Dimension(0, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 250, Short.MAX_VALUE));

        jPanel2.setLayout(new java.awt.BorderLayout());

        jMenu1.setText("File");

        jMenuItem1.setText("Save");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)));

        add(jPanel2, java.awt.BorderLayout.CENTER);

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        renderer.setSeriesVisible(0, !renderer.getSeriesVisible(0));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        renderer.setSeriesVisible(1, !renderer.getSeriesVisible(1));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        renderer.setSeriesVisible(2, !renderer.getSeriesVisible(2));
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        renderer.setSeriesVisible(3, !renderer.getSeriesVisible(3));
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void MakeChart() {
        JFreeChart chart;
        if (this.image.getType() != BufferedImage.TYPE_BYTE_BINARY) {
            XYDataset xydataset = createDataset();
            chart = createChart(xydataset);
        } else {
            this.remove(this.jPanel1);
            DefaultCategoryDataset dataset = createbinaryDataset();
            chart = createChart(dataset);
        }

        ChartPanel chartpanelTmp = new ChartPanel(chart);
        chartpanelTmp.setPreferredSize(new Dimension(200, 200));
        if (this.chartpanel != null && this.jPanel2.getComponents().length > 0) {
            this.jPanel2.remove(this.chartpanel);
        }
        this.chartpanel = chartpanelTmp;
        this.jPanel2.add(this.chartpanel);
        this.jPanel2.revalidate();
    }

    private static final Logger LOG = Logger.getLogger(Histogram.class.getName());

    private DefaultCategoryDataset createbinaryDataset() {
        int max = this.image.getSampleModel().getNumBands();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int y = 0; y < max; y++) {
            double[] values = BufferedImageHelper.getHistogram(this.image, y);
            for (int x = 0; x < values.length; x++) {
                dataset.setValue(values[x], "", Integer.toString(x));
            }
        }

        return dataset;
    }

    private XYDataset createDataset() {
        int max = this.image.getSampleModel().getNumBands();
        XYSeriesCollection seriescollection = new XYSeriesCollection();
        for (int y = 0; y < max; y++) {
            XYSeries series = new XYSeries(y);
            double[] values = BufferedImageHelper.getHistogram(this.image, y);
            for (int x = 0; x < values.length; x++) {
                series.add(x, values[x]);
            }
            seriescollection.addSeries(series);
        }
        if (max == 3) {
            XYSeries series = new XYSeries(3);
            double[] values = BufferedImageHelper.getLuminanceHistogram(this.image);
            for (int x = 0; x < values.length; x++) {
                series.add(x, values[x]);
            }
            seriescollection.addSeries(series);
        }
        return seriescollection;
    }

    private JFreeChart createChart(DefaultCategoryDataset dataset) {
        JFreeChart jfreechart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.VERTICAL,
                false, false, false);
        final CategoryPlot plot = jfreechart.getCategoryPlot();
        BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
        return jfreechart;
    }

    private JFreeChart createChart(XYDataset xydataset) {
        JFreeChart jfreechart = ChartFactory.createXYAreaChart(null, null, null, xydataset,
                PlotOrientation.VERTICAL, false, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setForegroundAlpha(0.65F);
        xyplot.setDomainGridlinePaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.white);
        ValueAxis valueaxis = xyplot.getDomainAxis();
        valueaxis.setTickMarkPaint(Color.black);
        valueaxis.setLowerMargin(0.0D);
        valueaxis.setUpperMargin(0.0D);
        renderer = xyplot.getRenderer();
        if (xydataset.getSeriesCount() == 4) {
            renderer.setSeriesPaint(0, Color.red);
            renderer.setSeriesPaint(1, Color.green);
            renderer.setSeriesPaint(2, Color.blue);
            renderer.setSeriesVisible(0, true);
            renderer.setSeriesVisible(1, true);
            renderer.setSeriesVisible(2, true);
            renderer.setSeriesPaint(3, Color.BLACK);
            renderer.setSeriesVisible(3, true);
        } else {
            this.remove(this.jPanel1);
            renderer.setSeriesPaint(0, Color.BLACK);
            renderer.setSeriesVisible(0, true);
        }
        ValueAxis valueaxis1 = xyplot.getRangeAxis();
        valueaxis1.setTickMarkPaint(Color.black);
        return jfreechart;
    }

    // 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.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}