java实现可视时钟

来源:互联网 发布:做菜软件下载排行榜 编辑:程序博客网 时间:2024/04/28 13:00

代码文件:

http://dl2.csdn.net/down4/20070730/30225836651.rar

java代码:

 //SwbClock.java
import java.awt.*;
import java.util.*;
import java.text.*;
import java.applet.*;

public class SwbClock extends Applet implements Runnable{
 //declare variable
 private Thread my_thread;  //thread to display the clock
 private Date current_date;  //
 private String lastdate,lasttime;   // string of the date
 private int last_x_s,last_y_s,last_x_m,
   last_y_m,last_x_h,last_y_h; // the dimension of hands
 private Color mh_hand_color,s_hand_color,inner_circle_color,
   outer_circle_color,inner_fill_color,outer_fill_color,
   date_color,bg_color;
 private String mh_hand_str,s_hand_str,inner_circle_str,
   outer_circle_str,inner_fill_str,outer_fill_str,
   date_str,bg_str;
 private String font_to_use;
 private Font font;
 private SimpleDateFormat formatter;
 private int R,r,xcenter,ycenter;
 //method: init()
 public void init(){
  R=60;
  r=50;
  xcenter=70;
  ycenter=70;
  last_x_s=last_y_s=last_x_m=last_y_m=last_x_h=last_y_h=xcenter;
  mh_hand_str="999900";
  s_hand_str="99ff00";
  inner_fill_str="ffffff";
  outer_fill_str="edfefe";
  inner_circle_str="99ffcc";
  outer_circle_str="99ffcc";
  date_str="3300cc";
  bg_str="ffffff";
  current_date=new Date();
  formatter=new SimpleDateFormat("EEE-yy-MMM-dd",Locale.getDefault());
  lastdate=formatter.format(current_date);
  formatter.applyPattern("hh:mm:ss");
  lasttime=formatter.format(current_date);
  font_to_use="Serif";
  font=new Font(font_to_use,Font.PLAIN,12);
  String param;
  if( (param=getParameter("bg_str")) != null)
   bg_str=param;
  if( (param=getParameter("date_str")) != null)
   date_str=param;
  if( (param=getParameter("outer_circle_str")) != null)
   outer_circle_str=param;
  if( (param=getParameter("inner_circle_str")) != null)
   inner_circle_str=param;
  if( (param=getParameter("outer_fill_str")) != null)
   outer_fill_str=param;
  if( (param=getParameter("inner_circle_str")) != null)
   inner_circle_str=param;
  if( (param=getParameter("s_hand_str")) != null)
   s_hand_str=param;
  if( (param=getParameter("mh_hand_str")) != null)
   mh_hand_str=param;
  try{
   mh_hand_color=new Color(Integer.parseInt(mh_hand_str,16));
   s_hand_color=new Color(Integer.parseInt(s_hand_str,16));
   inner_circle_color=new Color(Integer.parseInt(inner_circle_str,16));
   outer_circle_color=new Color(Integer.parseInt(outer_circle_str,16));
   inner_fill_color=new Color(Integer.parseInt(inner_fill_str,16));
   outer_fill_color=new Color(Integer.parseInt(outer_fill_str,16));
   date_color=new Color(Integer.parseInt(date_str,16));
   bg_color=new Color(Integer.parseInt(bg_str,16));
  }catch(IllegalArgumentException e){
   e.printStackTrace();
  }
  setBackground(bg_color);
 }
 //method: start()
 public void start(){
  my_thread=new Thread(this);
  my_thread.start();
 }
 //method: stop()
 public void stop(){
  my_thread=null;
 }
 //method: run()
 public void run(){
  while(true){
   repaint();
   try{
    my_thread.sleep(100);
   }catch(InterruptedException e){
    e.printStackTrace();
   }
  }
 }
 //method: update(Graphics g)
 public void update(Graphics g){
  int xh,yh,xm,ym,xs,ys;
  int s=0,m=10,h=10;
  String today;
  String time;
  current_date=new Date();
  
  formatter.applyPattern("s");
  try{
   s=Integer.parseInt(formatter.format(current_date));
  }catch(NumberFormatException e){
   s=0;
  }
  formatter.applyPattern("m");
  try{
   m=Integer.parseInt(formatter.format(current_date));
  }catch(NumberFormatException e2){
   m=10;
  }
  formatter.applyPattern("h");
  try{
   h=Integer.parseInt(formatter.format(current_date));
  }catch(NumberFormatException e3){
   e3.printStackTrace();
  }
  
  xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 50 + xcenter);
        ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 50 + ycenter);
        xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
        ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
        xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
                   + xcenter);
        yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
                   + ycenter);
        //Get the date to print
        formatter.applyPattern("EEE-yy-MMM-dd");
        today=formatter.format(current_date);
        formatter.applyPattern("hh:mm:ss");
        time=formatter.format(current_date);
        g.setFont(font);
  //draw the hand
  g.setColor(inner_fill_color);

