//
//  DialTiros.java
//
//  DialTiros version 0.03
//
/*  

copyright 2009 UCA and prior, Alliance for the Advancement of Technology, all rights reserved.

redistribution and use in source and binary forms, with or without 
modification, is only permitted subject to conformance with 
'ICAS in use' and AAT ICAS licensing, and on the condition that any 
derivative code represent the use of 'ICAS in use' for any subsequent 
developer, and not the developer of this code.

the Uniform Calendar (UC), New Calendar (NC), and Inter-Dial Clock (IDC)
systems are part of the Integrated Chronological Applications System (ICAS).
AAT provides ICAS standards documents subject to terms of use described
in document AAT ICAS 9010. please refer to other key
AAT ICAS standards documents accessible via the AAT ICAS web site at
http://www.aatideas.org/icas for important information about ICAS.
AAT has made efforts to ensure the quality of ICAS resources;
however is not responsible for errors. AAT ICAS implementations of
ICAS including AppleScript and Java are not designed for fault tolerance
nor are intended for use in high-risk situations
calling for fault tolerant software/hardware systems.

redistribution and use is further subject to the terms for the sample 
Clock.java applet:

 * 
 * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * -Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 * 
 * -Redistribution in binary form must reproduct the above copyright
 *  notice, this list of conditions and the following disclaimer in
 *  the documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
 * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
 * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
 * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that Software is not designed, licensed or intended for
 * use in the design, construction, operation or maintenance of any nuclear
 * facility.

 * @(#)Clock.java	1.12 03/01/23
 * Time!
 *
 * @author Rachel Gollub
 * @modified Daniel Peek replaced circle drawing calculation, few more changes


DialTiros 0.01 was built UCN 12005X20 Orange IDC deka(SanJose) t692 by Ron Stone, 
and is no longer supported.

DialTiros 0.02 was built UCN 12006A99 IDC deka(SanJose) t999 by Ron Stone. 
Applet built with Xcode 2.2 for universal binaries. 
update introduced applet parameter for dial face image, 
and fixed screen-redraw bug in 0.01.

DialTiros 0.03 was built UCN 12009 V26 Yellow IDC (UT) t318 by Ron Stone.
comments were updated for UCN 12009.
formatting of first decitriad was increased relative size of second decitriad.


DialTiros 0.03 version reference:

     ICAS date and time (UT):

          ❀  UCN 12009 V26 Yellow  ❀
          ❀  UCA 2009 V26 Yellow  ❀
          ❀  IDC (UT) t318  tt495  ❀
          ❀  day of year 236  ❀
          ❀  AD 2009 August 24 Monday  ❀
          ❀  SMH (UT) 07:38:38  ❀

        built with 'ICAS now' open source:
             'http://www.aatideas.org/icas'
	   					
*/ 


import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;

public class DialTiros extends Applet implements Runnable {
    private volatile Thread timer;       // the thread that displays clock
    private int xnowTik, ynowTik, xnowTok,
                ynowTok, xnowTiros, ynowTiros;  // dimensions used to draw pointers 
	private float idc, maj, majinc, min, submin;
	private int d1, d2;
	private SimpleDateFormat formatter;  // formats the date displayed
    private String lastdate;             // string for computer's formatter date
    private String formDoY = "-";             // string for display of day of year
    private String formG = "-";             // string for display of Gregorian date
    private String formSMH = "-";             // string for display of SMH time
	private String decitFirst, decitSecond; // strings for decitriads
	private String displayFirst, displaySecond; // strings for display of decitriads
    private Font clockFaceFont;          // font for number display on clock
    private Font clockDeciFirstFont;          // font and size for number display of decitriad
    private Date currentDate;            // stringbuffer used for date for display
    private Color icasColor;           // color of ICAS text forms
    private Color customaryColor;           // color of customary text forms
    private Color majorColor;           // color of major unit pointer
    private Color minorColor;           // color of minor unit pointer
    private Color subminorColor;           // color of subminor unit pointer
	private Image dialFace;		// background image of dial face
    private int xcenter = 201, ycenter = 201; // center position

    public void init() {
        int x,y;
        xnowTik = ynowTik = xnowTok = ynowTok = xnowTiros = ynowTiros = 0;
        formatter = new SimpleDateFormat ("yyyy MMM dd EEE z HH:mm:ss.SSS DDD", 
                                          Locale.getDefault());
        currentDate = new Date();
        clockFaceFont = new Font("Serif", Font.PLAIN, 14);
        clockDeciFirstFont = new Font("Serif", Font.PLAIN, 24);
        lastdate = formatter.format(currentDate);
		displayFirst =  "t" + decitFirst;
		displaySecond =  "tt" + decitSecond;
        majorColor = Color.blue;
        minorColor = Color.green;
        subminorColor = Color.lightGray;
        icasColor = Color.darkGray;
        customaryColor = Color.lightGray;
		dialFace = getImage(getCodeBase(), "images/dialTiros-400-ab90.gif");


		try {
            setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
                                                     16)));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            dialFace = getImage(getCodeBase(), getParameter("dface1"));
        } catch (NullPointerException e) {
        }
        try {
            majorColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
                                                   16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            minorColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
                                                   16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            subminorColor = new Color(Integer.parseInt(getParameter("fgcolor3"),
                                                   16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            icasColor = new Color(Integer.parseInt(getParameter("fgcolor4"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        try {
            customaryColor = new Color(Integer.parseInt(getParameter("fgcolor5"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
        resize(401,401);              // set clock window size
    }

    // paint is the main part of the program
    public void update(Graphics g) {
        int xTiros, yTiros, xTok, yTok, xTik, yTik;
        float idc = 0, maj = 0, majinc = 0, min = 0, submin = 0;
        int s = 0, m = 0, h = 0, fs = 0;
        String today;

        g.drawImage(dialFace, 1, 1, this);
		currentDate = new Date();
		        
        formatter.applyPattern("S");
        try {
            fs = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            fs = 2;
        }
        formatter.applyPattern("s");
        try {
            s = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            s = 4;
        }
        formatter.applyPattern("m");
        try {
            m = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            m = 6;
        }    
        formatter.applyPattern("H");
        try {
			h = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
            h = 8;
        }
    
        // calculates the ICAS IDC time values
		idc = (float) ((h * 3600000) + (m * 60000) + (s * 1000) + fs)/86400000; 
				// is decimal fraction of day to the millisecond
		maj = (float) idc * 100; // phases continuously for calculating subunits
		majinc = (float) (int)((idc * 100) - (100 * (int)idc)); // phases incrementally for display
		min = (float) (int)((100 * maj) - (100 * (int)maj)); // phases incrementally (or quasi-incrementally)
		submin = (float) (int)((1000 * maj) - (1000 * (int)maj)); // phases incrementally
		d1 = (int) ((1000 * idc) - (1000 * (int)idc)) + 1000; //  + 1000 for proper decitFirst
		d2 = (int) ((1000000 * idc) - (1000000 * (int)idc) - ((d1 - 1000) * 1000)) + 1000; 
				//  corrects the + 1000 used for d1, then adds 1000 for proper decitSecond
		String decitFirst = Float.toString(d1);
		String decitSecond = Float.toString(d2);
		displayFirst =  "t " + decitFirst.substring(1, 4); // ignores the + 1000 for proper display
		displaySecond =  "tt " + decitSecond.substring(1, 4); // ignores the + 1000 for proper display

				
        // set position of the tips of the pointers
        xTik = (int) (Math.cos((submin * 3 / 5) * Math.PI / 30 - Math.PI / 2) * 180 + xcenter);
        yTik = (int) (Math.sin((submin * 3 / 5) * Math.PI / 30 - Math.PI / 2) * 180 + ycenter);
        xTok = (int) (Math.cos((min * 3 / 5) * Math.PI / 30 - Math.PI / 2) * 170 + xcenter);
        yTok = (int) (Math.sin((min * 3 / 5) * Math.PI / 30 - Math.PI / 2) * 170 + ycenter);
        xTiros = (int) (Math.cos((majinc * 3 / 5) * Math.PI / 30 - Math.PI / 2) * 180 + xcenter);
        yTiros = (int) (Math.sin((majinc * 3 / 5) * Math.PI / 30 - Math.PI / 2) * 180 + ycenter);
    
        // formats the date and time to print at the bottom
        formatter.applyPattern("yyyy MMM dd EEE z HH:mm:ss DDD");
        today = formatter.format(currentDate);
        formatter.applyPattern("DDD");
        formDoY = "DoY " + formatter.format(currentDate);
        formatter.applyPattern("yyyy MMM dd EEE");
        formG = formatter.format(currentDate);
        formatter.applyPattern("z HH:mm:ss");
        formSMH = "SMH " + formatter.format(currentDate);

        g.setFont(clockDeciFirstFont);

        // draw date and hands
        g.setColor(icasColor);
		g.drawString(displayFirst, 70, 20);
        g.setFont(clockFaceFont);
		g.drawString(displaySecond, 300, 20);
		g.drawString(formDoY, 5, 375);    
        g.setColor(customaryColor);
		g.drawString(formG, 5, 395);    
		g.drawString(formSMH, 280, 395);    
        g.setColor(subminorColor);
        g.drawLine(xcenter, ycenter, xTik, yTik);
        g.setColor(minorColor);
        g.drawLine(xcenter, ycenter-1, xTok, yTok);
        g.drawLine(xcenter, ycenter-2, xTok, yTok);
        g.drawLine(xcenter, ycenter-3, xTok, yTok);
        g.drawLine(xcenter-1, ycenter, xTok, yTok);
        g.drawLine(xcenter-2, ycenter, xTok, yTok);
        g.drawLine(xcenter-3, ycenter, xTok, yTok);
        g.setColor(majorColor);
        g.drawLine(xcenter, ycenter-5, xTiros, yTiros);
        g.drawLine(xcenter, ycenter-6, xTiros, yTiros);
        g.drawLine(xcenter, ycenter-7, xTiros, yTiros);
        g.drawLine(xcenter-5, ycenter, xTiros, yTiros);
        g.drawLine(xcenter-6, ycenter, xTiros, yTiros);
        g.drawLine(xcenter-7, ycenter, xTiros, yTiros);
        xnowTik = xTik; ynowTik = yTik; 
        xnowTok = xTok; ynowTok = yTok; 
        xnowTiros = xTiros; ynowTiros = yTiros; 
        lastdate = today;
		currentDate = null;
    }

    public void paint(Graphics g) {
        // draw the dial
		g.drawImage(dialFace, 1, 1, this);

        // draw date and pointers
        g.setFont(clockDeciFirstFont);
        g.setColor(icasColor);
		g.drawString(displayFirst, 70, 20);
        g.setFont(clockFaceFont);
		g.drawString(displaySecond, 300, 20);
		g.drawString(formDoY, 5, 375);    
        g.setColor(customaryColor);
		g.drawString(formG, 5, 395);    
		g.drawString(formSMH, 280, 395);    
        g.setColor(subminorColor);
        g.drawLine(xcenter, ycenter, xnowTik, ynowTik);
        g.setColor(minorColor);
        g.drawLine(xcenter, ycenter-1, xnowTok, ynowTok);
        g.drawLine(xcenter, ycenter-2, xnowTok, ynowTok);
        g.drawLine(xcenter, ycenter-3, xnowTok, ynowTok);
        g.drawLine(xcenter-1, ycenter, xnowTok, ynowTok);
        g.drawLine(xcenter-2, ycenter, xnowTok, ynowTok);
        g.drawLine(xcenter-3, ycenter, xnowTok, ynowTok);
        g.setColor(majorColor);
        g.drawLine(xcenter, ycenter-5, xnowTiros, ynowTiros);
        g.drawLine(xcenter, ycenter-6, xnowTiros, ynowTiros);
        g.drawLine(xcenter, ycenter-7, xnowTiros, ynowTiros);
        g.drawLine(xcenter-5, ycenter, xnowTiros, ynowTiros); 
        g.drawLine(xcenter-6, ycenter, xnowTiros, ynowTiros); 
        g.drawLine(xcenter-7, ycenter, xnowTiros, ynowTiros); 
    }

    public void start() {
        timer = new Thread(this);
        timer.start();
    }

    public void stop() {
        timer = null;
    }

    public void run() {
        Thread me = Thread.currentThread();
        while (timer == me) {
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException e) {
            }
            repaint();
        }
    }

    public String getAppletInfo() {
        return "Title: DialTiros 0.03, (ICAS-IDC) \n"
            + "Author: Ron Stone, 2009 UCA and prior \n"
            + "'ICAS in use' per www.aatideas.org";
    }
  
    public String[][] getParameterInfo() {
        String[][] info = {
            {"bgcolor", "hexadecimal RGB number", 
             "the background color. default is the color of your browser."},
            {"dface1", "image path and name", 
             "400 x 400 px. default is 'images/dialTiros-400-ab90.gif'."},
            {"fgcolor1", "hexadecimal RGB number", 
             "the color of the major unit pointer. default is blue."},
            {"fgcolor2", "hexadecimal RGB number", 
             "the color of minor unit pointer. default is green."},
            {"fgcolor3", "hexadecimal RGB number", 
             "the color of the subminor unit pointer. default is light gray."},
            {"fgcolor4", "hexadecimal RGB number", 
             "the color of the ICAS text forms. default is dark gray."},
            {"fgcolor5", "hexadecimal RGB number", 
             "the color of the customary text forms. default is light gray."}
        };
        return info;
    }
}

