Merge pull request #1253 from RemingtonHolder/unstable

Adds Ability To Export Simulation Graph/Chart As PNG
This commit is contained in:
SiboVG 2022-03-22 03:38:33 +01:00 committed by GitHub
commit b1067b6e48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 4 deletions

View File

@ -96,6 +96,7 @@ FileHelper.ALL_DESIGNS_FILTER = All rocket designs (*.ork; *.rkt)
FileHelper.OPENROCKET_DESIGN_FILTER = OpenRocket designs (*.ork)
FileHelper.ROCKSIM_DESIGN_FILTER = RockSim designs (*.rkt)
FileHelper.OPEN_ROCKET_COMPONENT_FILTER = OpenRocket presets (*.orc)
FileHelper.PNG_FILTER = PNG image (*.png)
FileHelper.IMAGES = Image files
@ -1273,6 +1274,7 @@ TCMotorSelPan.btn.close = Close
! PlotDialog
PlotDialog.CheckBox.Showdatapoints = Show data points
PlotDialog.lbl.Chart = left click drag to zoom area. mouse wheel to zoom. ctrl-mouse wheel to zoom x axis only. ctrl-left click drag to pan. right click drag to zoom dynamically.
PlotDialog.btn.exportImage = Export Image
ComponentTree.ttip.massoverride = mass override
ComponentTree.ttip.cgoverride = cg override

View File

@ -6,26 +6,32 @@ import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.document.Simulation;
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.Icons;
import net.sf.openrocket.gui.util.SwingPreferences;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.startup.Preferences;
import net.sf.openrocket.gui.widgets.SelectColorButton;
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.
@ -50,6 +56,7 @@ public class SimulationPlotDialog extends JDialog {
this.add(panel);
final ChartPanel chartPanel = new SimulationChart(myPlot.getJFreeChart());
final JFreeChart jChart = myPlot.getJFreeChart();
panel.add(chartPanel, "grow, wrap 20lp");
//// Description text
@ -108,6 +115,16 @@ public class SimulationPlotDialog extends JDialog {
}
});
panel.add(button, "gapleft rel");
//// Print chart button
button = new SelectColorButton(trans.get("PlotDialog.btn.exportImage"));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doPngExport(chartPanel,jChart);
}
});
panel.add(button, "gapleft rel");
//// Add series selection box
ArrayList<String> stages = new ArrayList<String>();
@ -141,16 +158,43 @@ public class SimulationPlotDialog extends JDialog {
}
});
panel.add(button, "right");
this.setLocationByPlatform(true);
this.pack();
GUIUtil.setDisposableDialogOptions(this, button);
GUIUtil.rememberWindowSize(this);
}
private boolean doPngExport(ChartPanel chartPanel, JFreeChart chart){
JFileChooser chooser = new JFileChooser();
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(FileHelper.PNG_FILTER);
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, chartPanel.getWidth(), chartPanel.getHeight());
} catch(Exception e){
return false;
}
return true;
}
/**
* Static method that shows a plot with the specified parameters.
*

View File

@ -56,6 +56,10 @@ public final class FileHelper {
public static final FileFilter CSV_FILTER =
new SimpleFileFilter(trans.get("FileHelper.CSV_FILTER"), ".csv");
/** File filter for PNG files (*.png) */
public static final FileFilter PNG_FILTER =
new SimpleFileFilter(trans.get("FileHelper.PNG_FILTER"), ".png");