随笔-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 阅读(153) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: