Back to index

wims  3.65+svn20090927
AnimatorTool.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.zirkel.Zirkel;
00008 import rene.zirkel.ZirkelCanvas;
00009 import rene.zirkel.construction.Selector;
00010 import rene.zirkel.constructors.*;
00011 import rene.zirkel.objects.*;
00012 
00022 public class AnimatorTool extends ObjectConstructor
00023        implements Runnable, Selector
00024 {      PointObject P; 
00025        Vector V; // Vector of segments or circles
00026        ZirkelCanvas ZC; 
00027        boolean Running=false,Interactive=true,Complete=false; 
00028        boolean Negative=false; 
00029        boolean Original=false;
00030        double Delay=50;
00031        
00032        public AnimatorTool ()
00033        {      V=new Vector(); 
00034               P=null; 
00035        }
00036        
00037        public AnimatorTool (PointObject p, Vector v, ZirkelCanvas zc, boolean negative,
00038                      boolean original, String delay)
00039        {      P=p; 
00040               if (!P.moveable()) return; 
00041               V=new Vector(); 
00042               Enumeration e=v.elements(); 
00043               while (e.hasMoreElements())
00044               {      ConstructionObject o=zc.getConstruction().find(
00045                      (String)e.nextElement()); 
00046                      if (!(o instanceof SegmentObject ||
00047                             o instanceof PrimitiveCircleObject ||
00048                             o instanceof PointObject
00049                             ))
00050                             return; 
00051                      V.addElement(o); 
00052               }
00053               Stopped=false; 
00054               ZC=zc; 
00055               Complete=true; 
00056               Negative=negative; 
00057               Original=original;
00058               try
00059               {      Delay=50;
00060                      Delay=new Double(delay).doubleValue();
00061               }
00062               catch (Exception ex) {}
00063               
00064               new Thread(this).start(); 
00065        }
00066        
00067        public void setInteractive (boolean flag)
00068        {      Interactive=flag; 
00069        }
00070        
00071        public boolean isAdmissible (ZirkelCanvas zc, ConstructionObject o)
00072        {      if ((o instanceof CircleObject) && 
00073                      ((CircleObject)o).getP2()==P) return true;
00074               if (zc.depends(o,P)) return false;
00075               return true;
00076        }
00077        
00078        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00079        {      double x=zc.x(e.getX()),y=zc.y(e.getY()); 
00080               if (P==null)
00081               {      P=zc.selectPoint(e.getX(),e.getY()); 
00082                      Complete=false; 
00083                      if (P!=null && P.moveable())
00084                      {      P.setSelected(true); 
00085                             zc.repaint(); 
00086                             showStatus(zc); 
00087                      }
00088               }
00089               else
00090               {      if (!Complete && Interactive)
00091                      {      ConstructionObject o=zc.selectWithSelector(e.getX(),e.getY(),this); 
00092                             if (o==null) return; 
00093                             boolean Have=(o==P); 
00094                             if (!Have)
00095                             {      Enumeration en=V.elements(); 
00096                                    while (en.hasMoreElements())
00097                                    {      if (o==en.nextElement())
00098                                           {      Have=true; break; 
00099                                           }
00100                                    }
00101                             }
00102                             if (!Have)
00103                             {      if (!((o instanceof SegmentObject) ||
00104                                    (o instanceof PrimitiveCircleObject) ||
00105                                           (o instanceof PointObject)
00106                                           ))
00107                                           return; 
00108                                    o.setSelected(true); 
00109                                    V.addElement(o); 
00110                                    zc.repaint(); 
00111                                    showStatus(zc); 
00112                                    return; 
00113                             }
00114                      }
00115                      if (!Running)
00116                      {      Stopped=false; 
00117                             ZC=zc;
00118                             zc.clearSelected(); 
00119                             Complete=true; 
00120                             zc.clearIndicated(); 
00121                             showStatus(zc);
00122                             new Thread(this).start(); 
00123                      }
00124                      else if (e.isShiftDown())
00125                      {      Negative=!Negative; 
00126                      }
00127                      else
00128                      {      Stopped=true; 
00129                      }
00130               }
00131        }
00132        
00133        public void mouseMoved (MouseEvent e, ZirkelCanvas zc, boolean simple)
00134        {      if (Complete || !Interactive) return; 
00135               if (P==null)
00136                      zc.indicatePointObjects(e.getX(),e.getY()); 
00137               else
00138                      zc.indicateObjects(e.getX(),e.getY()); 
00139        }
00140        
00141        public synchronized void reset (ZirkelCanvas zc)
00142        {      Stopped=true; Complete=false;
00143               super.reset(zc); 
00144               P=null; 
00145               Delay=50;
00146               V=new Vector(); showStatus(zc); zc.repaint(); 
00147        }
00148        
00149        public void showStatus (ZirkelCanvas zc)
00150        {      if (P==null) zc.showStatus(
00151                      Zirkel.name("message.animator.point")); 
00152               else if (!Complete) zc.showStatus(
00153                      Zirkel.name("message.animator.segment")); 
00154               else zc.showStatus(
00155                      Zirkel.name("message.animator.running")); 
00156        }
00157        
00158        public void save (XmlWriter xml)
00159        {      if (P==null) return; 
00160               xml.startTagStart("Animate"); 
00161               xml.printArg("animate",P.getName()); 
00162               int k=0; 
00163               Enumeration e=V.elements(); 
00164               while (e.hasMoreElements())
00165               {      String s=((ConstructionObject)e.nextElement()).getName(); 
00166                      xml.printArg("via"+k,s); 
00167                      k++; 
00168               }
00169               if (Negative) xml.printArg("negative","true"); 
00170               if (Delay!=50) xml.printArg("delay",""+Delay);
00171               xml.finishTagNewLine(); 
00172        }
00173        
00174        boolean Stopped=false; 
00175        
00176        public void run ()
00177        {      ZC.resetSum();
00178               if (Original) ZC.getConstruction().setOriginalOrder(true);
00179               Running=true; 
00180               showStatus(ZC); 
00181               Enumeration e=V.elements(); 
00182               double x=0.001; 
00183               boolean full=true; 
00184               ConstructionObject o=null; 
00185               long time=System.currentTimeMillis(); 
00186               long stoptime=time; 
00187               double end=2*Math.PI;
00188               boolean back=false;
00189               while (true)
00190               {      if (Stopped) break; 
00191                      try
00192                      {      long t=System.currentTimeMillis(); 
00193                             int h=(int)(t-time); 
00194                             if (h<0) h=0; 
00195                             if (h>Delay) h=(int)Delay; 
00196                             Thread.sleep((int)(Delay-h)); 
00197                             time=System.currentTimeMillis(); 
00198                      }
00199                      catch (Exception ex) {      }
00200                      if (Stopped) break; 
00201                      if (ZC.I==null) continue; 
00202                      synchronized (this)
00203                      {      if (full)
00204                             {      if (!e.hasMoreElements())
00205                                    {      e=V.elements(); 
00206                                    }
00207                                    o=(ConstructionObject)e.nextElement(); 
00208                                    full=false; 
00209                                    x=0.0001; 
00210                                    if (o instanceof SegmentObject)
00211                                           x=0.001*((SegmentObject)o).getLength(); 
00212                                    else if (o instanceof PrimitiveCircleObject)
00213                                    {      PrimitiveCircleObject c=(PrimitiveCircleObject)o;
00214                                           if (c.hasRange())
00215                                           {      c.computeA1A2();
00216                                                  x=c.getA1();
00217                                                  end=c.getA2();
00218                                                  if (end<x) end=end+2*Math.PI;
00219                                                  back=false;
00220                                           }
00221                                           else 
00222                                                  x=0.0001*c.getR(); 
00223                                    }
00224                                    else if (o instanceof PointObject)
00225                                    {      PointObject p=(PointObject)o; 
00226                                           synchronized (ZC)
00227                                           {      P.move(p.getX(),p.getY()); 
00228                                                  ZC.dovalidate(); 
00229                                           }
00230                                           ZC.repaint(); 
00231                                           stoptime=System.currentTimeMillis(); 
00232                                    }
00233                             }
00234                             if (o instanceof SegmentObject)
00235                             {      SegmentObject s=(SegmentObject)o;
00236                                    synchronized (ZC) 
00237                                    {      if (back)
00238                                                  P.move(s.getP1().getX()+(s.getLength()-x)*s.getDX(),
00239                                                         s.getP1().getY()+(s.getLength()-x)*s.getDY()); 
00240                                           else
00241                                                  P.move(s.getP1().getX()+x*s.getDX(),
00242                                                         s.getP1().getY()+x*s.getDY()); 
00243                                           ZC.dovalidate(); 
00244                                    }
00245                                    ZC.repaint(); 
00246                                    x+=ZC.dx(2); 
00247                                    if (x>0.99*s.getLength()) 
00248                                    {      if (Negative && !back)
00249                                           {      x=0.00001; back=true;
00250                                           }
00251                                           else
00252                                           {      back=false; full=true; 
00253                                           }
00254                                    }
00255                             }
00256                             else if (o instanceof PrimitiveCircleObject)
00257                             {      PrimitiveCircleObject c=(PrimitiveCircleObject)o; 
00258                                    if (c.getR()<1e-10)
00259                                    {      full=true; 
00260                                    }
00261                                    else
00262                                    {      synchronized (ZC)
00263                                           {      if (Negative && !c.hasRange())
00264                                                         P.move(c.getP1().getX()+Math.cos(x)*c.getR(),
00265                                                                c.getP1().getY()-Math.sin(x)*c.getR()); 
00266                                                  else
00267                                                         P.move(c.getP1().getX()+Math.cos(x)*c.getR(),
00268                                                                c.getP1().getY()+Math.sin(x)*c.getR()); 
00269                                                  ZC.dovalidate(); 
00270                                           }
00271                                           ZC.repaint(); 
00272                                           if (back) x-=ZC.dx(2)/c.getR(); 
00273                                           else x+=ZC.dx(2)/c.getR(); 
00274                                           if (x>end && !back)
00275                                           {      if (!Negative || back || !c.hasRange())
00276                                                  {      full=true; back=false;
00277                                                  }
00278                                                  else
00279                                                  {      back=true; end=c.getA1();
00280                                                  }
00281                                           } 
00282                                           if (back && x<end)
00283                                           {      full=true; back=false;
00284                                           }
00285                                    }
00286                             }
00287                             else if (o instanceof PointObject)
00288                             {      if (System.currentTimeMillis()-stoptime>5000)
00289                                    full=true; 
00290                             }
00291                      }
00292               }
00293               Running=false; 
00294               if (Original) ZC.getConstruction().setOriginalOrder(false);
00295        }
00296        
00297        public synchronized void invalidate (ZirkelCanvas zc)
00298        {      Stopped=true; 
00299        }
00300        
00301        public boolean useSmartBoard ()
00302        {      return false; 
00303        }
00304 
00305        public void increaseSpeed ()
00306        {      if (Delay>1) Delay=Delay/1.5;
00307        }
00308        
00309        public void decreaseSpeed ()
00310        {      if (Delay<500) Delay=Delay*1.5;
00311        }
00312 
00313 }