¿Cómo utilizar la programación JAVA para implementar un reloj analógico que funcione dinámicamente y en tiempo real?
importar java.awt.*
importar java.applet.Applet
importar java.util.Calendar
importar; java.text.SimpleDateFormat;
importar java.util.Date;
la clase pública ClockApplet extiende el Applet implementa Runnable //El Applet admite subprocesos
{
Hilo privado athread; // Hilo
private SimpleDateFormat sdateformat; // Formato de fecha
public void init()
{
this.setBackground(Color.white); //Establece el color de fondo en blanco
this.athread = null;
}
public void paint (Gráficos g)
{
this.sdateformat = new SimpleDateFormat("hh hora mm minuto ss segundo"); sdateformat .format(new Date()), 25, 131);
Calendario ahora mismo = Calendar.getInstance();
int segundo = justo ahora.get(Calendar.SECOND);
int minuto = justo ahora.get(Calendario.MINUTO);
int hora = justo ahora.get(Calendario.HORA);
//Radio p >
int R_H = 20, R_M = 4, R_S = 4;
//Coordenadas de la manecilla de hora
//x ====(9-3 )[0 -6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x;
int H_y
//x
if(hora == 0)
{
hora = 12
}
if( hora gt; = 3 amp; amp; hora lt; = 9)
{
H_x = R_H*Math.abs(hora - 9
);
}
else
{
if(hora gt; 9)
{
H_x = R_H* Math.abs(hora - 9);
}
else
{
H_x = R_H*Math.abs(hora 3 );
}
}
//y
if( hora gt; = 6 amp; amp; hora lt; = 12 )
{
H_y = R_H*Math.abs(hora - 12
}
else
);{
H_y = R_H*hora;
}
//Coordenadas del minutero
int M_x; p >
int M_y
if(minuto == 0)
{
minuto = 60; / p>
if( minuto gt;= 15 amp; amp; minuto lt;= 45)
{
M_x = R_M*Math.abs(minuto - 45)
}
else
{
if(minuto gt; 45)
{
M_x = R_M*Math.abs(minuto - 45);
}
else
{
M_x = R_M *Math.abs(minuto 15);
}
}
//y
if( minuto gt; = 30 amp ;amp; minuto lt; 60)
{
M_y = R_M*Math.abs(minuto - 60);
}
else
{
M_y = R_M*minuto
}
//Segunda mano
Coordenadas
int S_x
int S_y
if(segundo == 0)
{
segundo = 60;
}
si( segundo gt; = 15 amp; segundo lt; = 45 )
{
S_x = R_S*Math.abs(segundo - 45);
}
else
{
if(segundo gt; 45)
{
S_x = R_S*Math.abs(segundo - 45
}
más
{
S_x = R_S*Math.abs(segundo 15);
}
}
//y
if( segundo gt;= 30 amp; segundo lt;= 60)
{
S_y = R_S*Math.abs(segundo - 60) ;
}
más
{
S_y = R_S*segundo
}
/ / g.drawString(String.valueOf(segundo), 25, 50);
// g.drawString(String.valueOf(minuto), 25, 60); / g.drawString(String.valueOf(hora), 25, 70);
// g.drawString(String.valueOf(H_x), 25, 80); / g.drawString(String.valueOf(H_y), 25, 90);
g.drawOval(0, 0, 120, 120); //La distancia es de 10 píxeles
g.setColor(Color.darkGray);
g.drawString("9", 5, 65
g.drawString("3", 110, 65); ;
g.drawString("12", 55, 15);
g.drawString("6", 55, 115); dibujarCadena("1", 80
, 20);
g.drawString("2", 100, 40
g.drawString("4", 100,
g.drawString("5", 80, 110);
g.drawString("7", 30, 110);
g.drawString("8", 10); , 90);
g.drawString("10", 10, 40
g.drawString("11", 30, 20); > g.setColor(Color.red);
g.drawLine(60, 60, H_x, H_y); //El primer punto representa el punto inicial y el otro representa el punto final
g .setColor(Color.azul);
g.drawLine(60, 60, M_x, M_y);
g.setColor(Color.amarillo ); p>
g.drawLine(60, 60, S_x, S_y);
}
inicio vacío público()
{
if (athread == null)
{
athread = new Thread(this
athread.start();
}
}
parada nula pública()
{
if(athread != null)
{
athread.interrupt();
athread = nulo
}
}
ejecución pública vacía ()
{
while(athread != null)
{
repintar();
intentar
{
athread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}