minor refactoring of gif exporter

This commit is contained in:
hneemann 2017-05-22 08:55:58 +02:00
parent 6fb7ae6b65
commit 752f36f857
2 changed files with 16 additions and 29 deletions

View File

@ -30,12 +30,14 @@ import java.io.IOException;
/** /**
* Exporter which creates an animated GIF file. * Exporter which creates an animated GIF file.
* You can attach it to a model and then every modification
* of the running circuit is reordered as a new frame in the
* GIF file.
* Created by hneemann on 17.05.17. * Created by hneemann on 17.05.17.
*/ */
public class GifExporter extends JDialog implements ModelStateObserver, ModelModifier { public class GifExporter extends JDialog implements ModelStateObserver, ModelModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(GifExporter.class); private static final Logger LOGGER = LoggerFactory.getLogger(GifExporter.class);
private final Circuit circuit; private final Circuit circuit;
private final int delayMs;
private final GraphicMinMax minMax; private final GraphicMinMax minMax;
private final JLabel frameLabel; private final JLabel frameLabel;
private int frames; private int frames;
@ -49,8 +51,10 @@ public class GifExporter extends JDialog implements ModelStateObserver, ModelMod
* @param parent the parent frame * @param parent the parent frame
* @param circuit the circuit to export * @param circuit the circuit to export
* @param delayMs the delay between frames im milliseconds * @param delayMs the delay between frames im milliseconds
* @param file the file to write
* @throws IOException IOException
*/ */
public GifExporter(JFrame parent, Circuit circuit, int delayMs) { public GifExporter(JFrame parent, Circuit circuit, int delayMs, File file) throws IOException {
super(parent, Lang.get("msg_gifExport"), false); super(parent, Lang.get("msg_gifExport"), false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE); setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frameLabel = new JLabel(Lang.get("msg_framesWritten_N", frames)); frameLabel = new JLabel(Lang.get("msg_framesWritten_N", frames));
@ -73,27 +77,15 @@ public class GifExporter extends JDialog implements ModelStateObserver, ModelMod
}.setToolTip(Lang.get("btn_gifComplete_tt")).createJButton(), BorderLayout.SOUTH); }.setToolTip(Lang.get("btn_gifComplete_tt")).createJButton(), BorderLayout.SOUTH);
this.circuit = circuit; this.circuit = circuit;
this.delayMs = delayMs;
minMax = new GraphicMinMax(); minMax = new GraphicMinMax();
circuit.drawTo(minMax); circuit.drawTo(minMax);
pack(); LOGGER.debug("open GIF file");
setLocation(parent.getLocation());
}
/**
* Exports the file
*
* @param file the file to write
* @return this for chained calls
* @throws IOException IOException
* @throws NodeException NodeException
*/
public GifExporter export(File file) throws IOException, NodeException {
output = new FileImageOutputStream(file); output = new FileImageOutputStream(file);
writer = new GifSequenceWriter(output, BufferedImage.TYPE_INT_ARGB, delayMs, true); writer = new GifSequenceWriter(output, BufferedImage.TYPE_INT_ARGB, delayMs, true);
LOGGER.debug("open GIF file");
return this; pack();
setLocation(parent.getLocation());
} }
private void close() { private void close() {
@ -118,10 +110,9 @@ public class GifExporter extends JDialog implements ModelStateObserver, ModelMod
@Override @Override
public void handleEvent(ModelEvent event) { public void handleEvent(ModelEvent event) {
if (event.equals(ModelEvent.STEP)) { if (event.equals(ModelEvent.STEP))
writeImage(); writeImage();
} }
}
private void writeImage() { private void writeImage() {
if (!closed) { if (!closed) {

View File

@ -1162,15 +1162,11 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS
new SaveAsHelper(Main.this, fc, "gif").checkOverwrite( new SaveAsHelper(Main.this, fc, "gif").checkOverwrite(
file -> { file -> {
lastExportDirectory = file.getParentFile(); lastExportDirectory = file.getParentFile();
try { GifExporter gifExporter = new GifExporter(Main.this, circuitComponent.getCircuit(), 500, file);
GifExporter ge = new GifExporter(Main.this, circuitComponent.getCircuit(), 500).export(file);
setDebug(false); setDebug(false);
windowPosManager.closeAll(); windowPosManager.closeAll();
runModelState.enter(false, ge); runModelState.enter(false, gifExporter);
circuitComponent.hasChanged(); circuitComponent.hasChanged();
} catch (NodeException e1) {
new ErrorMessage().addCause(e1).show(Main.this);
}
} }
); );
} }