简体   繁体   English

在按钮上显示JfreeChart单击秋千

[英]Show JfreeChart on Button Click in swing

So below is my code I have created the histogram chart using JFree I wanted to show the out put on Button click 所以下面是我的代码,我已经使用JFree创建了直方图,我想显示按钮单击时的输出

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 我想在botton上单击,然后在面板上打开jfreechart并将其保存在云端硬盘中

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);

    }

}}

Instead of calling saveChart() in createChart , do so in a button's Action . 与其在createChart中调用saveChart()saveChart()在按钮的Action中调用saveChart()

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);

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM