home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09962.iso / vrml / cp2b2x.exe / DATA.Z / clock.java < prev    next >
Text File  |  1996-06-21  |  4KB  |  161 lines

  1. // clock.java
  2. // 
  3. // (c)Copyright 1996 Sony Corporation. All rights reserved.
  4. //
  5. import vrml.*;
  6. import java.util.*;
  7.  
  8. public class clock extends Script{
  9.  
  10.     SFRotation setMin  = (SFRotation) getEventOut("setMin");
  11.     SFRotation setHour = (SFRotation) getEventOut("setHour");
  12.     SFRotation rotation = (SFRotation) getEventOut("rotation");
  13.  
  14.     float aRad = (float)(Math.PI/180.0);
  15.  
  16.     float clockRotation[] = {0.0f,1.0f,0.0f,0.0f} ;
  17.     float rotDgr = 0.0f ;
  18.     boolean rotating = false ;
  19.  
  20.     /* second parts*/
  21.     int secondsMax = 60;
  22.     SFColor seconds[] = new SFColor[secondsMax];
  23.     String secondsName[] = 
  24.       {"sec0","sec1","sec2","sec3","sec4","sec5", 
  25.      "sec6","sec7","sec8","sec9","sec10","sec11", 
  26.      "sec12","sec13","sec14","sec15","sec16","sec17", 
  27.      "sec18","sec19","sec20","sec21","sec22","sec23", 
  28.      "sec24","sec25","sec26","sec27","sec28","sec29", 
  29.      "sec30","sec31","sec32","sec33","sec34","sec35", 
  30.      "sec36","sec37","sec38","sec39","sec40","sec41", 
  31.      "sec42","sec43","sec44","sec45","sec46","sec47", 
  32.      "sec48","sec49","sec50","sec51","sec52","sec53", 
  33.      "sec54","sec55","sec56","sec57","sec58","sec59"} ;
  34.     float secColors[][] = ((MFColor)getField("secColors")).getValue();
  35.  
  36.     /* quarter min parts */
  37.     SFColor Ss[]=new SFColor[4];
  38.     String sName[] = { "s0","s15","s30","s45" };
  39.     float sColors[][] = ((MFColor)getField("sColors")).getValue();
  40.  
  41.     // rotation parameters.
  42.     float rotate[];
  43.     public int min, hour, sec;
  44.        
  45.     boolean doReset = true ; 
  46.     // constructor.
  47.     public clock(){
  48.     
  49.     int i ;
  50.     // get time
  51.     Date currentDate = new Date();
  52.     
  53.     /* get second panels */
  54.     for(i=0;i<secondsMax;i++){
  55.         seconds[i] = (SFColor) getEventOut(secondsName[i]);
  56.     }
  57.     for(i=0;i<4;i++){
  58.         Ss[i] = (SFColor) getEventOut(sName[i]);
  59.     }
  60.  
  61.         // rotation axle's direction.
  62.         rotate = new float[4];
  63.         rotate[0] = 0.0f;
  64.         rotate[1] = 0.0f;
  65.         rotate[2] = 1.0f;
  66.  
  67.         sec = currentDate.getSeconds();
  68.         min = currentDate.getMinutes();
  69.         hour = currentDate.getHours();
  70.         System.out.println( "min,hour:" + min + " " + hour);
  71.  
  72.     resetSec();    
  73.         // rotation axle's position.
  74.     setMin( min );
  75.     setHour( hour, min );
  76.  
  77.     }
  78.     
  79.     public void tick(ConstSFTime arg, ConstSFTime ts){
  80.     // get time
  81.     Date currentDate = new Date();
  82.  
  83.         sec = currentDate.getSeconds();
  84.         min = currentDate.getMinutes();
  85.         hour = currentDate.getHours();
  86.  
  87.     if ( sec == 0 ) {
  88.         // rotation axle's position.
  89.         setMin( min );
  90.         setHour( hour, min );
  91.  
  92.     }
  93.     setSec ( sec ) ;
  94.         if(rotating) {
  95.             rotDgr += 10.0f;
  96.             if (rotDgr > 360.0f) {
  97.                 rotDgr -= 360.0f;
  98.             }
  99.             clockRotation[3] = (aRad * rotDgr) ;
  100.             rotation.setValue(clockRotation);
  101.         }
  102.     }
  103.     void setMin ( int min ){
  104.         rotate[3] = (-6.0f * min)*aRad;
  105.         setMin.setValue(rotate);
  106.  
  107.     }
  108.     void setHour ( int hour, int min ) {
  109.         rotate[3] = ((-30.0f * hour) +(-30.0f *(min/60.0f))) * aRad;
  110.         setHour.setValue(rotate);
  111.     }
  112.     int last = 0 ;
  113.     void setSec(int sec){
  114.     if(last > sec){ 
  115.         resetSec();
  116.     }
  117.     for(int i=last;i<=sec;i++){
  118.         secColors[i][0]=1.0f;
  119.             seconds[i].setValue(secColors[i]);
  120.         if(i==0||i==15||i==30||i==45){
  121.                 int j = i/15;
  122.         sColors[j][0]=1.0f;
  123.         Ss[j].setValue(sColors[j]);
  124.         }
  125.     }
  126.         last = sec ;
  127.     }
  128.     void resetSec(){
  129.         int i ;
  130.     for(i=0;i<secondsMax;i++){
  131.         secColors[i][0]=0.0f;
  132.         secColors[i][2]=0.5f;
  133.         seconds[i].setValue(secColors[i]);
  134.     }
  135.     for(i=0;i<4;i++){
  136.         sColors[i][0]=0.0f;
  137.         sColors[i][2]=0.5f;
  138.         Ss[i].setValue(sColors[i]);
  139.     }
  140.         last = 0 ;
  141.     }
  142.  
  143.     public void clicked ( ConstSFBool arg, ConstSFTime now ) {
  144.         if ( arg.getValue() ) return ;
  145.         if (rotating) {
  146.             rotDgr = 0.0f;
  147.             clockRotation[3] = 0.0f;
  148.             rotation.setValue(clockRotation);
  149.             rotating = false ;
  150.         }         
  151.         else {
  152.             rotating = true ;
  153.         }
  154.         String s[] = new String[1] ;
  155.         s[0] = "html/clock.html";
  156.         Browser.loadWorld ( s ) ;
  157.     }
  158. }
  159.  
  160.  
  161.