复制代码代码如下:
//paket com.clock;
impor java.awt.BasicStroke;
impor java.awt.Color;
impor java.awt.Font;
impor java.awt.Graphics;
impor java.awt.Graphics2D;
impor java.awt.Insets;
impor java.awt.event.ActionEvent;
impor java.awt.event.ActionListener;
impor java.awt.event.WindowAdapter;
impor java.awt.event.WindowEvent;
import java.util.Kalender;
import java.util.GregorianCalendar;
import javax.swing.*;
impor javax.swing.JFrame;
Jam kelas publik memperluas implementasi JFrame ActionListener{
RAD ganda akhir pribadi=Math.PI/180;
Jam publik(){
super("Jam");
setUkuran(400, 400);
setLokasi(400, 200);
setVisible(benar);
setResizable(benar);
setBackground(Warna.putih);
addWindowListener(WindowAdapter baru() {
@Mengesampingkan
jendela kekosongan publikPenutupan(WindowEvent e) {
Sistem.keluar(0);
}
});
ActionListener drawClock=ActionListener baru() {
@Mengesampingkan
public void actionPerformed(ActionEvent e) {
mengecat ulang();
}
};
Timer baru(1000,drawClock).start();
}
@Mengesampingkan
kekosongan publik actionPerformed(ActionEvent e) {}
cat void publik(Grafik g){
Grafik2D g2=(Grafik2D)g;
Sisipkan sisipan=getInsets();
int h=getSize().tinggi;
g2.setStroke(BasicStroke baru(3.0f));
g.drawOval(40,40, jam-80,jam-80);
ke dalam r=200;
int R1=140;
int initRad=60;
untuk(int i=1;i<=5;i++){
int x0= (int) (r+R1*Matematika.cos(RAD*initRad));
int y0= (int) (r-R1*Matematika.sin(RAD*initRad));
g.setFont(Font baru("楷体", Font.BOLD, 20));
g.drawString(""+i,x0,y0);
initRad-=30;
}
int R2=150;
int initRad2=-120;
untuk(int i=7;i<=11;i++){
int x0= (int) (r+R2*Matematika.cos(RAD*initRad2));
int y0= (int) (r-R2*Matematika.sin(RAD*initRad2));
g.setFont(Font baru("楷体", Font.BOLD, 20));
g.drawString(""+i,x0,y0);
initRad2-=30;
}
g.drawString("6", 190, 350);
g.drawString("12", 190, 60);
Kalender sekarang=New GregorianCalendar();
int jam=sekarang.dapatkan(Kalender.HOUR_OF_DAY);
int menit=sekarang.dapatkan(Kalender.MINUTE);
int detik=sekarang.dapatkan(Kalender.SECOND);
Waktu senar;
if(jam<10){time="0"+jam;}else{time=""+jam;}
if(menit<10){time+=":0"+menit;}else{time+=":"+menit;}
if(detik<10){time+=":0"+detik;}else{time+=":"+detik;}
g.setColor(Warna.PUTIH);
g.fillRect(10, 30, 90, 28);
g.setColor(Warna.merah);
g.drawString(waktu, 10, 50);
int S=90 detik*6;
int M=90 menit*6;
int H=90 jam*30 menit/2;
g2.setStroke(BasicStroke baru(1.0f));
int x=(int) (200+130*Matematika.cos(RAD*S));
int y=(int) (200-130*Matematika.sin(RAD*S));
int SS=90-(detik-1)*6;
int xx=(int) (200+130*Matematika.cos(RAD*SS));
int yy=(int) (200-130*Matematika.sin(RAD*SS));
g.setColor(getBackground());
g.drawLine(200, 200, xx, yy);
g.setColor(Warna.hitam);
g.drawLine(200, 200, x, y);
g2.setStroke(BasicStroke baru(2.2f));
int x1=(int) (200+110*Matematika.cos(RAD*M));
int y1=(int) (200-110*Matematika.sin(RAD*M));
int MM=90-(menit-1)*6;
int x11=(int) (200+110*Matematika.cos(RAD*MM));
int y11=(int) (200-110*Matematika.sin(RAD*MM));
g.setColor(getBackground());
g.drawLine(200, 200, x11, y11);
g.setColor(Warna.biru);
g.drawLine(200, 200, x1, y1);
g2.setStroke(BasicStroke baru(4.3f));
int xx1=(int) (200+90*Matematika.cos(RAD*H));
int yy1=(int) (200-90*Matematika.sin(RAD*H));
int HH=90-(jam-1)*30-menit/2;;
int xxx1=(int) (200+90*Matematika.cos(RAD*HH));
int yyy1=(int) (200-90*Matematika.sin(RAD*HH));
g.setColor(Warna.putih);
g.drawLine(200, 200, xxx1, yyy1);
g.setColor(Warna.hijau);
g.drawLine(200, 200, xx1, yy1);
}
public static void main(String[] args) {
Jam baru();
}
}