Skip to content

Commit

Permalink
Add "Export as Image" function
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyers committed Jun 18, 2024
1 parent aba9a9e commit 75a3436
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 20 deletions.
45 changes: 32 additions & 13 deletions EU3_Scenario_Editor/src/editor/EditorUI.form
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@
<Property name="text" type="java.lang.String" value="File"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="reloadMenuItem">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
<KeyStroke key="Ctrl+R"/>
</Property>
<Property name="text" type="java.lang.String" value="Force a reload of all data"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="reloadMenuItemActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableLocal" type="java.lang.Boolean" value="false"/>
</AuxValues>
</MenuItem>
<MenuItem class="javax.swing.JPopupMenu$Separator" name="jSeparator7">
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="exportImageMenuItem">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
<KeyStroke key="Ctrl+S"/>
</Property>
<Property name="text" type="java.lang.String" value="Export map as image..."/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="exportImageMenuItemActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableLocal" type="java.lang.Boolean" value="false"/>
</AuxValues>
</MenuItem>
<MenuItem class="javax.swing.JPopupMenu$Separator" name="jSeparator8">
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="exitMenuItem">
<Properties>
<Property name="accelerator" type="javax.swing.KeyStroke" editor="org.netbeans.modules.form.editors.KeyStrokeEditor">
Expand Down Expand Up @@ -86,19 +118,6 @@
<AuxValue name="JavaCodeGenerator_VariableModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
</MenuItem>
<MenuItem class="javax.swing.JSeparator" name="jSeparator7">
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="reloadMenuItem">
<Properties>
<Property name="text" type="java.lang.String" value="Force a reload of all data"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="reloadMenuItemActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_VariableLocal" type="java.lang.Boolean" value="false"/>
</AuxValues>
</MenuItem>
</SubComponents>
</Menu>
<Menu class="javax.swing.JMenu" name="viewMenu">
Expand Down
56 changes: 49 additions & 7 deletions EU3_Scenario_Editor/src/editor/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
*
Expand Down Expand Up @@ -410,6 +412,10 @@ private void initComponents() {
showCountryHistButton = new javax.swing.JButton();
javax.swing.JMenuBar menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
reloadMenuItem = new javax.swing.JMenuItem();
javax.swing.JPopupMenu.Separator jSeparator7 = new javax.swing.JPopupMenu.Separator();
exportImageMenuItem = new javax.swing.JMenuItem();
javax.swing.JPopupMenu.Separator jSeparator8 = new javax.swing.JPopupMenu.Separator();
exitMenuItem = new javax.swing.JMenuItem();
toolsMenu = new javax.swing.JMenu();
zoomInMenuItem = new javax.swing.JMenuItem();
Expand All @@ -418,8 +424,6 @@ private void initComponents() {
goToProvMenuItem = new javax.swing.JMenuItem();
javax.swing.JSeparator jSeparator3 = new javax.swing.JSeparator();
setDateMenuItem = new javax.swing.JMenuItem();
javax.swing.JSeparator jSeparator7 = new javax.swing.JSeparator();
reloadMenuItem = new javax.swing.JMenuItem();
viewMenu = new javax.swing.JMenu();
helpMenu = new javax.swing.JMenu();
aboutMenuItem = new javax.swing.JMenuItem();
Expand Down Expand Up @@ -539,6 +543,18 @@ private void initComponents() {
fileMenu.setMnemonic('F');
fileMenu.setText("File");

reloadMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_DOWN_MASK));
reloadMenuItem.setText("Force a reload of all data");
reloadMenuItem.addActionListener(formListener);
fileMenu.add(reloadMenuItem);
fileMenu.add(jSeparator7);

exportImageMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_DOWN_MASK));
exportImageMenuItem.setText("Export map as image...");
exportImageMenuItem.addActionListener(formListener);
fileMenu.add(exportImageMenuItem);
fileMenu.add(jSeparator8);

exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_DOWN_MASK));
exitMenuItem.setMnemonic('x');
exitMenuItem.setText("Exit");
Expand All @@ -563,11 +579,6 @@ private void initComponents() {

setDateMenuItem.setAction(setDateAction);
toolsMenu.add(setDateMenuItem);
toolsMenu.add(jSeparator7);

reloadMenuItem.setText("Force a reload of all data");
reloadMenuItem.addActionListener(formListener);
toolsMenu.add(reloadMenuItem);

menuBar.add(toolsMenu);

Expand Down Expand Up @@ -628,6 +639,9 @@ else if (evt.getSource() == aboutMenuItem) {
else if (evt.getSource() == checkVersionMenuItem) {
EditorUI.this.checkVersionMenuItemActionPerformed(evt);
}
else if (evt.getSource() == exportImageMenuItem) {
EditorUI.this.exportImageMenuItemActionPerformed(evt);
}
}

public void mouseClicked(java.awt.event.MouseEvent evt) {
Expand Down Expand Up @@ -940,6 +954,33 @@ private void mapPanelMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-
zoomInAction.actionPerformed(null);
}
}//GEN-LAST:event_mapPanelMouseWheelMoved

private void exportImageMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportImageMenuItemActionPerformed
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setFileFilter(new FileNameExtensionFilter("JPG or PNG files", "jpg", "jpeg", "png"));

if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
String fileName = chooser.getSelectedFile().toString();
String format;
if (fileName.toLowerCase().endsWith("jpg")
|| fileName.toLowerCase().endsWith("jpeg")) {
format = "JPEG";
} else if (fileName.toLowerCase().endsWith("png")) {
format = "PNG";
} else {
return;
}

try {
ImageIO.write(mapPanel.getCurrentSnapshot(), format, chooser.getSelectedFile());
JOptionPane.showMessageDialog(this, "Saved successfully to " + fileName);
} catch (IOException ex) {
log.log(Level.WARNING, "Failed to save snapshot image to {0}", fileName);
JOptionPane.showMessageDialog(this, "Failed to save image!", "Error", JOptionPane.WARNING_MESSAGE);
}
}
}//GEN-LAST:event_exportImageMenuItemActionPerformed

private void checkForUpdates() {
SwingWorker<Boolean, String> worker = new SwingWorker<Boolean, String>() {
Expand Down Expand Up @@ -2041,6 +2082,7 @@ private void addNativesFilters(JMenu rootMenu, GenericObject colors) {
private javax.swing.JLabel ctryNameLabel;
private javax.swing.JSpinner daySpinner;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenuItem exportImageMenuItem;
javax.swing.JButton flashProvsButton;
private javax.swing.JButton goToProvButton;
private javax.swing.JMenuItem goToProvMenuItem;
Expand Down
7 changes: 7 additions & 0 deletions EU3_Scenario_Editor/src/editor/MapPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ private void initComponents() {
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
// </editor-fold>

public BufferedImage getCurrentSnapshot() {
if (isDirty || backBufferImage == null)
paintBuffer();

return backBufferImage;
}

@Override
protected void paintComponent(final Graphics g) {
Expand Down

0 comments on commit 75a3436

Please sign in to comment.