Java程序设计课程设计_《JAVA程序设计》课程设计

展开全部

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 }

追问

能说明一下吗?

本回答由网友推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值