Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #870 and other bugs in saveImage #872

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -566,6 +566,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 @@ -521,13 +521,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 @@ -595,7 +596,7 @@ public void drawEstimateRect(ArrayList<Double> estimateArray, boolean isZen) {

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 @@ -610,6 +611,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 @@ -621,21 +632,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