home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.7 KB | 209 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- @(#)LocalDateTime.java 1.00 05-Mar-96
-
- Continuos time update class.
-
- Authors:
-
- jlee James Lee
-
- Version Ident:
-
- $Header$
-
- History:
-
- 1.00 05-Mar-96 jlee Initial Creation
- 25-Mar-96 jlee Added two more info item:free memory and total memory
- Changed the super class from Label to Canvas
- ---------------------------------------------------------------------------*/
-
- package pj.util;
-
- import pj.awt.PjFinals;
-
- import java.awt.Canvas;
- import java.awt.Event;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.TextField;
- import java.lang.Thread;
- import java.lang.Runnable;
- import java.lang.Runtime;
- import java.util.Date;
-
- /**
- * Display the current time and date.
- *
- * @version 1.00 05-Mar-96
- * @author James Lee
- */
- public class LocalDateTime extends Canvas implements Runnable
- {
- // --- Instance variables
-
- private Thread thDateTime;
- private Runtime rtRunime;
- private Font fntInfoBar = null;
- private FontMetrics fmInfoBar = null;
- private int nTextHeight = 16;
- private int nTextAscent = 0;
- private int nType = 0;
-
-
- // --- Public constructors
-
- public LocalDateTime()
-
- {
- rtRunime = Runtime.getRuntime();
-
- thDateTime = new Thread( this );
- thDateTime.setPriority( Thread.NORM_PRIORITY );
-
- thDateTime.start();
- }
-
- // --- Public operations
-
- /**
- * Run the loop.
- */
- public void run()
- {
- while(true)
- {
- System.gc();
- repaint();
-
- try
- {
- Thread.sleep(5000);//5 second
- }
- catch (InterruptedException e)
- {
- }
- }
- }
-
- public void paint( Graphics g )
- {
- if ( fmInfoBar == null )
- {
- g.setFont( PjFinals.fntInfoBar );
- fmInfoBar = g.getFontMetrics();
- if (fmInfoBar != null)
- {
- nTextHeight = fmInfoBar.getHeight();
- nTextAscent = fmInfoBar.getAscent();
- }
- else
- System.out.println("Error-LocalDateTime-paint: getFontMetrics() returned null!");
- }
-
- switch ( nType )
- {
- case 0: //date and time
- g.drawString( getDateTime(), 1, (size().height - nTextHeight) / 2 + nTextAscent );
- break;
- case 1: //free memory
- g.drawString( "FreeMem:" + (rtRunime.freeMemory() / 1000) + "K", 1, (size().height - nTextHeight) / 2 + nTextAscent );
- break;
- case 2: //free memory
- g.drawString( "TotalMem:" + (rtRunime.totalMemory() / 1000) + "K", 1, (size().height - nTextHeight) / 2 + nTextAscent );
- break;
- default:
- break;
- }
- }
-
-
- public boolean handleEvent(Event evt)
- {
- switch (evt.id)
- {
- case Event.MOUSE_DOWN:
- nType = (nType + 1) % 3;
- System.out.println("Debug-LocalDateTime-handleEvent:nType=" + nType);
- return true;;
- default:
- break;
- }
- return false;
- }
-
-
- // --- Private operations
-
- private protected String getDateTime()
- {
- String strTime = new String();
-
- Date date = new Date();
- String strMonth[] = {"Jan.","Feb.","Mar.","Apr.","May","June", "July","Aug.","Sep.","Oct.","Nov.","Dec."};
-
- String strDD = new String();
- String strMM = new String();
- String strYY = new String();
-
- String strMMDDYY = new String();
- String strTemp = new String();
-
- int nMin = 0;
- int nTemp = 0;
- boolean bIsPM = false;
-
- bIsPM = (date.getHours() >= 12) ? true: false;
-
- if( bIsPM )
- {
- nTemp = date.getHours();
- strTime = (nTemp == 12) ? String.valueOf( 12 ): String.valueOf( date.getHours() - 12 );
- }
- else
- {
- nTemp = date.getHours();
- strTime = (nTemp == 0) ? String.valueOf( 12 ): String.valueOf( nTemp );
- }
-
- strTime = strTime.concat(":");
-
- nMin = date.getMinutes();
-
- if( nMin >= 10 )
- strTime = strTime.concat( String.valueOf( nMin ) );
- else
- {
- strTime = strTime.concat("0");
- strTime = strTime.concat( String.valueOf( nMin ) );
- }
-
- strTime = bIsPM ? strTime.concat(" p.m. "): strTime.concat(" a.m. ");
-
- strDD = String.valueOf(date.getDate());
- strMM = strMonth[date.getMonth()];
- strYY = String.valueOf( date.getYear() + 1900 );
-
- strTemp = strTemp.concat(" " + strMM + " " + strDD + ", " + strYY );
- strTemp = strTime.concat(strTemp);
-
- return strTemp;
- }// getDateTime
-
-
- } //LocalDateTime
-
-