[Patchanim-commit] SF.net SVN: patchanim: [197] trunk/patchanim/src/com/mebigfatguy/patchanim
Brought to you by:
dbrosius
From: <dbr...@us...> - 2008-02-18 03:04:25
|
Revision: 197 http://patchanim.svn.sourceforge.net/patchanim/?rev=197&view=rev Author: dbrosius Date: 2008-02-17 19:04:31 -0800 (Sun, 17 Feb 2008) Log Message: ----------- pull out the patch animation into a separate class and hook in the test pane thru listeners, so that the code can be shared with export. Modified Paths: -------------- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java Added Paths: ----------- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionEvent.java trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionListener.java trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchAnimator.java Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java 2008-02-18 02:31:45 UTC (rev 196) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java 2008-02-18 03:04:31 UTC (rev 197) @@ -25,7 +25,6 @@ import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.lang.reflect.InvocationTargetException; -import java.util.List; import java.util.ResourceBundle; import javax.swing.JFrame; @@ -33,42 +32,24 @@ import javax.swing.SwingUtilities; import com.mebigfatguy.patchanim.AnimationType; -import com.mebigfatguy.patchanim.OutOfBoundsColor; import com.mebigfatguy.patchanim.PatchAnimDocument; +import com.mebigfatguy.patchanim.gui.events.PatchCompletionEvent; +import com.mebigfatguy.patchanim.gui.events.PatchCompletionListener; import com.mebigfatguy.patchanim.main.PatchAnimBundle; -import com.mebigfatguy.patchanim.surface.CombinedPatch; -import com.mebigfatguy.patchanim.surface.PatchGenerator; +import com.mebigfatguy.patchanim.surface.PatchAnimator; -public class JTestFrame extends JFrame { +public class JTestFrame extends JFrame implements PatchCompletionListener { private static final long serialVersionUID = -7975149184522257748L; private Thread animThread = null; private TestPanel testPanel = null; - private int tweenCount; - private int width; - private int height; - private AnimationType type; - private OutOfBoundsColor oob; - private List<CombinedPatch> patches; - private BufferedImage image; + private PatchAnimDocument document; + private long lastRedraw; public JTestFrame() { PatchPanelMediator mediator = PatchPanelMediator.getMediator(); - PatchAnimDocument document = mediator.getDocument(); - tweenCount = document.getTweenCount(); - width = document.getWidth(); - height = document.getHeight(); - type = document.getAnimationType(); - oob = document.getOutOfBoundsColor(); - patches = document.getPatches(); - - if (width == 0) - width = 100; - if (height == 0) - height = 100; - if (tweenCount == 0) - tweenCount = 1; + document = mediator.getDocument(); initComponents(); initListeners(); @@ -77,73 +58,16 @@ public synchronized void beginAnimation() { if (animThread != null) return; - image = PatchGenerator.buildImage(null, width, height); animThread = new Thread(new Runnable() { public void run() { try { - PatchGenerator.recalcCombinedImage(patches.get(0), image, oob); - while (!Thread.interrupted()) { - testPanel.redraw(); - int lastPatch = patches.size() - 1; - long lastRedraw = System.currentTimeMillis(); - for (int p = 0; p < lastPatch; p++) { - CombinedPatch startPatch = patches.get(p); - CombinedPatch endPatch = patches.get(p+1); - for (int t = 0; t < tweenCount + 1; t++) { - CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)(tweenCount + 1)); - PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); - long now = System.currentTimeMillis(); - long sleepTime = 100 - (now - lastRedraw); - if (sleepTime > 0) - Thread.sleep(sleepTime); - testPanel.redraw(); - lastRedraw = now; - } - } - - switch (type) { - case None: { - return; - } - - case Cycle: { - CombinedPatch startPatch = patches.get(lastPatch); - CombinedPatch endPatch = patches.get(0); - for (int t = 0; t <= tweenCount + 1; t++) { - CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)(tweenCount + 1)); - PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); - long now = System.currentTimeMillis(); - long sleepTime = 100 - (now - lastRedraw); - if (sleepTime > 0) - Thread.sleep(sleepTime); - testPanel.redraw(); - lastRedraw = now; - } - } - break; - - case 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 + 1)); - PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); - long now = System.currentTimeMillis(); - long sleepTime = 100 - (now - lastRedraw); - if (sleepTime > 0) - Thread.sleep(sleepTime); - testPanel.redraw(); - lastRedraw = now; - } - } - } - break; - } - } + PatchAnimator animator = new PatchAnimator(document); + animator.addPatchCompletionListener(JTestFrame.this); + AnimationType type = document.getAnimationType(); + while (type != AnimationType.None) + animator.animatePatches(); } catch (InterruptedException ie) { - //OK } } }); @@ -165,6 +89,15 @@ } } + public void patchCompleted(PatchCompletionEvent pce) throws InterruptedException { + long now = System.currentTimeMillis(); + long sleepTime = 100 - (now - lastRedraw); + if (sleepTime > 0) + Thread.sleep(sleepTime); + testPanel.redraw(pce.getImage()); + lastRedraw = now; + } + private void initComponents() { ResourceBundle rb = PatchAnimBundle.getBundle(); testPanel = new TestPanel(); @@ -185,9 +118,20 @@ } class TestPanel extends JPanel { - private static final long serialVersionUID = 6268304008663415749L; + private static final long serialVersionUID = -6464417075282170562L; + private BufferedImage image = null; + private int width; + private int height; public TestPanel() { + width = document.getWidth(); + height = document.getHeight(); + + if (width == 0) + width = 100; + if (height == 0) + height = 100; + Dimension d = new Dimension(width, height); setMinimumSize(d); setMaximumSize(d); @@ -195,8 +139,9 @@ setSize(width, height); } - public void redraw() throws InterruptedException { + public void redraw(BufferedImage redrawImage) throws InterruptedException { try { + image = redrawImage; SwingUtilities.invokeAndWait(new Runnable() { public void run() { invalidate(); @@ -213,7 +158,8 @@ @Override public void paintComponent(Graphics g) { - g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null); + if (image != null) + g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null); } } } Added: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionEvent.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionEvent.java (rev 0) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionEvent.java 2008-02-18 03:04:31 UTC (rev 197) @@ -0,0 +1,19 @@ +package com.mebigfatguy.patchanim.gui.events; + +import java.awt.image.BufferedImage; +import java.util.EventObject; + +public class PatchCompletionEvent extends EventObject { + + private static final long serialVersionUID = -2446326623721576523L; + private BufferedImage image; + + public PatchCompletionEvent(Object src, BufferedImage animatedImage) { + super(src); + image = animatedImage; + } + + public BufferedImage getImage() { + return image; + } +} Property changes on: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionEvent.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionListener.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionListener.java (rev 0) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionListener.java 2008-02-18 03:04:31 UTC (rev 197) @@ -0,0 +1,5 @@ +package com.mebigfatguy.patchanim.gui.events; + +public interface PatchCompletionListener { + void patchCompleted(PatchCompletionEvent pce) throws InterruptedException; +} Property changes on: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/events/PatchCompletionListener.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchAnimator.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchAnimator.java (rev 0) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchAnimator.java 2008-02-18 03:04:31 UTC (rev 197) @@ -0,0 +1,92 @@ +package com.mebigfatguy.patchanim.surface; + +import java.awt.image.BufferedImage; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.mebigfatguy.patchanim.AnimationType; +import com.mebigfatguy.patchanim.OutOfBoundsColor; +import com.mebigfatguy.patchanim.PatchAnimDocument; +import com.mebigfatguy.patchanim.gui.events.PatchCompletionEvent; +import com.mebigfatguy.patchanim.gui.events.PatchCompletionListener; + +public class PatchAnimator { + + private Set<PatchCompletionListener> pcListeners = new HashSet<PatchCompletionListener>(); + private PatchAnimDocument document; + + public PatchAnimator(PatchAnimDocument paDocument) { + document = paDocument; + } + + public void addPatchCompletionListener(PatchCompletionListener listener) { + pcListeners.add(listener); + } + + public void animatePatches() throws InterruptedException { + 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(); + AnimationType atype = document.getAnimationType(); + + if (lastPatch == 0) { + PatchGenerator.recalcCombinedImage(patches.get(0), image, oob); + firePatchCompleted(image); + } else { + for(int p = 0; p < lastPatch; p++) { + CombinedPatch startPatch = patches.get(p); + CombinedPatch endPatch = patches.get(p+1); + for (int t = 0; t < tweenCount + 1; t++) { + CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)(tweenCount + 1)); + PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); + firePatchCompleted(image); + } + } + + switch (atype) { + case None: { + CombinedPatch patch = patches.get(lastPatch); + PatchGenerator.recalcCombinedImage(patch, image, oob); + firePatchCompleted(image); + if (atype == AnimationType.None) + return; + } + break; + + case Cycle: { + CombinedPatch startPatch = patches.get(lastPatch); + CombinedPatch endPatch = patches.get(0); + for (int t = 0; t < tweenCount + 1; t++) { + CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)(tweenCount + 1)); + PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); + firePatchCompleted(image); + } + } + break; + + case 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 + 1)); + PatchGenerator.recalcCombinedImage(tweenPatch, image, oob); + firePatchCompleted(image); + } + } + } + break; + } + } + } + + private void firePatchCompleted(BufferedImage image) throws InterruptedException { + PatchCompletionEvent pce = new PatchCompletionEvent(this, image); + for (PatchCompletionListener listener : pcListeners) { + listener.patchCompleted(pce); + } + } +} Property changes on: trunk/patchanim/src/com/mebigfatguy/patchanim/surface/PatchAnimator.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |