复制代码代码如下:
//封装com.clock;
导入 java.awt.BasicStroke;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入 java.awt.Graphics2D;
导入 java.awt.Insets;
导入 java.awt.event.ActionEvent;
导入 java.awt.event.ActionListener;
导入 java.awt.event.WindowAdapter;
导入 java.awt.event.WindowEvent;
导入 java.util.Calendar;
导入 java.util.GregorianCalendar;
导入 javax.swing.*;
导入 javax.swing.JFrame;
公共类 Clock 扩展 JFrame 实现 ActionListener{
私有最终双精度 RAD=Math.PI/180;
公共时钟(){
超级(“时钟”);
设置大小(400, 400);
设置位置(400, 200);
设置可见(真);
设置可调整大小(真);
setBackground(颜色.白色);
addWindowListener(new WindowAdapter() {
@覆盖
公共无效windowClosing(WindowEvent e){
系统.退出(0);
}
});
ActionListener drawClock=new ActionListener() {
@覆盖
公共无效actionPerformed(ActionEvent e){
重绘();
}
};
新计时器(1000,drawClock).start();
}
@覆盖
公共无效actionPerformed(ActionEvent e){}
公共无效油漆(图形g){
Graphics2D g2=(Graphics2D)g;
插入插入=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(日历.MINUTE);
int Second=now.get(日历.SECOND);
弦时间;
if(小时<10){时间=“0”+小时;}否则{时间=“”+小时;}
if(分钟<10){time+=":0"+分钟;}else{time+=":"+分钟;}
if(秒<10){时间+=":0"+秒;}else{时间+=":"+秒;}
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);
}
公共静态无效主(字符串[] args){
新时钟();
}
}