
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class Frame1 extends JFrame
{
  Relogio relogio = new Relogio();
  Thread relogio1 = new Thread(relogio);
  GridLayout gridLayout1 = new GridLayout();
  GridLayout gridLayout2 = new GridLayout();
  JPanel jPanel1 = new JPanel();

  public Frame1() {
    super();
    try  {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception
  {
    this.getContentPane().setLayout(gridLayout1);
    this.setSize(new Dimension(400, 340));
    this.setTitle("Relógio em Java - GenialSoftware Corporation...");
    this.getContentPane().add(jPanel1, null);
    jPanel1.setLayout(gridLayout2);
    jPanel1.add(relogio);
    relogio1.start();
  }
}

class Relogio extends JPanel implements Runnable
{
  public void run()
  {
    Thread antiga = Thread.currentThread();
    Thread atual = Thread.currentThread();
    while (antiga == atual)
    {
    try {
        antiga.sleep(100);
        }
    catch (InterruptedException e)
    {
    }
    repaint();
    }
 }
  public void paint(Graphics g)
  {
    int ang,segux,seguy,minux,minuy,horax,horay,s,m,h;
    String horagora;
    double tempx,tempy;

    Date DataAtual = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
    try
    {
      s = Integer.parseInt(formatter.format(DataAtual));
    }
    catch (NumberFormatException n)
    {
      s = 0;
    }
    formatter.applyPattern("m");
    try
    {
      m = Integer.parseInt(formatter.format(DataAtual));
    }
    catch (NumberFormatException n)
    {
      m = 10;
    }
    formatter.applyPattern("h");
    try
    {
       h = Integer.parseInt(formatter.format(DataAtual));
    }
    catch (NumberFormatException n)
    {
       h = 10;
    }
    formatter.applyPattern("HH:mm:ss");
    horagora = formatter.format(DataAtual);
    g.clearRect(0,0,399,339);
    //--------------------------------------------------------
    //Calcula a posição do ponteiro de segundo!
    ang = s*6; //Cada segundo tem 6º de deslocamento angular
    ang -= 90; //O 0º do Relogio seria no numero 12 que é 90º,
               //então subtraindo 90 tornaremos ambos iguais!
    tempx = ang * (3.1415)/180;
    tempy = ang * (3.1415)/180;
    segux = (int)(Math.cos(tempx) * 100);
    seguy = (int)(Math.sin(tempy) * 100);
    g.setColor(Color.green); //Desenha a nova posição
    g.drawLine(200,150,segux + 200, seguy + 150);
    //--------------------------------------------------------
    //Calcula a posição do ponteiro de minuto!
    ang = m*6; //Idem ao de segundos
    ang -= 90; //Idem ao de segundos
    tempx = ang * (3.1415)/180;
    tempy = ang * (3.1415)/180;
    minux = (int)(Math.cos(tempx) * 90);
    minuy = (int)(Math.sin(tempy) * 90);
    g.setColor(Color.yellow);
    g.drawLine(200,150, minux + 200, minuy + 150);
    //--------------------------------------------------------
    //Calcula a posição do ponteiro de horas!
    ang = (h*30)+(6*(m/12));//Cada hora tem 30º,
                            //mas a cada 12min devemos acrescentar 6º ao ponteiro
    ang -= 90; //Idem ao de segundos
    tempx = ang * (3.1415)/180;
    tempy = ang * (3.1415)/180;
    horax = (int)(Math.cos(tempx) * 80);
    horay = (int)(Math.sin(tempy) * 80);
    g.setColor(Color.red);
    g.drawLine(200,150,horax + 200, horay + 150);
    //--------------------------------------------------------
    g.setColor(Color.blue);
    g.drawArc(100,50,200,200,0,360);
    g.fillArc(198,148,4,4,0,360);
    g.drawString("12",193,65);
    g.drawString("1",243,75);
    g.drawString("2",277,110);
    g.drawString("3",290,155);
    g.drawString("4",277,200);
    g.drawString("5",243,235);
    g.drawString("6",197,245);
    g.drawString("7",153,235);
    g.drawString("8",117,200);
    g.drawString("9",105,155);
    g.drawString("10",117,110);
    g.drawString("11",150,77);
    g.drawString(horagora,175,200);
    g.setColor(Color.white);
    g.drawString("Relógio Java - Alan Carvalho de Assis",100,25);
  }
}
