复制代码代码如下:
//paquete com.clock;
importar java.awt.BasicStroke;
importar java.awt.Color;
importar java.awt.Font;
importar java.awt.Graphics;
importar java.awt.Graphics2D;
importar java.awt.Insets;
importar java.awt.event.ActionEvent;
importar java.awt.event.ActionListener;
importar java.awt.event.WindowAdapter;
importar java.awt.event.WindowEvent;
importar java.util.Calendar;
importar java.util.GregorianCalendar;
importar javax.swing.*;
importar javax.swing.JFrame;
La clase pública Clock extiende JFrame implementa ActionListener {
doble final privado RAD=Math.PI/180;
Reloj público(){
super("Reloj");
establecerTamaño(400, 400);
establecerUbicación(400, 200);
setVisible(verdadero);
setResizable(verdadero);
setBackground(Color.blanco);
addWindowListener(nuevo WindowAdapter() {
@Anular
ventana vacía públicaClosing(WindowEvent e) {
Sistema.salir(0);
}
});
ActionListener drawClock=nuevo ActionListener() {
@Anular
acción vacía pública realizada (ActionEvent e) {
repintar();
}
};
nuevo Temporizador(1000,drawClock).start();
}
@Anular
acción nula pública realizada (ActionEvent e) {}
pintura vacía pública (Gráficos g) {
Gráficos2D g2=(Gráficos2D)g;
Inserciones insert=getInsets();
int h=getSize().altura;
g2.setStroke(nuevo BasicStroke(3.0f));
g.drawOval(40,40, h-80,h-80);
intr=200;
entero R1=140;
int initRad=60;
para(int i=1;i<=5;i++){
int x0= (int) (r+R1*Math.cos(RAD*initRad));
int y0= (int) (r-R1*Math.sin(RAD*initRad));
g.setFont(nueva fuente("楷体", Font.BOLD, 20));
g.drawString(""+i, x0, y0);
initRad-=30;
}
entero R2=150;
int initRad2=-120;
para(int i=7;i<=11;i++){
int x0= (int) (r+R2*Math.cos(RAD*initRad2));
int y0= (int) (r-R2*Math.sin(RAD*initRad2));
g.setFont(nueva fuente("楷体", Font.BOLD, 20));
g.drawString(""+i, x0, y0);
initRad2-=30;
}
g.drawString("6", 190, 350);
g.drawString("12", 190, 60);
Calendario ahora=nuevo Calendario Gregoriano();
int hora=ahora.get(Calendario.HOUR_OF_DAY);
int minuto=ahora.get(Calendario.MINUTO);
int segundo=ahora.get(Calendario.SEGUNDO);
tiempo de cadena;
if(hora<10){time="0"+hora;}else{time=""+hora;}
if(minuto<10){time+=":0"+minuto;}else{time+=":"+minuto;}
if(segundo<10){time+=":0"+segundo;}else{time+=":"+segundo;}
g.setColor(Color.BLANCO);
g.fillRect(10, 30, 90, 28);
g.setColor(Color.rojo);
g.drawString(tiempo, 10, 50);
int S=90 segundos*6;
int M=90 minutos*6;
int H=90 horas*30 minutos/2;
g2.setStroke(nuevo BasicStroke(1.0f));
int x=(int) (200+130*Math.cos(RAD*S));
int y=(int) (200-130*Math.sin(RAD*S));
int SS=90-(segundo-1)*6;
int xx=(int) (200+130*Math.cos(RAD*SS));
int yy=(int) (200-130*Math.sin(RAD*SS));
g.setColor(getBackground());
g.drawLine(200, 200, xx, yy);
g.setColor(Color.negro);
g.drawLine(200, 200, x, y);
g2.setStroke(nuevo BasicStroke(2.2f));
int x1=(int) (200+110*Math.cos(RAD*M));
int y1=(int) (200-110*Math.sin(RAD*M));
int MM=90-(minuto-1)*6;
int x11=(int) (200+110*Math.cos(RAD*MM));
int y11=(int) (200-110*Math.sin(RAD*MM));
g.setColor(getBackground());
g.drawLine(200, 200, x11, y11);
g.setColor(Color.azul);
g.drawLine(200, 200, x1, y1);
g2.setStroke(nuevo BasicStroke(4.3f));
int xx1=(int) (200+90*Math.cos(RAD*H));
int yy1=(int) (200-90*Math.sin(RAD*H));
int HH=90-(hora-1)*30-minuto/2;;
int xxx1=(int) (200+90*Math.cos(RAD*HH));
int yyy1=(int) (200-90*Math.sin(RAD*HH));
g.setColor(Color.blanco);
g.drawLine(200, 200, xxx1, yyy1);
g.setColor(Color.verde);
g.drawLine(200, 200, xx1, yy1);
}
público estático vacío principal (String [] argumentos) {
nuevo reloj();
}
}