jasmine.imaging.core.util.Histogram.java Source code

Java tutorial

Introduction

Here is the source code for jasmine.imaging.core.util.Histogram.java

Source

package jasmine.imaging.core.util;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import jasmine.imaging.commons.StatisticsSolver;

import javax.swing.*;
import java.util.Hashtable;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 30-May-2007
 * @version 1.0
 */
public class Histogram extends JFrame {

    JLabel chart;
    JFreeChart myChart;

    public Histogram(Hashtable<Integer, Integer> count) {

        super("Histogram");

        DefaultCategoryDataset series = new DefaultCategoryDataset();

        StatisticsSolver solver = new StatisticsSolver(1000);

        for (int i = 0; i < 256; i++) {
            Integer value = count.get(i);
            if (value == null)
                value = 1;
            for (int j = 0; j < value; j++) {
                solver.addData(i);
            }
            series.addValue(value, "row1", String.valueOf(i));
        }

        setTitle("Histogram: v=" + solver.getStandardDeviation());

        chart = new JLabel();
        getContentPane().add(chart);

        setSize(320, 240);
        setVisible(true);

        myChart = ChartFactory.createLineChart(null, null, null, series, PlotOrientation.VERTICAL, false, false,
                false);

        //updateChart();

        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                if (myChart != null)
                    updateChart();
            }
        });

    }

    public void updateChart() {
        if (chart != null) {
            BufferedImage image = myChart.createBufferedImage(chart.getWidth(), chart.getHeight());
            chart.setIcon(new ImageIcon(image));
        }
    }

}