home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / util / localdatetime.java next >
Encoding:
Java Source  |  1996-08-14  |  5.7 KB  |  209 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)LocalDateTime.java  1.00 05-Mar-96
  15.  
  16.         Continuos time update class.
  17.  
  18.     Authors:
  19.  
  20.         jlee     James Lee
  21.  
  22.     Version Ident:
  23.  
  24.         $Header$
  25.  
  26.     History:
  27.  
  28.         1.00 05-Mar-96  jlee      Initial Creation
  29.              25-Mar-96  jlee      Added two more info item:free memory and total memory
  30.                                   Changed the super class from Label to Canvas
  31. ---------------------------------------------------------------------------*/
  32.  
  33. package pj.util;
  34.  
  35. import pj.awt.PjFinals;
  36.  
  37. import java.awt.Canvas;
  38. import java.awt.Event;
  39. import java.awt.Font;
  40. import java.awt.FontMetrics;
  41. import java.awt.Graphics;
  42. import java.awt.TextField;
  43. import java.lang.Thread;
  44. import java.lang.Runnable;
  45. import java.lang.Runtime;
  46. import java.util.Date;
  47.  
  48. /**
  49.  * Display the current time and date.
  50.  *
  51.  * @version 1.00 05-Mar-96
  52.  * @author James Lee
  53. */
  54. public class LocalDateTime extends Canvas implements Runnable
  55. {
  56.     // --- Instance variables
  57.  
  58.     private Thread      thDateTime;
  59.     private Runtime     rtRunime;
  60.     private Font        fntInfoBar = null;
  61.     private FontMetrics fmInfoBar = null;
  62.     private int         nTextHeight = 16;
  63.     private int         nTextAscent = 0;
  64.     private int         nType = 0;
  65.  
  66.  
  67.     // --- Public constructors
  68.  
  69.     public LocalDateTime()
  70.  
  71.         {
  72.         rtRunime = Runtime.getRuntime();
  73.  
  74.         thDateTime = new Thread( this );
  75.         thDateTime.setPriority( Thread.NORM_PRIORITY );
  76.  
  77.         thDateTime.start();
  78.         }
  79.  
  80.     // --- Public operations
  81.  
  82.     /**
  83.      * Run the loop.
  84.     */
  85.     public void run()
  86.         {
  87.         while(true)
  88.             {
  89.             System.gc();
  90.             repaint();
  91.  
  92.             try
  93.                 {
  94.                 Thread.sleep(5000);//5 second
  95.                 }
  96.             catch (InterruptedException e)
  97.                 {
  98.                 }
  99.             }
  100.         }
  101.  
  102.     public void paint( Graphics g )
  103.         {
  104.         if ( fmInfoBar == null )
  105.             {
  106.             g.setFont( PjFinals.fntInfoBar );
  107.             fmInfoBar = g.getFontMetrics();
  108.             if (fmInfoBar != null)
  109.                 {
  110.                 nTextHeight = fmInfoBar.getHeight();
  111.                 nTextAscent = fmInfoBar.getAscent();
  112.                 }
  113.             else
  114.                 System.out.println("Error-LocalDateTime-paint: getFontMetrics() returned null!");
  115.             }
  116.  
  117.         switch ( nType )
  118.             {
  119.             case 0: //date and time
  120.                 g.drawString( getDateTime(), 1, (size().height - nTextHeight) / 2 + nTextAscent );
  121.                 break;
  122.             case 1: //free memory
  123.                 g.drawString( "FreeMem:" + (rtRunime.freeMemory() / 1000) + "K", 1, (size().height - nTextHeight) / 2 + nTextAscent );
  124.                 break;
  125.             case 2: //free memory
  126.                 g.drawString( "TotalMem:" + (rtRunime.totalMemory() / 1000) + "K", 1, (size().height - nTextHeight) / 2 + nTextAscent );
  127.                 break;
  128.             default:
  129.                 break;
  130.             }
  131.         }
  132.  
  133.  
  134.     public boolean handleEvent(Event evt)
  135.         {
  136.         switch (evt.id)
  137.             {
  138.             case Event.MOUSE_DOWN:
  139.                 nType = (nType + 1) % 3;
  140.                 System.out.println("Debug-LocalDateTime-handleEvent:nType=" + nType);
  141.                 return true;;
  142.             default:
  143.                 break;
  144.             }
  145.         return false;
  146.         }
  147.  
  148.  
  149.     // --- Private operations
  150.  
  151.     private protected String getDateTime()
  152.         {
  153.         String strTime = new String();
  154.  
  155.         Date    date = new Date();
  156.         String  strMonth[] = {"Jan.","Feb.","Mar.","Apr.","May","June", "July","Aug.","Sep.","Oct.","Nov.","Dec."};
  157.  
  158.         String  strDD = new String();
  159.         String  strMM = new String();
  160.         String  strYY = new String();
  161.  
  162.         String  strMMDDYY = new String();
  163.         String  strTemp = new String();
  164.  
  165.         int     nMin = 0;
  166.         int     nTemp = 0;
  167.         boolean bIsPM = false;
  168.  
  169.         bIsPM = (date.getHours() >= 12) ? true: false;
  170.  
  171.         if( bIsPM )
  172.             {
  173.             nTemp = date.getHours();
  174.             strTime = (nTemp == 12) ? String.valueOf( 12 ): String.valueOf( date.getHours() - 12 );
  175.             }
  176.         else
  177.             {
  178.             nTemp = date.getHours();
  179.             strTime = (nTemp == 0) ? String.valueOf( 12 ): String.valueOf( nTemp );
  180.             }
  181.  
  182.         strTime = strTime.concat(":");
  183.  
  184.         nMin = date.getMinutes();
  185.  
  186.         if( nMin >= 10 )
  187.             strTime = strTime.concat( String.valueOf( nMin ) );
  188.         else
  189.             {
  190.             strTime = strTime.concat("0");
  191.             strTime = strTime.concat( String.valueOf( nMin ) );
  192.             }
  193.  
  194.         strTime = bIsPM ? strTime.concat(" p.m. "): strTime.concat(" a.m. ");
  195.  
  196.         strDD = String.valueOf(date.getDate());
  197.         strMM = strMonth[date.getMonth()];
  198.         strYY = String.valueOf( date.getYear() + 1900 );
  199.  
  200.         strTemp = strTemp.concat(" " + strMM + " " + strDD + ", " + strYY );
  201.         strTemp = strTime.concat(strTemp);
  202.  
  203.         return strTemp;
  204.         }// getDateTime
  205.  
  206.  
  207. } //LocalDateTime
  208.  
  209.