本文共 1960 字,大约阅读时间需要 6 分钟。
1 import java.awt.;2 import java.util.Date; 3 import javax.swing.;
4 5 public class Clock extends JComponent{ 6 /*7 8 /9 private static final long serialVersionUID = -5379472973578609775L;10 private Font f = new Font("微软雅黑",Font.PLAIN,15);11 private Font f2 = new Font("微软雅黑",Font.BOLD,15);12 private JLabel l = new JLabel("当前时间:");13 private JLabel display = new JLabel();14 private JLabel display2 = new JLabel("");15 private int hour = 0;16 private int min = 0;17 private int sec = 0;18 private Date now = new Date();19 private Graphics2D g;20 final double PI = Math.PI;21 private String strTime = "" ;22 23 @SuppressWarnings("deprecation")24 public Clock(){ 25 add(l);26 l.setBounds(120, 320, 80, 20);27 l.setFont(f);28 add(display);29 display.setBounds(195, 320, 80, 20);30 display.setFont(f);31 display.setBorder(BorderFactory.createLineBorder(Color.black));32 add(display2);33 display2.setBounds(90, 350, 250, 20);34 display2.setFont(f);35 hour = now.getHours();36 min = now.getMinutes();37 sec = now.getSeconds();38 setVisible(true);39 }40 41 public void paintComponent(Graphics g1){ 42 double x,y;43 super.paintComponent(g1);44 g = (Graphics2D) g1;45 //反锯齿开关开46 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);47 48 //画表盘49 g.setPaint(new GradientPaint(5,40,Color.blue,15,50,Color.yellow,true));50 g.setStroke( new BasicStroke(3,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));51 g.drawOval(75, 40, 250, 250);52 g.fillOval(195, 160, 10, 10);53 g.setColor(Color.black);54 55 //画60个点56 for(int i = 0;i < 60;i++)57 { 58 double[] co = new double[2];59 co = paint_Dot(i 2 * PI / 60);60 x = co[0];61 y = co[1];62 if(i == 0 || i == 15 || i == 30 || i == 45)//画3,6,9,12四个大点63 { 64 g.fillOval((int)(x - 5 + 200),(int)(y - 5 + 165),10,10);65 }66 else//其他小点67 { 68 g.fillOval((int)(x - 2.5 + 200),(int)(y - 2.5 + 165),5,5);69 }转载于:https://blog.51cto.com/14380112/2407092