// Analog.java: this applet is a remake of the Java demo that comes with the JDK
// @author: Stefan v/d Linden
// @copyright: (c)2006 Stefan v/d Linden, ALL RIGHTS RESERVED
// A lot of thanks to the Java creators

// Analog v 1.0.0

// Import the packages that are needed in this applet
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.text.*;

//The main class
public class Analog extends Applet implements Runnable, MouseListener
{
 // Initialize all the variables
 SimpleDateFormat f_s = new SimpleDateFormat ("ss", Locale.getDefault());
 SimpleDateFormat f_m = new SimpleDateFormat ("mm", Locale.getDefault());
 SimpleDateFormat f_h = new SimpleDateFormat ("hh", Locale.getDefault());
 SimpleDateFormat standard = new SimpleDateFormat ("HH:mm:ss", Locale.getDefault());

 int xs, ys, xm, ym, xh, yh, s, xcenter, ycenter, m, h, x, y, mouse_x, mouse_y;
 String temp; 
 Dimension d;
 Date now;
 Thread thr = null;
 Image im;
 Boolean M = false;
 Graphics gIm;
 Dimension dIm;
 int sx, sy, sx2, sy2; 
 Color digital = Color.blue, hour = Color.blue, minute = Color.yellow,
 second = Color.green, circle = Color.red, hours = Color.blue, mute_logo = Color.green;

 // start() handles everything when the applet starts
 public void start()
 {
 // Create a new thread
 if(thr == null) {
 thr = new Thread(this);
 thr.start();
 }
 }
 
 // init() handles everything when the applet get's initialized
 public void init()
 {
 if(getParameter("sound") == "off")
 {
 M = true;
 } else {
 M = false;
 }

 addMouseListener(this);
 setBackground(Color.lightGray);
 try {
            setBackground(new Color(Integer.parseInt(getParameter("bg"),
                                                     16)));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
try {
            digital = new Color(Integer.parseInt(getParameter("digital"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
try {
            mute_logo = new Color(Integer.parseInt(getParameter("mute"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }

try {
            hour = new Color(Integer.parseInt(getParameter("hour"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
try {
            minute = new Color(Integer.parseInt(getParameter("minute"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
try {
            second = new Color(Integer.parseInt(getParameter("second"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
try {
            circle = new Color(Integer.parseInt(getParameter("circle"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }
try {
             hours = new Color(Integer.parseInt(getParameter("hours"),
                                                     16));
        } catch (NullPointerException e) {
        } catch (NumberFormatException e) {
        }

 }

 // stop() handles everything when the applet stops
 public void stop()
 {
 thr = null;
 }

 public void run()
 {
 while(thr != null)
 {
 try{
 // Refresh the clock
 repaint();
 // Sleep for a second
 Thread.sleep(1000);
 }catch(InterruptedException e) {}
 }
 }

 // paint(): the main part of the program
 public void paint(Graphics g)
 {
 d = getSize();
 xcenter = d.width/2;
 ycenter = d.height/2;
 x = d.width;
 y = d.height;

 if(im == null)
 {
 im = createImage(x, y);
 gIm = im.getGraphics();
 dIm = new Dimension(x, y);
 }
 
 // Get the current timestamp
 now = new Date();
 
 // Get the seconds
 temp = f_s.format(now);
 s = Integer.parseInt(temp); 
 
 temp = f_m.format(now);
 m = Integer.parseInt(temp); 

 temp = f_h.format(now);
 h = Integer.parseInt(temp); 

 
 // Calculate all the positions of the hands
 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
 ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
 xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
 ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
 xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xcenter);
 yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + ycenter); 

 

 // Refresh the image
 gIm.setColor(getBackground());
 gIm.fillRect(0, 0, dIm.width, dIm.height);

 // Draw the circle of the clock
 gIm.setColor(circle);
 gIm.drawOval(0, 0, d.width-1, d.height-1); 
 
 gIm.setColor(hours); 

 // Draw the stripes of the hours
 for(int i = 0;i<12;i++)
 {
 sx  = (int) (Math.cos((i * 30) * Math.PI / 180 - Math.PI / 2) * 47 + xcenter);
 sy  = (int) (Math.sin((i * 30) * Math.PI / 180 - Math.PI / 2) * 47 + xcenter);
 sx2 = (int) (Math.cos((i * 30) * Math.PI / 180 - Math.PI / 2) * 49 + xcenter);
 sy2 = (int) (Math.sin((i * 30) * Math.PI / 180 - Math.PI / 2) * 49 + xcenter);
 gIm.drawLine(sx, sy, sx2, sy2);
 }

 //  Draw the time on the top of the clock
 gIm.setColor(digital);
 gIm.drawString(standard.format(now), 25, 30);
 
 // Draw the mute logo
 Mute m = new Mute(M, gIm);

 // Draw the hands

 // Seconds
 gIm.setColor(second);
 gIm.drawLine(xcenter, ycenter, xs, ys);
 
 // Minutes
 gIm.setColor(minute);
 gIm.drawLine(xcenter, ycenter-1, xm, ym);
 gIm.drawLine(xcenter-1, ycenter, xm, ym);

 // Hour
 gIm.setColor(hour);
 gIm.drawLine(xcenter, ycenter-1, xh, yh);
 gIm.drawLine(xcenter-1, ycenter, xh, yh);
 
 

 // Paint the generated image on the screen
 if(im != null) 
 {
 g.drawImage(im, 0, 0, null);
 
 if(M == false)
 play(getCodeBase(), "Sound/Click.wav");
 
 }
 }

 public void update(Graphics g)
 {
 paint(g);
 }

 class Mute
 {
 Mute(Boolean mute, Graphics g)
 {
 g.setColor(mute_logo);
 Polygon p = new Polygon();
 p.addPoint(1, 7);
 p.addPoint(6, 2);
 p.addPoint(6, 12);
 g.fillPolygon(p);
 g.drawLine(7, 5, 7, 9);
 g.drawLine(9, 4, 9, 10);
 g.drawLine(11, 3, 11, 11);

 if(mute == true)
 g.drawLine(13, 3, 1, 11);
 

 }
 }
 
 // All the mouse events
 public void mousePressed(MouseEvent evt)
   {  
      mouse_x = evt.getX();
      mouse_y = evt.getY();
      if(mouse_x <= 11 && mouse_x >= 1 && mouse_y <= 12 && mouse_y >= 2)
      {
      if(M == true)
      {
      M = false;
      } else {
      M = true;
      }
      } 
      repaint();
   }
   public void mouseReleased(MouseEvent evt){}
   public void mouseEntered(MouseEvent evt){}
   public void mouseExited(MouseEvent evt){}
   public void mouseClicked(MouseEvent evt){}


}