复代码代码如下:
//com.clock 패키지;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.JFrame;
공개 클래스 Clock은 JFrame을 확장하여 ActionListener를 구현합니다.
비공개 최종 더블 RAD=Math.PI/180;
공개 시계(){
super("시계");
setSize(400, 400);
setLocation(400, 200);
setVisible(참);
setResizing(true);
setBackground(Color.white);
addWindowListener(새 WindowAdapter() {
@보수
공공 무효 windowClosing(WindowEvent e) {
시스템.exit(0);
}
});
ActionListener drawClock=새 ActionListener() {
@보수
공공 무효 actionPerformed(ActionEvent e) {
다시 칠하기();
}
};
새로운 타이머(1000,drawClock).start();
}
@보수
공개 무효 actionPerformed(ActionEvent e) {}
공공 무효 페인트(그래픽 g){
Graphics2D g2=(Graphics2D)g;
삽입 insert=getInsets();
int h=getSize().height;
g2.setStroke(new BasicStroke(3.0f));
g.drawOval(40,40, h-80,h-80);
정수 r=200;
정수 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;
}
정수 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);
이제 달력=new GregorianCalendar();
int hour=now.get(Calendar.HOUR_OF_DAY);
int 분=now.get(Calendar.MINUTE);
int second=now.get(Calendar.SECOND);
문자열 시간;
if(시간<10){time="0"+시간;}else{time=""+시간;}
if(분<10){time+=":0"+분;}else{time+=":"+분;}
if(초<10){time+=":0"+초;}else{time+=":"+초;}
g.setColor(Color.WHITE);
g.fillRect(10, 30, 90, 28);
g.setColor(Color.red);
g.drawString(시간, 10, 50);
int S=90초*6;
int M=90분*6;
int H=90시간*30분/2;
g2.setStroke(new BasicStroke(1.0f));
int x=(int) (200+130*Math.cos(RAD*S));
int y=(int) (200-130*Math.sin(RAD*S));
int SS=90-(초-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.black);
g.drawLine(200, 200, x, y);
g2.setStroke(new BasicStroke(2.2f));
int x1=(int) (200+110*Math.cos(RAD*M));
int y1=(int) (200-110*Math.sin(RAD*M));
int MM=90-(분-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.blue);
g.drawLine(200, 200, x1, y1);
g2.setStroke(new BasicStroke(4.3f));
int xx1=(int) (200+90*Math.cos(RAD*H));
int yy1=(int) (200-90*Math.sin(RAD*H));
int HH=90-(시간-1)*30-분/2;;
int xxx1=(int) (200+90*Math.cos(RAD*HH));
int yyy1=(int) (200-90*Math.sin(RAD*HH));
g.setColor(Color.white);
g.drawLine(200, 200, xxx1, yyy1);
g.setColor(Color.green);
g.drawLine(200, 200, xx1, yy1);
}
공개 정적 무효 메인(String[] args) {
새로운 시계();
}
}