2

So below is my code I have created the histogram chart using JFree I wanted to show the out put on Button click

this is the histogram of normalized image

i wanted to do that the on botton click the jfreechart is open on the panel and save in Drive

please help me out I am newbie

code is below..........

package org.ivb.jfreechart.barchart;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Histogram extends ApplicationFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Histogram(final String title) throws IOException {
        super(title);
        IntervalXYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private IntervalXYDataset createDataset() throws IOException {
        BufferedImage imageA = ImageIO.read(new File("D://oriented.jpg"));
        int[] red = new int[imageA.getHeight() * imageA.getWidth()];
        int[] redhisto = new int[256];
        int[] pixel;
        int k = 0;
        for (int y = 0; y < imageA.getHeight(); y++) {
            for (int x = 0; x < imageA.getWidth(); x++) {
                pixel = imageA.getRaster().getPixel(x, y, new int[3]);
                red[k] = pixel[0];
                k++;
            }
        }

        for (int x = 0; x < red.length; x++) {
            int y = red[x];
            redhisto[y]++;
        }

        final XYSeries series = new XYSeries("No of pixels");
        for (int i = 0; i < redhisto.length; i++)
            series.add(i, redhisto[i]);

        final XYSeriesCollection dataset = new XYSeriesCollection(series);
        return dataset;
    }

    private JFreeChart createChart(IntervalXYDataset dataset) {
        final JFreeChart chart = ChartFactory.createXYBarChart(
                "Histogram of oriented Image", "X", false, "Y", dataset,
                PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        saveChart(chart);
        return chart;

    }

    public void saveChart(JFreeChart chart) {
        String fileName = "D:/histogram.jpg";

        try {
            ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 800, 600);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("creating erroe during chart prepartatin");
        }

    }

    public static void main(final String[] args) throws IOException {

        final Histogram demo = new Histogram("Histogram");
        demo.pack();

        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}}
vaib
  • 319
  • 4
  • 27

1 Answers1

2

Instead of calling saveChart() in createChart, do so in a button's Action.

final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
add(chartPanel);
add(new JButton(new AbstractAction("Save") {

    @Override
    public void actionPerformed(ActionEvent e) {
        saveChart(chart);
    }
}), BorderLayout.SOUTH);

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • i do that what u have suggested but it didn't show any result i.e. on button click no map will open neither it saves – vaib Jun 17 '14 at 05:21
  • I'm confused. I called your `saveChart()` method. Does it not work for you? Note JPEG loses resolution. You want the chart to appear a _second_ time when you click the button? You could try a `JOptionPane`. – trashgod Jun 17 '14 at 10:38
  • @ trashgod it works as previously my image is save in jpeg format ,what i want is on buttonclick the jfreechart is generated and as well as it saves in drive – vaib Jun 17 '14 at 11:04
  • You should be able to `clear()` the `XYSeries` in the dataset; when you `add()` new values, the listening chart will update itself. – trashgod Jun 17 '14 at 18:03
  • thanks ,can u please elaborate this,suppose in my swing application if i want to show the captured fingerprint image on button click.when i click the button the image is show on the panel same like that what i want there is button when i click on button the jfreechart of histogram is created and automatically it will save in any drive of my computer – vaib Jun 18 '14 at 06:36
  • Use one `JFileChooser` to let the user select the original file and another to select where to save the chart. – trashgod Jun 18 '14 at 15:12