import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class clock{
public static void main(String[] args) {
MyJFrame clo = new MyJFrame();
clo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clo.setVisible(true);
}
}
class MyJFrame extends JFrame
{
public MyJFrame()
{
setSize(450,450);
setTitle("clock");
MyJPanel clock = new MyJPanel();
add(clock);
}
}
class MyJPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
double circlecenterX = 190.0;
double circlecenterY = 190.0;
double radius = 150;
//画钟面
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(circlecenterX,circlecenterY,circlecenterX+radius,circlecenterY+radius);
//g2.setPaint(Color.green);
//g2.fill(circle);
g2.draw(circle);
//画出时钟中心
Ellipse2D center = new Ellipse2D.Double();
center.setFrameFromCenter(circlecenterX,circlecenterY,circlecenterX+2,circlecenterY+2);
g2.setPaint(Color.red);
g2.fill(center);
g2.draw(center);
//画时针、分针、秒针
Line2D second = new Line2D.Double(circlecenterX,circlecenterY,circlecenterX+130,circlecenterY);
g2.setColor(Color.red);
g2.draw(second);
Line2D minute = new Line2D.Double(circlecenterX,circlecenterY,circlecenterX,circlecenterY-110);
g2.setColor(Color.blue);
g2.draw(minute);
Line2D hour = new Line2D.Double(circlecenterX,circlecenterY,circlecenterX,circlecenterY +90);
g2.setColor(Color.black);
g2.draw(hour);
//画钟盘刻度
for(int i = 0; i <= 360; i+=6)
{
double radian = Math.toRadians(i);
if(i <= 360)
{
double cX = circlecenterX + radius*Math.sin(radian);
double cY = circlecenterY - radius*Math.cos(radian);
if(i == 0)
g.drawString("12",(int)(circlecenterX-5),(int)(circlecenterY-radius-4));
else if(i == 90)
g.drawString("3",(int)(circlecenterX+radius+4),(int)(circlecenterY+4));
else if(i == 180)
g.drawString("6",(int)(circlecenterX-2),(int)(circlecenterY+radius+10));
else if(i == 270)
g.drawString("9",(int)(circlecenterX-radius-8),(int)(circlecenterY+4));
if(i%5 == 0)
g2.draw(new Line2D.Double(cX,cY,circlecenterX+(radius-8)*Math.sin(radian),circlecenterY-(radius-8)*Math.cos(radian)));
else
g2.draw(new Line2D.Double(cX,cY,circlecenterX+(radius-5)*Math.sin(radian),circlecenterY-(radius-5)*Math.cos(radian)));
}
}
//显示当前时间
g2.drawString("Hour:06 Minute:00 Second:15",(int)(circlecenterX-75),(int)(circlecenterY+radius+25));
}
}