《JAVA程序设计》课程设计

设计题目和内容
设计一个时钟,有时针和分针,要求界面美观
要求:软件需提供源程序,并能正常运行
如果选择后再追加100分
没说明的不与考虑!

1 package study.part02;
2 import java.util.Calendar;
3 import java.awt.*;
4 import javax.swing.*;
5 import java.awt.event.*;
6 import java.lang.Thread;
7 public class Clock extends JFrame implements ComponentListener,
8 ItemListener,Runnable{
9 Thread timer;
10 private JComboBox combobox_color;
11 public void start(){
12 if(timer==null)
13 timer=new Thread(this,"ShowTime");
14 timer.start();
15 }
16 public void run(){
17 while(true){
18 try{
19 timer.sleep(1000);
20 }catch(InterruptedException e){}
21 repaint();
22 }
23 }
24 public void stop(){
25 timer.stop();
26 }
27 public Clock(){
28 super("Clock");
29 this.setSize(600,600);
30 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
31 this.setLayout(new FlowLayout());
32
33 this.setVisible(true);
34 }
35 public void paint(Graphics g){
36 Calendar cal=Calendar.getInstance();
37 int year=cal.get(Calendar.YEAR);
38 int month=cal.get(Calendar.MONTH);
39 int day=cal.get(Calendar.DATE);
40 int hour=cal.get(Calendar.HOUR);
41 int minute=cal.get(Calendar.MINUTE);
42 int second=cal.get(Calendar.SECOND);
43 int a,b;
44 a=this.getWidth()/2;
45 for(int i=1;i<=360;i++){
46 double angle=i*Math.PI/180;
47 double radius=a-50;
48 int x=(int)Math.round(radius*Math.sin(angle));
49 int y=(int)Math.round(radius*Math.cos(angle));
50 if(i%30==0){
51 int j=i/30;
52 String str=String.valueOf(j);
53 g.setColor(Color.black);
54 // g.fillOval(a+x,a+y,1,1);
55 g.drawString(str,a+x,a-y);
56 }
57 double radh=a-200;
58 angle=hour*Math.PI/30;
59 int xh=(int)Math.round(radh*Math.sin(angle));
60 int yh=(int)Math.round(radh*Math.cos(angle));
61 g.setColor(Color.red);
62 g.drawLine(a,a,a+xh,a-yh);
63 double radm=a-150;
64 angle=minute*Math.PI/30;
65 int xm=(int)Math.round(radm*Math.sin(angle));
66 int ym=(int)Math.round(radm*Math.cos(angle));
67 g.setColor(Color.blue);
68 g.drawLine(a,a,a+xm,a-ym);
69 double rads=a-100;
70 angle=second*Math.PI/30;
71 int xs=(int)Math.round(rads*Math.sin(angle));
72 int ys=(int)Math.round(rads*Math.cos(angle));
73 g.setColor(Color.yellow);
74 g.drawLine(a,a,a+xs,a-ys);
75 //g.drawString(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.
76 // MINUTE)+":"+cal.get(Calendar.SECOND));
77 }
78 }
79 public void itemStateChanged(ItemEvent e){
80 repaint();
81 }
82 public void componentResized(ComponentEvent e){
83 repaint();
84 }
85 public void componentMoved(ComponentEvent e){}
86 public void componentHidden(ComponentEvent e){}
87 public void componentShown(ComponentEvent e){}
88
89 public static void main(String[] args){
90 Clock show=new Clock();
91 show.start();
92 }
93 }追问

能说明一下吗?

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答