//先擦除掉已绘各针
  if(xs!=last_x_s||ys!=last_y_s){
   g.drawLine(xcenter,ycenter,last_x_s,last_y_s);
   g.drawString(lastdate,25,2*ycenter+5);
   g.drawString(lasttime,xcenter-20,ycenter+25);
  }
  if(xm!=last_x_m||ym!=last_y_m){
   g.drawLine(xcenter,ycenter-1,last_x_m,last_y_m);
   g.drawLine(xcenter-1,ycenter,last_x_m,last_y_m);
  }
  if(xh!=last_x_h||yh!=last_y_h){
   g.drawLine(xcenter,ycenter-1,last_x_h,last_y_h);
   g.drawLine(xcenter-1,ycenter,last_x_h,last_y_h);
  } 
  //draw the hand
  g.setColor(mh_hand_color);
  g.drawLine(xcenter,ycenter-1,xh,yh);
  g.drawLine(xcenter-1,ycenter,xh,yh);
  g.drawLine(xcenter,ycenter-1,xm,ym);
  g.drawLine(xcenter-1,ycenter,xm,ym);
  g.setColor(s_hand_color);
  g.drawLine(xcenter,ycenter,xs,ys); 
  
  g.setColor(date_color);
  g.drawString(today,25,2*ycenter+5);
  g.drawString(time,xcenter-20,ycenter+25);
  
  last_x_s = xs; last_y_s = ys;
        last_x_m = xm; last_y_m = ym;
        last_x_h = xh; last_y_h = yh;
        lastdate = today;
        lasttime = time;
        current_date = null;      
 }
 //method: paint(Graphics g)
 public void paint(Graphics g){
  g.setFont(font);
  //draw the clock
  g.setColor(outer_fill_color);
  g.fillArc(xcenter-R,ycenter-R,2*R,2*R,0,360);
  g.setColor(outer_circle_color);
  g.drawArc(xcenter-R,ycenter-R,2*R,2*R,0,360);
  g.setColor(inner_fill_color);
  g.fillArc(xcenter-r,ycenter-r,2*r,2*r,0,360);
  g.setColor(inner_circle_color);
  g.drawArc(xcenter-r,ycenter-r,2*r,2*r,0,360);
  //draw the number
  g.setColor(mh_hand_color);
  g.drawString("12",xcenter-5,ycenter-r+12);
  g.drawString("3",xcenter+r-9,ycenter+3);
  g.drawString("6",xcenter-3,ycenter+r-3);
  g.drawString("9",xcenter-r+5,ycenter+3);
  //draw the hand
  g.setColor(s_hand_color);
  g.drawLine(xcenter,ycenter,last_x_s,last_y_s);
  g.setColor(mh_hand_color);
  g.drawLine(xcenter,ycenter-1,last_x_m,last_y_m);
  g.drawLine(xcenter-1,ycenter,last_x_m,last_y_m);
  g.drawLine(xcenter,ycenter-1,last_x_h,last_y_h);
  g.drawLine(xcenter-1,ycenter,last_x_h,last_y_h);

 }
 
}

html代码:

<applet code="SwbClock.class" width=150 height=160>
<param name="mh_hand_str" value="999900">
<param name="s_hand_str" value="99ff00">
<param name="inner_fill_str" value="ffffff">
<param name="outer_fill_str" value="edfefe">
<param name="inner_circle_str" value="99ffcc">
<param name="outer_circle_str" value="99ffcc">
<param name="date_str" value="3300cc">
<param name="bg_str" value="ffffff">
</applet>
所有param都是可选的,mh_hand_str是时针和分针的颜色,s_hand_str是秒针的颜色,inner_fill_str是表盘内部填充色,outer_fill_str是表盘背景色,inner_circle_str表盘内部边缘颜色,outer_circle_str表盘外部边缘颜色,date_str字符颜色,bg_str背景色。

原创粉丝点击