课程设计2 机械表

大学那会读的是计算机,起初并不知道这个专业的真正含义是什么,只不过自己喜欢电脑所以就选了计算机,当初并不知道如今的自己走上程序猿之路。大学期间刚开始学习C语言的时候,压根一窍不通,我觉得这跟学校的教育有关,当然也跟自己对于这门课程的认知有关,脑子里对这个东西没有任何概念而硬要往里面塞,当然后来也就只是为了应付考试了,什么补码呀,反码呀,二进制呀...当时还真把我整的够呛。

后来开始学习了面向对象,当然那个时候依然对面向对象没任何概念,直到那个时候这门课程需要学一门语言来支持了,所以就开始接触了,说到这个地方,我们学校有个有趣的规定,当时计算机有13个班,规定前几个班学习C ++语言 后几个班开设JAVA语言,我是9班所以也就学习了JAVA,没有任何前兆,纯属是鸭子被赶上架了...

那时候开始对编程有了很大的兴趣,总感觉我可以让计算机做一些事情了,尤其是接触到GUI的时候,兴趣愈来越浓了,我终于也可以写出窗口了,那时候感觉真的好。。那时候在课堂上学习到的当然只是一些基础理论只是,真正动手还得课后靠自己敲代码。。。就这样开始了程序之路。。


import java.awt.*;

import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.*;
 public class Zhong extends JFrame implements Runnable{
       Thread t=null;                             
               int ox,oy;                                                             //圆心
       final double rad=Math.PI/30.0;                             //标点数用
       final double rad1=Math.PI/180.0;                         //弧度   画秒,分,时针用
   int m=0,n=0,m1=0,n1=0,m2=0,n2=0;                     //判断    擦除秒,分,时针
    Zhong(){
          super("机械表");
          Color color=new Color(255,100,100);
          setBackground(color);
          setSize(600,350);
          setVisible(true);  
          t=new Thread(this);                               //创建一个新线程
          t.start();
          addWindowListener(new MyWindowAdapter());                //窗口 监听
         }
     public void run(){
          try{
        while(true){
          repaint();
      Thread.sleep(1);
          }
    }catch (InterruptedException e){}
       }  
    public void paint( Graphics g ){
         int hh,mm,ss;
         String str1,str3,str2,str;
         int w=getSize().width;
         int h=getSize().height;
         int k;
         int sk;
         int  mk,hk;
         int x1,y1,x2,y2,x3,y3;
         int r;
         int ox1,oy1;
        g.drawString("作者:武学鸿",w-220,200);                   
         g.drawString("班级:计科0609",w-220,230);
         g.drawString("学号:0902060921",w-220,260);        
         Calendar now=Calendar.getInstance();                       //创建Calendar对象
         hh=now.get(Calendar.HOUR);
         mm=now.get(Calendar.MINUTE);
         ss=now.get(Calendar.SECOND);
         g.setColor(Color.pink);
         g.fillRect(w-150,80,100,40);                       //画一个填充矩形
         g.setColor(Color.blue);
         if (hh < 10) str1="0"+hh;     
                 else str1=""+hh;                                  
         if (mm < 10) str2=":0"+mm;
                  else str2=":"+mm;
         if (ss < 10) str3=":0"+ss; 
                  else str3=":"+ss;
            str=str1+str2+str3;
        g.drawString(str,w-130,100);                    //显示时间
         g.setColor(Color.white);
         g.drawOval(40,46,h-70,h-70);                   //钟的边框 圆
         g.drawOval(42,48,h-74,h-74);
         r=h/2-35;                                                   //半径
         ox1=37+r;
         oy1=50+r;
         k=-10;
         for(int i=1;i<=12;i++){                                 //标出时间点
           x1=(int)(ox1+Math.cos(rad*k)*(r+10));
           y1=(int)(oy1+Math.sin(rad*k)*(r+10));
           g.drawString(""+i,x1,y1);
           k=k+5;
            }
         ox=40+r;                                                        //圆心
         oy=46+r;
          sk=ss*6-90;
          mk=(int)((mm+0.0166667*ss)*6-90);
          hk=(int)((hh+0.0166667*mm)*30-90);
          if(m>0){                                                    //擦掉秒针
         g.setColor(new Color(255,100,100));
         g.drawLine(ox,oy,m,n);
         }
         g.setColor(Color.yellow);
         x1=(int)(ox+Math.cos(rad1*sk)*(r-8));
         y1=(int)(oy+Math.sin(rad1*sk)*(r-8));
         g.drawLine(ox,oy,x1,y1);                                //画秒针
         m=x1; 
         n=y1; 

         if(m1>0){                                                    //擦掉分针
         g.setColor(new Color(255,100,100));
         g.drawLine(ox,oy,m1,n1);
         }
         g.setColor(Color.black);
         x2=(int)(ox+Math.cos(rad1*mk)*(r-30));
         y2=(int)(oy+Math.sin(rad1*mk)*(r-30));
         g.drawLine(ox,oy,x2,y2);                             //画分针
         m1=x2;
         n1=y2;

         if(m2>0){                                                        //擦掉时针
         g.setColor(new Color(255,100,100));
         g.drawLine(ox,oy,m2,n2);
         }
         x3=(int)(ox+Math.cos(rad1*hk)*(r-60));
         y3=(int)(oy+Math.sin(rad1*hk)*(r-60));
        g.setColor(Color.green);
         g.drawLine(ox,oy,x3,y3);                              //画时针
         m2=x3;
         n2=y3;
         }

       public static void main(String args[]){
           Zhong z1=new Zhong();
      }
     
}
       class MyWindowAdapter extends WindowAdapter{           //窗口监听  关闭窗口
          public void windowClosing(WindowEvent we){
             System.exit(0);
        }
    }

转载于:https://my.oschina.net/yjwxh/blog/176162

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值