[Patchanim-commit] SF.net SVN: patchanim: [57] trunk/patchanim/src/com/mebigfatguy/patchanim
Brought to you by:
dbrosius
From: <dbr...@us...> - 2008-01-30 01:34:30
|
Revision: 57 http://patchanim.svn.sourceforge.net/patchanim/?rev=57&view=rev Author: dbrosius Date: 2008-01-29 17:34:35 -0800 (Tue, 29 Jan 2008) Log Message: ----------- break out export routines to another class, fix the add scroll button Modified Paths: -------------- trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties Added Paths: ----------- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java 2008-01-30 00:59:16 UTC (rev 56) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/ExportType.java 2008-01-30 01:34:35 UTC (rev 57) @@ -1,18 +1,22 @@ package com.mebigfatguy.patchanim; +import com.mebigfatguy.patchanim.main.PatchAnimBundle; + public enum ExportType { - JPegs("jpg", true), - Pngs("png", true), - Gifs("gif", true), - AnimatedGif("gif", false), - MPeg("mpg", false); + JPegs("jpg", true, PatchAnimBundle.JPGSERIESFILTER), + Pngs("png", true, PatchAnimBundle.PNGSERIESFILTER), + Gifs("gif", true, PatchAnimBundle.GIFSERIESFILTER), + AnimatedGif("gif", false, PatchAnimBundle.ANIMATEDGIFFILTER), + MPeg("mpeg", false, PatchAnimBundle.MPEGFILTER); private String ext; private boolean multi; + private String key; - private ExportType(String extension, boolean multipleFiles) { + private ExportType(String extension, boolean multipleFiles, String descriptionKey) { ext = extension; multi = multipleFiles; + key = descriptionKey; } public String getExtension() { @@ -22,4 +26,8 @@ public boolean isMultipleFiles() { return multi; } + + public String getDescriptionKey() { + return key; + } } Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-01-30 00:59:16 UTC (rev 56) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-01-30 01:34:35 UTC (rev 57) @@ -46,6 +46,7 @@ import com.mebigfatguy.patchanim.OutOfBoundsColor; import com.mebigfatguy.patchanim.PatchAnimDocument; import com.mebigfatguy.patchanim.io.PatchAnimIO; +import com.mebigfatguy.patchanim.io.PatchExporter; import com.mebigfatguy.patchanim.main.PatchAnimBundle; import com.mebigfatguy.patchanim.surface.CombinedPatch; import com.mebigfatguy.patchanim.surface.PatchGenerator; @@ -66,9 +67,7 @@ private JMenuItem quitItem; private PatchAnimDocument document; private File documentLocation; - private AnimatedGifEncoder agEncoder = null; - public JPatchAnimFrame() { initComponents(); initMenus(); @@ -213,7 +212,11 @@ exportJpgsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { - exportFiles(ExportType.JPegs); + File f = getExportLocation(ExportType.JPegs); + if (f != null) { + PatchExporter exporter = new PatchExporter(ExportType.JPegs, f); + exporter.export(document); + } } catch (IOException ioe) { JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.EXPORTFAILED); } @@ -223,7 +226,11 @@ exportPngsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { - exportFiles(ExportType.Pngs); + File f = getExportLocation(ExportType.Pngs); + if (f != null) { + PatchExporter exporter = new PatchExporter(ExportType.Pngs, f); + exporter.export(document); + } } catch (IOException ioe) { JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.EXPORTFAILED); } @@ -233,7 +240,11 @@ exportGifsItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { - exportFiles(ExportType.Gifs); + File f = getExportLocation(ExportType.Gifs); + if (f != null) { + PatchExporter exporter = new PatchExporter(ExportType.Gifs, f); + exporter.export(document); + } } catch (IOException ioe) { JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.EXPORTFAILED); } @@ -243,7 +254,11 @@ exportAnimatedGifItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { - exportFiles(ExportType.AnimatedGif); + File f = getExportLocation(ExportType.AnimatedGif); + if (f != null) { + PatchExporter exporter = new PatchExporter(ExportType.AnimatedGif, f); + exporter.export(document); + } } catch (IOException ioe) { JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.EXPORTFAILED); } @@ -354,7 +369,8 @@ return JOptionPane.showConfirmDialog(JPatchAnimFrame.this, rb.getString(PatchAnimBundle.ASKSAVE), rb.getString(PatchAnimBundle.TITLE), JOptionPane.YES_NO_CANCEL_OPTION); } - private void exportFiles(ExportType type) throws IOException { + private File getExportLocation(final ExportType type) { + final ResourceBundle rb = PatchAnimBundle.getBundle(); JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); File defLocation; @@ -362,85 +378,28 @@ defLocation = new File(System.getProperty("user.dir")); else defLocation = documentLocation.getParentFile(); - chooser.setCurrentDirectory(defLocation); - int option = chooser.showSaveDialog(JPatchAnimFrame.this); - if (option != JFileChooser.APPROVE_OPTION) - return; - File dir = chooser.getSelectedFile(); - String baseName = dir.getName(); - if (type.isMultipleFiles()) { - dir.mkdir(); - } else { - dir = dir.getParentFile(); - String dotExt = "." + type.getExtension(); - if (!baseName.endsWith(dotExt)) - baseName = baseName + dotExt; - if (type == ExportType.AnimatedGif) { - agEncoder = new AnimatedGifEncoder(); - agEncoder.start(new File(dir, baseName).getPath()); - } - } + if (!type.isMultipleFiles()) { + chooser.setFileFilter(new FileFilter() { - BufferedImage image = PatchGenerator.buildImage(null, document.getWidth(), document.getHeight()); - - List<CombinedPatch> patches = document.getPatches(); - int lastPatch = patches.size() - 1; - int tweenCount = document.getTweenCount(); - OutOfBoundsColor oob = document.getOutOfBoundsColor(); - int imageIndex = 1; - AnimationType atype = document.getAnimationType(); - - for(int p = 0; p < lastPatch; p++) { - CombinedPatch startPatch = patches.get(p); - CombinedPatch endPatch = patches.get(p+1); - for (int t = 0; t < tweenCount; t++) { - CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)tweenCount); - PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); - writeSingleFile(image, imageIndex++, dir, baseName, type); - } - } - - if (atype == AnimationType.None) - return; - - if (atype == AnimationType.Wave) { - for (int p = lastPatch; p > 0; p--) { - CombinedPatch startPatch = patches.get(p-1); - CombinedPatch endPatch = patches.get(p); - for (int t = tweenCount - 1; t >= 0; t--) { - CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)tweenCount); - PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); - writeSingleFile(image, imageIndex++, dir, baseName, type); + @Override + public boolean accept(File f) { + return (f.isDirectory() || f.getPath().toLowerCase().endsWith("." + type.getExtension())); } - } + + @Override + public String getDescription() { + return rb.getString(type.getDescriptionKey()); + } + }); } - if (type == ExportType.AnimatedGif) { - agEncoder.finish(); - agEncoder = null; - } + int option = chooser.showSaveDialog(JPatchAnimFrame.this); + if (option != JFileChooser.APPROVE_OPTION) + return null; + + return chooser.getSelectedFile(); } - - private void writeSingleFile(BufferedImage image, int index, File dir, String baseName, ExportType type) throws IOException { - NumberFormat nf = NumberFormat.getIntegerInstance(); - nf.setMinimumIntegerDigits(6); - String name = baseName + "_" + nf.format(index) + "." + type.getExtension(); - - File imageFile = new File(dir, name); - - if (type == ExportType.Gifs) { - AnimatedGifEncoder encoder = new AnimatedGifEncoder(); - encoder.start(imageFile.getPath()); - encoder.addFrame(image); - encoder.finish(); - } else if (type == ExportType.AnimatedGif) { - agEncoder.addFrame(image); - agEncoder.setDelay(100); - } - else - ImageIO.write(image, type.getExtension(), imageFile); - } } Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java 2008-01-30 00:59:16 UTC (rev 56) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchListPanel.java 2008-01-30 01:34:35 UTC (rev 57) @@ -96,14 +96,14 @@ public void actionPerformed(ActionEvent ae) { int selIndex = patchList.getSelectedIndex(); if (selIndex < 0) - selIndex = patchListModel.getSize() - 1; + selIndex = patchListModel.getSize(); + else + selIndex++; CombinedPatch newPatch = new CombinedPatch(true); patchListModel.add(selIndex, newPatch); - PatchPanelMediator mediator = PatchPanelMediator.getMediator(); - mediator.setNewActivePatch(newPatch); + patchList.setSelectedIndex(selIndex); removeButton.setEnabled(patchListModel.getSize() > 1); - patchList.setSelectedIndex(selIndex); } }); Added: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java (rev 0) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java 2008-01-30 01:34:35 UTC (rev 57) @@ -0,0 +1,125 @@ +/* + * patchanim - A bezier surface patch color blend gif builder + * Copyright (C) 2008 Dave Brosius + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package com.mebigfatguy.patchanim.io; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.text.NumberFormat; +import java.util.List; + +import javax.imageio.ImageIO; + +import com.fmsware.gif.AnimatedGifEncoder; +import com.mebigfatguy.patchanim.AnimationType; +import com.mebigfatguy.patchanim.ExportType; +import com.mebigfatguy.patchanim.OutOfBoundsColor; +import com.mebigfatguy.patchanim.PatchAnimDocument; +import com.mebigfatguy.patchanim.surface.CombinedPatch; +import com.mebigfatguy.patchanim.surface.PatchGenerator; + +public class PatchExporter { + private ExportType type; + private File loc; + private AnimatedGifEncoder agEncoder; + + public PatchExporter(ExportType exportType, File location) { + type = exportType; + loc = location; + if (type == ExportType.AnimatedGif) + agEncoder = new AnimatedGifEncoder(); + else + agEncoder = null; + } + + public void export(PatchAnimDocument document) throws IOException { + try { + String baseName = loc.getName(); + if (type.isMultipleFiles()) { + loc.mkdir(); + } else { + loc = loc.getParentFile(); + String dotExt = "." + type.getExtension(); + if (!baseName.toLowerCase().endsWith(dotExt)) + baseName = baseName + dotExt; + if (type == ExportType.AnimatedGif) + agEncoder.start(new File(loc, baseName).getPath()); + } + + BufferedImage image = PatchGenerator.buildImage(null, document.getWidth(), document.getHeight()); + + List<CombinedPatch> patches = document.getPatches(); + int lastPatch = patches.size() - 1; + int tweenCount = document.getTweenCount(); + OutOfBoundsColor oob = document.getOutOfBoundsColor(); + int imageIndex = 1; + AnimationType atype = document.getAnimationType(); + + for(int p = 0; p < lastPatch; p++) { + CombinedPatch startPatch = patches.get(p); + CombinedPatch endPatch = patches.get(p+1); + for (int t = 0; t < tweenCount; t++) { + CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)tweenCount); + PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); + writeSingleFile(image, imageIndex++, loc, baseName, type); + } + } + + if (atype == AnimationType.None) + return; + + if (atype == AnimationType.Wave) { + for (int p = lastPatch; p > 0; p--) { + CombinedPatch startPatch = patches.get(p-1); + CombinedPatch endPatch = patches.get(p); + for (int t = tweenCount - 1; t >= 0; t--) { + CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)tweenCount); + PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); + writeSingleFile(image, imageIndex++, loc, baseName, type); + } + } + } + } finally { + if (type == ExportType.AnimatedGif) { + agEncoder.finish(); + } + } + } + + private void writeSingleFile(BufferedImage image, int index, File dir, String baseName, ExportType type) throws IOException { + NumberFormat nf = NumberFormat.getIntegerInstance(); + nf.setMinimumIntegerDigits(6); + String name = baseName + "_" + nf.format(index) + "." + type.getExtension(); + + File imageFile = new File(dir, name); + + if (type == ExportType.Gifs) { + AnimatedGifEncoder encoder = new AnimatedGifEncoder(); + encoder.start(imageFile.getPath()); + encoder.addFrame(image); + encoder.finish(); + } else if (type == ExportType.AnimatedGif) { + agEncoder.addFrame(image); + agEncoder.setDelay(100); + } + else + ImageIO.write(image, type.getExtension(), imageFile); + } + +} Property changes on: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchExporter.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-01-30 00:59:16 UTC (rev 56) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-01-30 01:34:35 UTC (rev 57) @@ -31,9 +31,15 @@ public static final String SAVEAS = "patchanim.saveas"; public static final String EXPORT = "patchanim.export"; public static final String JPGSERIES = "patchanim.jpgs"; + public static final String JPGSERIESFILTER = "patchanim.filter.jpgs"; public static final String PNGSERIES = "patchanim.pngs"; + public static final String PNGSERIESFILTER = "patchanim.filter.pngs"; public static final String GIFSERIES = "patchanim.gifs"; + public static final String GIFSERIESFILTER = "patchanim.filter.gifs"; public static final String ANIMATEDGIF = "patchanim.animatedgif"; + public static final String ANIMATEDGIFFILTER = "patchanim.filter.animatedgif"; + public static final String MPEG = "patchanim.mpeg"; + public static final String MPEGFILTER = "patchanim.filter.mpeg"; public static final String QUIT = "patchanim.quit"; public static final String CONTROLS = "patchanim.control"; public static final String WIDTH = "patchanim.width"; Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-01-30 00:59:16 UTC (rev 56) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-01-30 01:34:35 UTC (rev 57) @@ -24,9 +24,15 @@ patchanim.saveas = Save As... patchanim.export = Export As... patchanim.jpgs = a series of JPGs +patchanim.filter.jpgs = Files (*.jpg) patchanim.pngs = a series of PNGs +patchanim.filter.pnga = Png Files (*.png) patchanim.gifs = a series of GIFs +patchanim.filter.gifs = Gif Files (*.gif) patchanim.animatedgif = an Animated Gif +patchanim.filter.animatedgif = Gif Files (*.gif) +patchanim.mpeg = an MPEG +patchanim.filter.mpeg = MPEG Files (*.mpg) patchanim.quit = Quit patchanim.control = Controls patchanim.width = Width This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |