Back to index

wims  3.65+svn20090927
BreakpointAnimator.java
Go to the documentation of this file.
00001 package rene.zirkel.tools;
00002 
00003 import java.awt.event.*;
00004 import java.util.*;
00005 
00006 import rene.util.xml.*;
00007 import rene.util.*;
00008 import rene.zirkel.Zirkel;
00009 import rene.zirkel.ZirkelCanvas;
00010 import rene.zirkel.constructors.*;
00011 import rene.zirkel.objects.*;
00012 
00013 public class BreakpointAnimator extends ObjectConstructor
00014        implements Runnable
00015 {      MyVector Breaks; // Vector of breakpoints
00016        ZirkelCanvas ZC;
00017        boolean Loop=false;
00018 
00019        public BreakpointAnimator ()
00020        {      Breaks=new MyVector();
00021        }
00022 
00023        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00024        {      double x=zc.x(e.getX()),y=zc.y(e.getY());
00025               ZC=zc;
00026               if (Running)
00027               {      if (e.isShiftDown())
00028                             Loop=!Loop;
00029                      else
00030                      {      Stopped=true;
00031                             zc.paintUntil(null);
00032                      }
00033               }
00034               else
00035               {      reset(zc);
00036               }
00037        }
00038 
00039        public synchronized void reset (ZirkelCanvas zc)
00040        {      super.reset(zc);
00041               ZC=zc;
00042               showStatus(zc);
00043               if (Running)
00044               {      Stopped=true;
00045                      zc.paintUntil(null);
00046               }
00047               else
00048               {      Stopped=false;
00049                      new Thread(this).start();
00050               }
00051        }
00052 
00053        public void showStatus (ZirkelCanvas zc)
00054        {      zc.showStatus(
00055                      Zirkel.name("message.animatebreak"));
00056        }
00057 
00058        public void save (XmlWriter xml)
00059        {      xml.startTagStart("AnimateBreakpoints"); 
00060               xml.printArg("time",""+SleepTime);
00061               if (Loop) xml.printArg("loop","true"); 
00062               xml.finishTagNewLine(); 
00063        }
00064        
00065        boolean Running=false,Stopped=false;
00066        long SleepTime=1024;
00067 
00068        public void run ()
00069        {      ZC.getConstruction().setOriginalOrder(true);
00070               Running=true;
00071               Breaks=new MyVector();
00072               Enumeration e=ZC.getConstruction().elements();
00073               while (e.hasMoreElements())
00074               {      ConstructionObject o=(ConstructionObject)e.nextElement();
00075                      if (o.isBreak()) Breaks.addElement(o);
00076               }
00077               Object H[]=Breaks.getArray();
00078               int N=0;
00079               if (Breaks.size()==0)
00080               {      Running=false;
00081                      ZC.getConstruction().setOriginalOrder(false);
00082                      return;
00083               }
00084               ConstructionObject O=(ConstructionObject)H[0];
00085               boolean forward=true;
00086               while (!Stopped)
00087               {      if (!Stopped) ZC.paintUntil(O);
00088                      try
00089                      {      Thread.sleep(SleepTime); 
00090                      }
00091                      catch (Exception ex) {      }             
00092                      if (Stopped) break;
00093                      if (Loop)
00094                      {      if (forward)
00095                             {      if (N<Breaks.size()-1)
00096                                    {      N++;
00097                                           O=(ConstructionObject)H[N];                             
00098                                    }
00099                                    else if (N==Breaks.size()-1)
00100                                    {      N++;
00101                                           forward=false;
00102                                           O=null;
00103                                    }
00104                             }
00105                             else
00106                             {      if (N>0)
00107                                    {      N--;
00108                                           O=(ConstructionObject)H[N];
00109                                    }
00110                                    else
00111                                    {      N=1;
00112                                           O=(ConstructionObject)H[N];
00113                                           forward=true;
00114                                    }
00115                             }
00116                      }
00117                      else
00118                      {      if (N<Breaks.size()-1)
00119                             {      N++;
00120                                    O=(ConstructionObject)H[N];
00121                             }
00122                             else if (N==Breaks.size()-1)
00123                             {      N++;
00124                                    O=null;
00125                             }
00126                             else
00127                             {      N=0;
00128                                    O=(ConstructionObject)H[N];
00129                             }
00130                      }
00131               }
00132               Running=false;
00133               ZC.getConstruction().setOriginalOrder(false);
00134        }
00135 
00136        public synchronized void invalidate (ZirkelCanvas zc)
00137        {      Stopped=true;
00138               ZC.paintUntil(null);
00139        }
00140 
00141        public boolean useSmartBoard ()
00142        {      return false;
00143        }
00144        
00145        public void increaseSpeed ()
00146        {      if (SleepTime>=32000) return;
00147               SleepTime=SleepTime*2;
00148        }
00149 
00150        public void decreaseSpeed ()
00151        {      if (SleepTime==1) return;
00152               SleepTime=SleepTime/2;
00153        }
00154        
00155        public void setSpeed (long delay)
00156        {      SleepTime=delay;
00157        }
00158        
00159        public void setLoop (boolean flag)
00160        {      Loop=flag;
00161        }
00162 }
00163