/** * This is class implements a clock, which can display time is various * formats. Specifically it displays Greenwich Mean Time (UTC), * local time (e.g. EST), or the local sidereal time, based on your * longitude. * Note that this class uses some of the depreciated interfaces to the * java.util.Date class. * * @version $Id: jlstclk.java,v 3.10 2003/09/25 15:56:17 jbrandt Exp jbrandt $ * @author Joe Brandt, National Radio Astronomy Observatory, jbrandt@nrao.edu * @see http://www.gb.nrao.edu/~jbrandt * */ /** * This class can be run either as an application, or as an applet, although * the latter is not well tested. * Available parameters are: * lstfmt : A @see DateFormat style string, defaults to "HH:mm:ss 'LST'". * If yyyyy is specified, the Modified Julian Day is displayed. * utcfmt : Same as above, for UTC time display * localfmt: Same as above for local time display * longitude: A decimal and fraction of the degrees West of Greenwich, England. * font : A name of the desired font. Defaults to "TimesRoman". * mode : The initial time display mode. Defaults to "lst". * fontsize : The font size number (e.g. 12, 14 etc.) * foregroundcolor : A color name for the text display * backgroundcolor : The background color name * */ import java.awt.*; import java.awt.event.*; import java.util.Date; import java.util.TimeZone; import java.text.*; public class jlstclk extends java.applet.Applet implements Runnable, MouseListener, KeyListener, ActionListener { Font theFont; Date theDate; Date lstDate; Double longitude; double longitudeRads; Thread runner; int xposition; int yposition; String xcoord; String ycoord; String localZone; Image offscreenImage; Graphics offscreenGraphics; static boolean initialzed = false; String mainArgs[]; static boolean inAnApplet = true; Frame f; SimpleDateFormat utcFormat; SimpleDateFormat lstFormat; SimpleDateFormat localFormat; String longitudeStr; String utcfmtStr; String lstfmtStr; String localfmtStr; String modeStr; String bgcolor; String fgcolor; String theFontName; String fontSizeStr; FontMetrics fontMetrics; int displayMode = 1; int curWidth = 0; int curHeight = 0; int theFontSize; PopupMenu menu; public jlstclk() { // sane defaults lstfmtStr=" HH:mm:ss 'LST' "; utcfmtStr=" HH:mm:ss 'UTC' "; localfmtStr=" HH:mm:ss "; // inAnApplet = true; displayMode = 1; modeStr = "lst"; curWidth = 0; curHeight = 0; } public void init() { addMouseListener(this); addKeyListener(this); // Set the background and foreground colors if(inAnApplet) { bgcolor = getParameter("backgroundcolor"); fgcolor = getParameter("foregroundcolor"); modeStr = getParameter("mode"); lstfmtStr=getParameter("lstfmt"); if(lstfmtStr == null) lstfmtStr=" HH:mm:ss 'LST' "; utcfmtStr=getParameter("utcfmt"); if(utcfmtStr == null) utcfmtStr=" HH:mm:ss 'UTC' "; localfmtStr=getParameter("localfmt"); if(localfmtStr == null) localfmtStr=" HH:mm:ss "; } else { bgcolor="white"; fgcolor="red"; } if(modeStr == null) { displayMode=1; } else if(modeStr.equals("lst")) { displayMode=1; } else if(modeStr.equals("utc")) { displayMode=2; } else if(modeStr.equals("local")) { displayMode=3; } else displayMode=1; utcFormat = new SimpleDateFormat(utcfmtStr); utcFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // DEBUG utcFormat.setTimeZone(TimeZone.getDefault()); // Set the LST one to the local zone, so no offset are made lstFormat = new SimpleDateFormat(lstfmtStr); lstFormat.setTimeZone(TimeZone.getDefault()); localFormat = new SimpleDateFormat(localfmtStr); localFormat.setTimeZone(TimeZone.getDefault()); // setup defaults setBackground(Color.white); setForeground(Color.black); if (bgcolor.equals("white")) setBackground(Color.white); if (bgcolor.equals("black")) setBackground(Color.black); if (bgcolor.equals("red")) setBackground(Color.red); if (bgcolor.equals("green")) setBackground(Color.green); if (bgcolor.equals("blue")) setBackground(Color.blue); if (bgcolor.equals("yellow")) setBackground(Color.yellow); if (fgcolor.equals("white")) setForeground(Color.white); if (fgcolor.equals("black")) setForeground(Color.black); if (fgcolor.equals("red")) setForeground(Color.red); if (fgcolor.equals("green")) setForeground(Color.green); if (fgcolor.equals("blue")) setForeground(Color.blue); if (fgcolor.equals("yellow")) setForeground(Color.yellow); // Get font info from the APPLET Tag and assign if(inAnApplet) { theFontName = getParameter("fontname"); } if (theFontName == null) theFontName = "TimesRoman"; if(inAnApplet) fontSizeStr = getParameter("fontsize"); if (fontSizeStr == null) theFontSize = 12; else theFontSize = Integer.parseInt(fontSizeStr); theFont = new Font(theFontName, Font.BOLD, theFontSize); // Get x y coordinate info from applet tag and assign if(inAnApplet) { xcoord = getParameter("xcoord"); ycoord = getParameter("ycoord"); } else { xcoord = "center"; ycoord = "center"; } // Get longitude for LST calculations and convert to radians if(inAnApplet) { longitudeStr = getParameter("longitude"); } if(longitudeStr != null) { longitude = Double.valueOf(longitudeStr); } else { longitude = Double.valueOf("0.0"); } longitudeRads = Math.PI*longitude.doubleValue()/180.0; // Get the local Time zone string localZone = TimeZone.getDefault().getID(); if(localZone == null) localZone = " "; fontMetrics = getFontMetrics(theFont); int tWidth1 = 0, tWidth2=0, tWidth3=0; int tHeight = fontMetrics.getHeight(); if(utcfmtStr != null) tWidth1 = fontMetrics.stringWidth(utcfmtStr); if(lstfmtStr != null) tWidth2 = fontMetrics.stringWidth(lstfmtStr); if(localfmtStr != null) tWidth3 = fontMetrics.stringWidth(localfmtStr); tWidth1=Math.max(tWidth1,Math.max(tWidth2, tWidth3)); tWidth1 += 50; tHeight += 30; // System.out.println("wxH " + Integer.toString(tWidth1) + " " + Integer.toString(tHeight)); doResize(tWidth1, tHeight); setVisible(true); if(!inAnApplet) { menu = new PopupMenu(); menu.add("Display LST"); menu.add("Display UTC"); menu.add("Display Local"); menu.add("Exit"); f.add(menu); menu.addActionListener(this); menu.addNotify(); } initialzed = true; } public static void main(String[] argv) { jlstclk.inAnApplet = false; jlstclk v = new jlstclk(); v.mainArgs = argv; v.f = new Frame("jLSTclock $Revision: 3.10 $"); v.f.add("Center", v); v.f.setSize(200,100); v.f.setVisible(true); int i; // Parse command line options for(i=0; i 2) theFontSize -= 2; theFont = new Font(theFontName, Font.BOLD, theFontSize); if(theFont == null) { theFontSize -= 2; theFont = new Font(theFontName, Font.BOLD, theFontSize); } fontMetrics = getFontMetrics(theFont); offscreenGraphics.setFont(theFont); break; default: break; } } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} // Return local siderial time in radians public Date lmst(Date localDate, double observerLongitude) { double tu; double gmst; double LST; double ut1; double dmjd; long imjd; int offset; int hours, day, month, year; Date lstTime; // Get correction in minutes between local and GMT time // (e.g. calculate UTC from Civil time) offset = localDate.getTimezoneOffset(); imjd = mjd(localDate); // get ut1 (really UTC) in turns ut1 = (localDate.getSeconds() + (localDate.getMinutes() + offset) * 60 + localDate.getHours()*3600)/86400.0; // If ut rolled over, means UTC on next day if(ut1 > 1.0) { ut1 -= 1.0; imjd += 1; } dmjd = (double)imjd + ut1; lstTime = localDate; // Julian centuries from fundamental epoch J2000 to this UT tu = (dmjd - 51544.5) / 36525.0; // tu is in turns // GMST at this UT in radians gmst = ( ut1 * (2.0*Math.PI) + ( 24110.54841 + ( 8640184.812866 + ( 0.093104 - ( 6.2e-6 * tu ) ) * tu ) * tu ) * Math.PI/43200.0 ); // correct for observer longitude LST = gmst - observerLongitude; // assure a positive answer, between 0 and 2PI while(LST < 0.0) { LST += 2.0*Math.PI; } while(LST > 2.0*Math.PI) { LST -= 2.0*Math.PI; } // Convert from radians to seconds LST = LST * 43200.0/Math.PI; lstTime.setSeconds((int)LST%60); lstTime.setMinutes(((int)LST/60)%60); lstTime.setHours(((int)LST/3600)); // A true hack but it is useful. // We use the year to hold the MJD value, but the formatter // will add 1900 to it before display, so offset it here lstTime.setYear((int)imjd - 1900); return(lstTime); } public long mjd(Date date) { long year; long month; long day; long jy, jm, ja, jday; // getMonth range is 0..11 month = 1 + date.getMonth(); day = date.getDate(); year = date.getYear() + 1900; jy = year; // Garbage in, garbage out ... if (jy == 0) return(0); if (jy < 0) ++jy; if (month > 2) { jm=month+1; } else { --jy; jm=month+13; } jday = (long)(Math.floor(365.25*jy)+Math.floor(30.6001*jm)+day+1720995); // switch over to gregorian calendar if (day+31L*(month+12L*year) >= (15+31L*(10+12L*1582))) { ja=(long)(0.01*jy); jday += 2-ja+(long) (0.25*ja); } return(jday-2400001); } // End class jlstclk }