[Patchanim-commit] SF.net SVN: patchanim: [29] trunk/patchanim/src/com/mebigfatguy/patchanim/ gui/J
Brought to you by:
dbrosius
From: <dbr...@us...> - 2008-01-27 01:43:20
|
Revision: 29 http://patchanim.svn.sourceforge.net/patchanim/?rev=29&view=rev Author: dbrosius Date: 2008-01-26 17:43:26 -0800 (Sat, 26 Jan 2008) Log Message: ----------- The test frame for animations Added Paths: ----------- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java Added: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java (rev 0) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.java 2008-01-27 01:43:26 UTC (rev 29) @@ -0,0 +1,140 @@ +package com.mebigfatguy.patchanim.gui; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.image.BufferedImage; +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; + +import com.mebigfatguy.patchanim.PatchAnimDocument; +import com.mebigfatguy.patchanim.surface.CombinedPatch; +import com.mebigfatguy.patchanim.surface.PatchGenerator; + +public class JTestFrame extends JFrame { + + private Thread animThread = null; + private TestPanel testPanel = null; + private int tweenCount; + private int width; + private int height; + private List<CombinedPatch> patches; + private BufferedImage image; + + public JTestFrame() { + PatchPanelMediator mediator = PatchPanelMediator.getMediator(); + PatchAnimDocument document = mediator.getDocument(); + tweenCount = document.getTweenCount(); + width = document.getWidth(); + height = document.getHeight(); + patches = document.getPatches(); + + initComponents(); + initListeners(); + } + + 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); + 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; t++) { + CombinedPatch tweenPatch = CombinedPatch.tween(startPatch, endPatch, (double)t / (double)tweenCount); + PatchGenerator.recalcCombinedImage(tweenPatch, image); + long now = System.currentTimeMillis(); + long sleepTime = 100 - (now - lastRedraw); + if (sleepTime > 0) + Thread.sleep(sleepTime); + testPanel.redraw(); + lastRedraw = now; + } + } + } + } catch (InterruptedException ie) { + //OK + } + } + }); + animThread.start(); + + } + + public synchronized void endAnimation() { + if (animThread == null) + return; + + try { + animThread.interrupt(); + animThread.join(); + } catch (InterruptedException ie) { + //OK + } finally { + animThread = null; + } + } + + private void initComponents() { + testPanel = new TestPanel(); + setLayout(new BorderLayout(4, 4)); + add(testPanel, BorderLayout.CENTER); + pack(); + } + + private void initListeners() { + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent we) { + endAnimation(); + dispose(); + } + }); + } + + class TestPanel extends JPanel { + public TestPanel() { + Dimension d = new Dimension(width, height); + setMinimumSize(d); + setMaximumSize(d); + setPreferredSize(d); + setSize(width, height); + } + + public void redraw() throws InterruptedException { + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + invalidate(); + revalidate(); + repaint(); + } + }); + } catch (InvocationTargetException ite) { + InterruptedException ie = new InterruptedException("Error rendering test"); + ie.initCause(ite); + throw ie; + } + } + + @Override + public void paintComponent(Graphics g) { + g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null); + } + } +} Property changes on: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JTestFrame.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. |