隨筆-3  評論-1  文章-13  trackbacks-0

          import java.applet.Applet;
          import java.awt.Color;
          import java.awt.Graphics;
          import java.util.Date;

          public class Clock_good extends Applet implements Runnable {
           Thread hourg = null, muniteg = null, secondg = null;

           int hour_a, hour_b, munite_a, munite_b, second_a, second_b;

           int hour = 0, munite = 0, second = 0;

           Graphics g_hour = null, g_munite = null, g_second = null;

           double point_x[] = new double[61], point_y[] = new double[61];

           double scaled_x[] = new double[61], scaled_y[] = new double[61];

           int start_count = 0;

           public void init() {
            g_hour = this.getGraphics();
            g_hour.setColor(Color.cyan);
            g_munite = this.getGraphics();
            g_munite.setColor(Color.blue);
            g_second = this.getGraphics();
            g_second.setColor(Color.red);
            g_hour.translate(100, 100);
            g_munite.translate(100, 100);
            g_second.translate(100, 100);
            point_x[0] = 0;
            point_y[0] = 60;
            scaled_x[0] = 0;
            scaled_y[0] = -70;
            double jiaodu = Math.PI / 30;
            for (int i = 0; i < 60; i++) {
             point_x[i+1] = point_x[i] * Math.cos(jiaodu) - Math.sin(jiaodu)
               * point_y[i];
             point_y[i+1] = point_y[i] * Math.cos(jiaodu) + Math.sin(jiaodu)
               * point_x[i];
            }
            point_x[60] = 0;
            point_y[60] = -60;
            for (int i = 0; i < 60; i++) {
             scaled_x[i+1] = scaled_x[i] * Math.cos(jiaodu) - Math.sin(jiaodu)
               * scaled_y[i];
             scaled_y[i+1] = scaled_y[i] * Math.cos(jiaodu) + Math.sin(jiaodu)
               * scaled_x[i];
            }
            scaled_x[60] = 0;
            scaled_y[60] =-70;
           }

           public void start() {
            if (start_count >= 1) {
             hourg.interrupt();muniteg.interrupt();secondg.interrupt();
            }
            secondg=new Thread(this);
            muniteg=new Thread(this);
            hourg=new Thread(this);
            
            
            secondg.start();
            muniteg.start();
            hourg.start();
           
            
            start_count++;
            if(start_count>=2)
             start_count=1;
           }

           public void run() {
            // TODO 自動生成方法存根
            Date date=new Date();
            String s=date.toString();
            
            hour=Integer.parseInt(s.substring(11,13));
            munite=Integer.parseInt(s.substring(14,16));
            second=Integer.parseInt(s.substring(17,19));
            if(Thread.currentThread()==secondg){
             second_a=(int)point_x[second];
             second_b=(int)point_y[second];
             g_second.drawLine(0,0,second_a, second_b);
             int i=second;
             while(true){
               try {
                secondg.sleep(1000);
                Color c=getBackground();
                g_second.setColor(c);
                g_second.drawLine(0,0, second_a, second_b);
                if((second_a==munite_a)&&(second_b==munite_b)){
                 g_munite.drawLine(0, 0, munite_a, munite_b);
                 
                }
                if((second_a==hour_a)&&(second_b==hour_b)){
                 g_hour.drawLine(0, 0, hour_a,hour_b);
                }
                
               } catch (InterruptedException e) {
                // TODO 自動生成 catch 塊
                Color c=getBackground();
                g_second.setColor(c);
                g_second.drawLine(0, 0, second_a, second_b);
                return;
               }
               second_a=(int)point_x[(i+1)%60];
               second_b=(int)point_y[(i+1)%60];
                  g_second.setColor(Color.red);
                  g_second.drawLine(0, 0, second_a, second_b);
                  i++;
             }
             
            }
            
            if(Thread.currentThread()==muniteg){
             munite_a=(int)(point_x[munite]/1.5);
             munite_b=(int)(point_y[munite]/1.5);
             g_munite.drawLine(0,0,munite_a, munite_b);
             int i=munite;
             while(true){
              try {
               muniteg.sleep(1000*60-second*1000);
               second=0;
               Color c=getBackground();
               g_munite.setColor(c);
               g_munite.drawLine(0, 0,munite_a , munite_b);
               if((hour_a==munite_a)&&(hour_b==munite_b)){
                g_hour.drawLine(0, 0, hour_a,hour_b);
               }
              } catch (InterruptedException e) {
               // TODO 自動生成 catch 塊
               return;
              }
              munite_a=(int)(point_x[(i+1)%60]/1.5);
              munite_b=(int)(point_y[(i+1)%60]/1.5);
                 g_munite.setColor(Color.blue);
                 g_munite.drawLine(0, 0, munite_a, munite_b);
                 i++;
              second=0;
             }
            }
            if(Thread.currentThread()==hourg){
             int h=hour%12;
             hour_a=(int)point_x[h*5+munite/12]/2;
             hour_b=(int)point_y[h*5+munite/12]/2;
             int i=h*5+munite/12;
             g_hour.drawLine(0, 0, hour_a, hour_b);
             while(true){
              try {
               hourg.sleep(1000*60*12-1000*60*(munite%12)-second*1000);
               munite=0;
               Color c=getBackground();
               g_hour.setColor(c);
               g_hour.drawLine(0, 0,hour_a , hour_b);
               
              } catch (InterruptedException e) {
               // TODO 自動生成 catch 塊
               return;
              }
              
              hour_a=(int)point_x[(i+1)%60]/2;
              hour_b=(int)point_y[(i+1)%60]/2;
                 g_hour.setColor(Color.cyan);
                 g_hour.drawLine(0, 0, hour_a, hour_b);
                 i++;
              munite=0;
             }
            }

           }
           public void stop(){
            secondg.interrupt();muniteg.interrupt();hourg.interrupt();
           }
           public void paint(Graphics g){
            this.start();
            g.drawOval(25, 25, 150, 150);
            g.translate(100, 100);
            //g.drawOval(0, 0, 150, 150);
            for(int i=0;i<60;i++){
             if(i%5==0){
              g.setColor(Color.red);
              g.fillOval((int)scaled_x[i], (int)scaled_y[i], 4,4);
             }else{
              g.fillOval((int)scaled_x[i], (int)scaled_y[i], 2, 2);
             }
            }
           }
           

          }

          posted on 2007-07-16 12:42 lcgxc 閱讀(165) 評論(0)  編輯  收藏 所屬分類: java
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆檔案(3)

          文章分類(13)

          文章檔案(13)

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 赤壁市| 宁都县| 罗田县| 鹿泉市| 鲜城| 昌宁县| 冷水江市| 葵青区| 繁昌县| 鄯善县| 吉首市| 洛隆县| 五河县| 广水市| 防城港市| 常州市| 社旗县| 江永县| 黄陵县| 乐东| 宁波市| 兴安盟| 金湖县| 河津市| 灵寿县| 乌鲁木齐市| 焉耆| 英超| 班玛县| 茶陵县| 江源县| 泾源县| 常德市| 左权县| 平南县| 斗六市| 东安县| 洛浦县| 栾城县| 永泰县| 固始县|