home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 February / PCO_0298.ISO / filesbbs / os2 / ndoc091.arj / NDOC091.ZIP / html / sample / WordClock.nrx < prev   
Encoding:
Text File  |  1997-11-03  |  5.0 KB  |  126 lines

  1. /** WordClock -- an applet that shows the time in English.     
  2. *                                                            
  3. * Based on the ancient QTIME.REXX, and typical Java applets. 
  4. * @param face the font face to use                           
  5. * @param size the font size to use                           
  6. *                                                            
  7. * @author Mike Cowlishaw,  December 1979 - January 1985. */
  8.  
  9. class WordClock extends Applet implements Runnable
  10.  
  11.  timer=Thread null                        /* the timer thread */
  12.  offsetx; offsety                            /* text position */
  13.  now                                          /* current time */
  14.  
  15.  /** Initialize the applet*/
  16.  method init
  17.   /* Get parameters from the <applet> markup */
  18.   face=getParameter("face")                      /* font face */
  19.   if face=null then face="TimesRoman"
  20.   size=getParameter("size")
  21.   if size=null then size="20"                    /* font size */
  22.  
  23.   setFont(Font(face, Font.BOLD, size))
  24.   resize(size*20, size*2)                  /* set window size */
  25.   offsetx=size/2                 /* and where text will start */
  26.   offsety=size*3/2                      /* note Y is from top */
  27.   parse Date() . . . now .     /* initial time is fourth word */
  28.  
  29.  /**
  30.  * Applet start: start the Thread
  31.  */
  32.  method start
  33.   if timer=null then timer=Thread(this)         /* new thread */
  34.   timer.setPriority(Thread.MAX_PRIORITY)      /* time matters */
  35.   timer.start                             /* start the thread */
  36.  
  37.  /** 
  38.  * Applet stopped: stop the timer
  39.  */
  40.  method stop
  41.   if timer\=null then do                       /* have thread */
  42.     timer.stop                               /* .. so stop it */
  43.     timer=null                              /* .. and discard */
  44.     end
  45.  
  46.  /** Timer: repaint every second 
  47.  */
  48.  method run
  49.   /* Use the Java Date class to get the time */
  50.   loop while timer\=null
  51.     parse Date() . . . now .           /* time is fourth word */
  52.     this.repaint                                 /* redisplay */
  53.     parse now ':' ':'secs                  /* where in minute */
  54.     wait=30-secs                /* calculate delay in seconds */
  55.     if wait<=0 then wait=wait+60
  56.     /* say 'secs, wait:' secs wait */
  57.     Thread.sleep(1000*wait)          /* wait for milliseconds */
  58.   catch InterruptedException
  59.     say 'Interrupted...'
  60.   end
  61.   timer=null                                          /* done */
  62.  
  63.  /** paint the applet 
  64.  * @param g Graphics contents for painting
  65.  */
  66.  method paint(g=Graphics)
  67.   g.drawString(wordtime(now), offsetx, offsety)    /* show it */
  68.  
  69.  /** WORDTIME -- a cut-down version of QTIME.REXX
  70.   @param Arg1 is the time string (hh:mm:ss)
  71.   @return the time in english, as a Rexx string
  72.  */
  73.  method wordtime(arg) static returns Rexx
  74.   /* Extract the hours, minutes, and seconds from the time.   */
  75.   parse arg hour':'min':'sec
  76.   if sec>29 then min=min+1                /* round up minutes */
  77.  
  78.   /* Nearness phrases - this time using an array              */
  79.   near=Rexx[5]                                  /* five items */
  80.   near[0]=''                                         /* exact */
  81.   near[1]=' just gone';  near[2]=' just after'       /* after */
  82.   near[3]=' nearly';     near[4]=' almost'          /* before */
  83.  
  84.   mod=min//5              /* where we are in 5 minute bracket */
  85.   out="It's"near[mod]            /* start building the result */
  86.   if min>32 then hour=hour+1         /* we are TO the hour... */
  87.   min=min+2     /* shift minutes to straddle a 5-minute point */
  88.  
  89.   /* Now special-case the result for Noon and Midnight hours  */
  90.   if hour//12=0 & min//60<=4 then do
  91.     if hour=12 then return out 'Noon.'
  92.     return 'Midnight.'
  93.     end
  94.  
  95.   min=min-(min//5)                     /* find nearest 5 mins */
  96.   if hour>12
  97.    then hour=hour-12              /* get rid of 24-hour clock */
  98.    else
  99.     if hour=0 then hour=12       /* .. and allow for midnight */
  100.  
  101.   /* Determine the phrase to use for each 5-minute segment    */
  102.   select
  103.     when min=0  then nop               /* add "o'clock" later */
  104.     when min=60 then min=0                           /* ditto */
  105.     when min= 5 then out=out 'five past'
  106.     when min=10 then out=out 'ten past'
  107.     when min=15 then out=out 'a quarter past'
  108.     when min=20 then out=out 'twenty past'
  109.     when min=25 then out=out 'twenty-five past'
  110.     when min=30 then out=out 'half past'
  111.     when min=35 then out=out 'twenty-five to'
  112.     when min=40 then out=out 'twenty to'
  113.     when min=45 then out=out 'a quarter to'
  114.     when min=50 then out=out 'ten to'
  115.     when min=55 then out=out 'five to'
  116.     end
  117.  
  118.   numbers='one two three four five six'-      /* continuation */
  119.     'seven eight nine ten eleven twelve '
  120.   out=out numbers.word(hour)           /* add the hour number */
  121.   if min=0 then out=out "o'clock"  /* .. and o'clock if exact */
  122.  
  123.   return out'.'                    /* return the final result */
  124.  
  125. /* NetRexx version March 1996; applet April 1996. */
  126.