From 752f36f8570fd74087caefdf581df42b41890a11 Mon Sep 17 00:00:00 2001 From: hneemann Date: Mon, 22 May 2017 08:55:58 +0200 Subject: [PATCH] minor refactoring of gif exporter --- .../neemann/digital/draw/gif/GifExporter.java | 31 +++++++------------ .../java/de/neemann/digital/gui/Main.java | 14 +++------ 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/neemann/digital/draw/gif/GifExporter.java b/src/main/java/de/neemann/digital/draw/gif/GifExporter.java index 335de0af5..c33bae11f 100644 --- a/src/main/java/de/neemann/digital/draw/gif/GifExporter.java +++ b/src/main/java/de/neemann/digital/draw/gif/GifExporter.java @@ -30,12 +30,14 @@ import java.io.IOException; /** * 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. */ public class GifExporter extends JDialog implements ModelStateObserver, ModelModifier { private static final Logger LOGGER = LoggerFactory.getLogger(GifExporter.class); private final Circuit circuit; - private final int delayMs; private final GraphicMinMax minMax; private final JLabel frameLabel; private int frames; @@ -49,8 +51,10 @@ public class GifExporter extends JDialog implements ModelStateObserver, ModelMod * @param parent the parent frame * @param circuit the circuit to export * @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); setDefaultCloseOperation(DISPOSE_ON_CLOSE); 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); this.circuit = circuit; - this.delayMs = delayMs; minMax = new GraphicMinMax(); circuit.drawTo(minMax); - pack(); - 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 { + LOGGER.debug("open GIF file"); output = new FileImageOutputStream(file); writer = new GifSequenceWriter(output, BufferedImage.TYPE_INT_ARGB, delayMs, true); - LOGGER.debug("open GIF file"); - return this; + + pack(); + setLocation(parent.getLocation()); } private void close() { @@ -118,9 +110,8 @@ public class GifExporter extends JDialog implements ModelStateObserver, ModelMod @Override public void handleEvent(ModelEvent event) { - if (event.equals(ModelEvent.STEP)) { + if (event.equals(ModelEvent.STEP)) writeImage(); - } } private void writeImage() { diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index 724e03e20..7fa73703b 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -1162,15 +1162,11 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS new SaveAsHelper(Main.this, fc, "gif").checkOverwrite( file -> { lastExportDirectory = file.getParentFile(); - try { - GifExporter ge = new GifExporter(Main.this, circuitComponent.getCircuit(), 500).export(file); - setDebug(false); - windowPosManager.closeAll(); - runModelState.enter(false, ge); - circuitComponent.hasChanged(); - } catch (NodeException e1) { - new ErrorMessage().addCause(e1).show(Main.this); - } + GifExporter gifExporter = new GifExporter(Main.this, circuitComponent.getCircuit(), 500, file); + setDebug(false); + windowPosManager.closeAll(); + runModelState.enter(false, gifExporter); + circuitComponent.hasChanged(); } ); }