复制代码代码如下:
//pacote 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;
classe pública Clock estende JFrame implementa ActionListener{
duplo final privado RAD=Math.PI/180;
relógio público(){
super("Relógio");
setSize(400, 400);
setLocation(400, 200);
setVisible(verdadeiro);
setResizable(true);
setBackground(Color.branco);
addWindowListener(new WindowAdapter() {
@Substituir
public void windowClosing(WindowEvent e) {
Sistema.exit(0);
}
});
ActionListener drawClock=new ActionListener() {
@Substituir
public void actionPerformed(ActionEvent e) {
repintar();
}
};
novo Timer(1000,drawClock).start();
}
@Substituir
public void actionPerformed(ActionEvent e) {}
public void paint(Gráficos g){
Gráfico2D g2=(Gráfico2D)g;
Inserções insert=getInsets();
int h=getSize().altura;
g2.setStroke(novo BasicStroke(3.0f));
g.drawOval(40,40,h-80,h-80);
int r=200;
interno R1=140;
int initRad=60;
for(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(new Font("楷体", Font.BOLD, 20));
g.drawString(""+i, x0, y0);
initRad-=30;
}
interno R2=150;
int initRad2=-120;
for(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(new Font("楷体", Font.BOLD, 20));
g.drawString(""+i, x0, y0);
initRad2-=30;
}
g.drawString("6", 190, 350);
g.drawString("12", 190, 60);
Calendário agora=new GregorianCalendar();
int hora=agora.get(Calendário.HOUR_OF_DAY);
int minuto=agora.get(Calendário.MINUTE);
int segundo = agora.get (Calendário.SEGUNDO);
Tempo de corda;
if(hora<10){time="0"+hora;}else{time=""+hora;}
if(minuto<10){time+=":0"+minuto;}else{time+=":"+minuto;}
if(segundo<10){tempo+=":0"+segundo;}else{tempo+=":"+segundo;}
g.setColor(Cor.BRANCO);
g.fillRect(10, 30, 90, 28);
g.setColor(Color.vermelho);
g.drawString(tempo, 10, 50);
int S=90 segundos*6;
int M=90 minutos*6;
int H=90 horas*30 minutos/2;
g2.setStroke(novo 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 aa=(int) (200-130*Math.sin(RAD*SS));
g.setColor(getBackground());
g.drawLine(200, 200, xx, aa);
g.setColor(Color.preto);
g.drawLine(200, 200, x, y);
g2.setStroke(novo 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(novo 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 minutos/2;;
int xxx1=(int) (200+90*Math.cos(RAD*HH));
int aaaa1=(int) (200-90*Math.sin(RAD*HH));
g.setColor(Color.branco);
g.drawLine(200, 200, xxx1, yyy1);
g.setColor(Color.verde);
g.drawLine(200, 200, xx1, yy1);
}
public static void main(String[] args) {
novo Relógio();
}
}