Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Feb 17, 2021
2 parents 4705efb + d40d8ee commit 9dc13b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/java/featurecat/lizzie/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ private JSONObject createPersistConfig() {
// About engine parameter
JSONObject filesys = new JSONObject();
filesys.put("last-folder", "");
filesys.put("last-image-folder", "");

config.put("filesystem", filesys);

Expand Down
37 changes: 24 additions & 13 deletions src/main/java/featurecat/lizzie/gui/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,14 @@ public void replayBranch(boolean generateGif) {
if (generateGif) {
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.gif", "GIF");
JSONObject filesystem = Lizzie.config.persisted.getJSONObject("filesystem");
JFileChooser chooser = new JFileChooser(filesystem.getString("last-folder"));
JFileChooser chooser = new JFileChooser(filesystem.getString("last-image-folder"));
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(false);
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
filesystem.put("last-image-folder", file.getParent());
if (file.exists()) {
int ret =
JOptionPane.showConfirmDialog(
Expand Down Expand Up @@ -607,7 +608,7 @@ public void resetImages() {

public void saveImage() {
JSONObject filesystem = Lizzie.config.persisted.getJSONObject("filesystem");
JFileChooser chooser = new JFileChooser(filesystem.getString("last-folder"));
JFileChooser chooser = new JFileChooser(filesystem.getString("last-image-folder"));
chooser.setAcceptAllFileFilterUsed(false);
// String writerNames[] = ImageIO.getWriterFormatNames();
FileNameExtensionFilter filter1 = new FileNameExtensionFilter("*.png", "PNG");
Expand All @@ -622,6 +623,16 @@ public void saveImage() {
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
filesystem.put("last-image-folder", file.getParent());
String ext =
chooser.getFileFilter() instanceof FileNameExtensionFilter
? ((FileNameExtensionFilter) chooser.getFileFilter()).getExtensions()[0].toLowerCase()
: "";
if (!Utils.isBlank(ext)) {
if (!chooser.getFileFilter().accept(file)) {
file = new File(file.getPath() + "." + ext);
}
}
if (file.exists()) {
int ret =
JOptionPane.showConfirmDialog(
Expand All @@ -633,21 +644,21 @@ public void saveImage() {
return;
}
}
String ext =
chooser.getFileFilter() instanceof FileNameExtensionFilter
? ((FileNameExtensionFilter) chooser.getFileFilter()).getExtensions()[0].toLowerCase()
: "";
if (!Utils.isBlank(ext)) {
if (!file.getPath().toLowerCase().endsWith("." + ext)) {
file = new File(file.getPath() + "." + ext);
}
}
// Never use ARGB here for supporting JPG!
// (cf.)
// https://stackoverflow.com/questions/57673051/writing-jpg-or-jpeg-image-with-imageio-write-does-not-create-image-file
BufferedImage bImg =
new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D cg = bImg.createGraphics();
paintAll(cg);
try {
ImageIO.write(bImg, ext, file);
boolean supported = ImageIO.write(bImg, ext, file);
if (!supported) {
String displayedMessage =
String.format("Failed to save \"%s\".\n(unsupported image format?)", file.getName());
JOptionPane.showMessageDialog(
Lizzie.frame, displayedMessage, "Lizzie - Error!", JOptionPane.ERROR_MESSAGE);
}
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 9dc13b3

Please sign in to comment.