com.decypher.threadsclient.JPanelChart.java Source code

Java tutorial

Introduction

Here is the source code for com.decypher.threadsclient.JPanelChart.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 com.decypher.threadsclient;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author JHK
 */
public class JPanelChart extends JPanel {

    JFreeChart chart;

    private JPanelChart() {
    }

    public JPanelChart(JFreeChart chrt) {
        chart = chrt;
    }

    private String getFolder() {
        JFileChooser folderPick = new JFileChooser();
        folderPick.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        folderPick.setDialogType(JFileChooser.SAVE_DIALOG);
        folderPick.setMultiSelectionEnabled(false);
        String selected;
        int returnVal = folderPick.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            selected = folderPick.getSelectedFile().getPath();
        } else {
            selected = "";
        }
        return selected;
    }

    public void saveAsImage() {

        String folder = getFolder();
        if (!folder.isEmpty()) {
            String location = folder + "/" + UUID.randomUUID().toString() + ".jpeg";
            int width = 1280;
            int height = 960;
            try {
                File imgChart = new File(location);
                ChartUtilities.saveChartAsJPEG(imgChart, chart, width, height);
                Desktop.getDesktop().open(new File(folder));
            } catch (IOException ex) {
                Extra.MessageBox.Show(ex.getMessage(), Extra.Title.Error);
            }
        }
    }
}