Adds Export PNG Button To Sim Chart Page

This commit is contained in:
Remington Holder 2022-03-19 22:15:12 -04:00
parent 402cc4b97e
commit d846094151

View File

@ -6,26 +6,32 @@ import java.awt.event.ActionListener;
import java.awt.event.InputEvent; import java.awt.event.InputEvent;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.awt.event.ItemListener; import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.document.Simulation; import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.gui.components.StyledLabel; import net.sf.openrocket.gui.components.StyledLabel;
import net.sf.openrocket.gui.util.FileHelper;
import net.sf.openrocket.gui.util.GUIUtil; import net.sf.openrocket.gui.util.GUIUtil;
import net.sf.openrocket.gui.util.Icons; import net.sf.openrocket.gui.util.Icons;
import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.l10n.Translator; import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.startup.Application; import net.sf.openrocket.startup.Application;
import net.sf.openrocket.startup.Preferences; import net.sf.openrocket.startup.Preferences;
import net.sf.openrocket.gui.widgets.SelectColorButton; import net.sf.openrocket.gui.widgets.SelectColorButton;
import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
/** /**
* Dialog that shows a plot of a simulation results based on user options. * Dialog that shows a plot of a simulation results based on user options.
@ -50,6 +56,7 @@ public class SimulationPlotDialog extends JDialog {
this.add(panel); this.add(panel);
final ChartPanel chartPanel = new SimulationChart(myPlot.getJFreeChart()); final ChartPanel chartPanel = new SimulationChart(myPlot.getJFreeChart());
final JFreeChart jChart = myPlot.getJFreeChart();
panel.add(chartPanel, "grow, wrap 20lp"); panel.add(chartPanel, "grow, wrap 20lp");
//// Description text //// Description text
@ -108,6 +115,16 @@ public class SimulationPlotDialog extends JDialog {
} }
}); });
panel.add(button, "gapleft rel"); panel.add(button, "gapleft rel");
//// Print chart button
button = new SelectColorButton(Icons.FILE_PRINT);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doPngExport(jChart);
}
});
panel.add(button, "gapleft rel");
//// Add series selection box //// Add series selection box
ArrayList<String> stages = new ArrayList<String>(); ArrayList<String> stages = new ArrayList<String>();
@ -141,16 +158,42 @@ public class SimulationPlotDialog extends JDialog {
} }
}); });
panel.add(button, "right"); panel.add(button, "right");
this.setLocationByPlatform(true); this.setLocationByPlatform(true);
this.pack(); this.pack();
GUIUtil.setDisposableDialogOptions(this, button); GUIUtil.setDisposableDialogOptions(this, button);
GUIUtil.rememberWindowSize(this); GUIUtil.rememberWindowSize(this);
} }
private boolean doPngExport(JFreeChart chart){
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(FileHelper.getImageFileFilter());
chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory());
/* Ensures No Problems When Choosing File */
if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
return false;
File file = chooser.getSelectedFile();
if (file == null)
return false;
file = FileHelper.forceExtension(file, "png");
if (!FileHelper.confirmWrite(file, this)) {
return false;
}
/* Uses JFreeChart Built In PNG Export Method */
try{
ChartUtilities.saveChartAsPNG(file, chart, 1000, 500);
} catch(Exception e){
return false;
}
return true;
}
/** /**
* Static method that shows a plot with the specified parameters. * Static method that shows a plot with the specified parameters.
* *