首先说明下,在发这个贴之前,自己还没找相关资料,也没什么思路.
大家双击window系统右下角的时钟,应该能看到一个走动的时钟吧,是有时针分针的那个,问题出来了:
这个时钟如何实现?(当然不是那个数学时钟)不知道三个指针是画出来的,还是用图片呢?
还有精度如何控制?秒针每走一格,时针分针都应该有相应的移位
还有下面的数字时钟数值改变了,指钟时钟也会跟着变化,也不大好办本帖纯为练手,虽实用价值不大,但技术价值个人认为还行,不知有没那位高手发挥下?让大家学习学习啊

解决方案 »

  1.   

    画出来的,不是用图片。
    记得初学java时有个画时钟的小例子,根据时间画线表示时针、分针和秒针就是了。每秒响应一次事件,确定各个指针的位置。
      

  2.   

    嗯,畫出來的,以前學java的時候某次考試就是這個
      

  3.   


    将一下文件编译过产生的Clock.class文件放在与Clock.html同一文件夹下,双击Clock.html文件,程序运行
    import java.awt.Graphics;
    import java.awt.Color;
    import java.util.Calendar;
    /*类Clock继承了java.applet.Applet,由于要用到线程,因而实现了Runnable接口*/
    public class Clock extends java.applet.Applet implements Runnable {
    /*声明一个Thread对象threadObj*/
    Thread threadObj; 
        /*下面的6个变量分别代表前一个位置秒针、分针、时针末端的横纵坐标*/
        int lastxs, lastys, lastxm,lastym, lastxh, lastyh;
        /*lastdate为前一个指针位置对应的日期*/    
        String lastdate;
        /*handColor用于设置时针、分针和钟框的颜色*/
        Color handColor;             
        /*numberColor设置秒针和钟面上数字的颜色*/
        Color numberColor;
        /*声明一个Calendar对象,用于取得当前时间*/
    Calendar rightnow = Calendar.getInstance();
    /*获得当前日期的年份并赋给yearInt*/
    int yearInt  = rightnow.get(rightnow.YEAR); 
    /*获得当前日期的月份并赋给monthInt*/
    int monthInt = rightnow.get(rightnow.MONTH);
    /*值得注意的是:月份以0为基准开始计数,一月对应的month值为0,依次类推*/
    /*获得当前的日期,赋给dayInt*/
    int dayInt   = rightnow.get(rightnow.DATE);  
    /*获得当前时间时钟对应的整数并赋给hoursInt*/
    int hoursInt = rightnow.get(rightnow.HOUR_OF_DAY);  
    /*获得当前时间分针对应的整数并赋给minutesInt*/
    int minutesInt = rightnow.get(rightnow.MINUTE);     
    /*获得当前时间秒针对应的整数并赋给secondsInt*/
    int secondsInt = rightnow.get(rightnow.SECOND);     
    /*获得当前时间对应的星期并赋给dayofweekInt*/
    int dayofweekInt = rightnow.get(rightnow.DAY_OF_WEEK); 
    /*值得注意的是通过DAY_OF_WEEK获得的星期值中,星期天对应的值为1,星期一对应的为2,依次类推,为此,将利用getweekday方法转换(参考getweekday方法)。*/
    /*init()方法用来初始化指针的位置和颜色,以及日期*/
        public void init() {
         /*将秒针、分针和时钟初始位置的横纵坐标设置为0*/
            lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
    /*将上面获得的信息用字符串表示出来*/
    lastdate = yearInt + "年" + (monthInt + 1) + "月" + dayInt + "日" + hoursInt + ":" + minutesInt + ":" + secondsInt + " 星期" + getweekday(dayofweekInt);
            /*设置时钟上时、分、秒针的颜色*/
            handColor = Color.red;
            /*设置时钟上数据的颜色*/
            numberColor = Color.darkGray;
        }
    /*下面的方法变换星期的表示,将1,2,3,4,5,7匹配为7,1,2,3,4,5,6*/
    public int getweekday(int dayInt) {
    int weekdayInt;
    switch(dayInt) {
    case 1: weekdayInt = 7;
    break;
    default: weekdayInt = dayInt - 1;
    }
    return weekdayInt;
    }
    /*start()方法用来启动Clock线程*/
    public void start() {
    /*初始化线程对象,名称为“Clock”*/
    threadObj = new Thread(this, "Clock");
    threadObj.start();
    }
        /*update()方法用来刷新图象*/
        public void update(Graphics g) {
            paint(g);
        }
    /*run()方法用来实现刷新时钟图象*/
    public void run() {
            while (threadObj != null) {
    try {
    threadObj.sleep(10);
    } catch(InterruptedException e) {}
    /*repaint方法用于时间的刷新显示*/
    repaint();
    }
    }
    /*paint()绘制指针位置并进行可能的刷新操作,同时显示当前日期和时间*/
    public void paint(Graphics g) {
            /*下面的前6个int变量分别代表时针、分钟和秒钟外边缘端点的横纵坐标,xcenter和ycenter代表时钟的中心位置*/
            int xh, yh, xm, ym, xs, ys, xcenter = 100, ycenter = 70;
      /*下面的语句用于获得当前时间*/
      Calendar rightnow = Calendar.getInstance();
    yearInt  = rightnow.get(rightnow.YEAR);
    monthInt = rightnow.get(rightnow.MONTH);
    dayInt   = rightnow.get(rightnow.DATE);
    hoursInt = rightnow.get(rightnow.HOUR_OF_DAY);
    minutesInt   = rightnow.get(rightnow.MINUTE);
    secondsInt   = rightnow.get(rightnow.SECOND);
    dayofweekInt = rightnow.get(rightnow.DAY_OF_WEEK);
    /*字符串today包含当前日期和时间信息*/
    String today = yearInt + "年" + (monthInt + 1) + "月" + dayInt + "日" + hoursInt + ":" + minutesInt + ":" + secondsInt + " 星期" + getweekday(dayofweekInt);
    /*计算秒针、分针、时针外边缘端点的横纵坐标(设定三针长度分别为45,40,30)*/
            xs = (int)(Math.cos(secondsInt * 3.14f / 30 - 3.14f / 2) * 45 + xcenter);
            ys = (int)(Math.sin(secondsInt * 3.14f / 30 - 3.14f / 2) * 45 + ycenter);
            /*值得注意的是,计算分针外边缘端点的横纵坐标时,需要考虑秒针对它的影响*/
            xm = (int)(Math.cos((minutesInt + secondsInt/60f) * 3.14f / 30 - 3.14f / 2) * 40 + xcenter);
            ym = (int)(Math.sin((minutesInt + secondsInt/60f) * 3.14f / 30 - 3.14f / 2) * 40 + ycenter);
            /*计算时针外边缘端点的横纵坐标时,需要考虑分针对它的影响,将秒针的影响忽略了*/
            xh = (int)(Math.cos((hoursInt * 30 + minutesInt / 2) * 3.14f / 180 - 3.14f/2) * 30 + xcenter);
            yh = (int)(Math.sin((hoursInt * 30 + minutesInt / 2) * 3.14f / 180 - 3.14f/2) * 30 + ycenter);
        /*绘制矩形和数字(9, 12, 3, 6)*/ 
        handColor = Color.red;
            g.setColor(handColor);
            /*下面的语句绘制红色的矩形外框*/
            g.drawRect(xcenter - 50, ycenter - 50, 100, 100);
            g.setColor(numberColor);
            g.drawString("9",xcenter-45,ycenter+3); 
            g.drawString("3",xcenter+40,ycenter+3);
            g.drawString("12",xcenter-5,ycenter-37);
            g.drawString("6",xcenter-3,ycenter+45);
         /*下面的语句根据当前时间和上一次显示的时间时、分、秒针是否相同来擦除相应的指针,绘制新指针*/
            g.setColor(getBackground());
            if (xs != lastxs || ys != lastys) {
                /*擦除秒针的显示*/
                g.drawLine(xcenter, ycenter, lastxs, lastys);
                /*擦除界面中日期和时间的字符串*/
                g.drawString(lastdate, 30, 150);
            }       
            if (xm != lastxm || ym != lastym) {
             /*擦除分针的显示,分针是由两条线组成的*/
                g.drawLine(xcenter, ycenter-1, lastxm, lastym);
                g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
            if (xh != lastxh || yh != lastyh) {
             /*擦除时针的显示,也是由两条线组成的*/
                g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
                g.drawLine(xcenter-1, ycenter, lastxh, lastyh); 
            }
            /*重新显示秒针和时间字符串*/
            g.setColor(numberColor);
         g.drawString("", 30, 150);
    g.drawString(today, 30, 150);
            g.drawLine(xcenter, ycenter, xs, ys);
            /*将分针和时针的颜色设置为蓝色*/
            handColor = Color.blue;
            g.setColor(handColor);
            /*绘制分针和时针,都分别由两条线组成*/
            g.drawLine(xcenter, ycenter - 1, xm, ym);
            g.drawLine(xcenter - 1, ycenter, xm, ym);       
            g.drawLine(xcenter, ycenter - 1, xh, yh);
            g.drawLine(xcenter - 1, ycenter, xh, yh);
            g.drawLine(xcenter, ycenter - 2, xh, yh);
            g.drawLine(xcenter - 2, ycenter, xh, yh);              
            /*将当前时间的时、分、秒针的横纵坐标赋给前一次时间对应的变量*/
            lastxs=xs; 
            lastys=ys;
            lastxm=xm; 
            lastym=ym;
            lastxh=xh; 
            lastyh=yh;
            /*将当前日期赋给上一次日期*/
            lastdate = today;
    }
    }

    以下是Clock.html的代码
    <HTML>
    <applet code = "Clock.class" width = 260 height = 160 >
    </applet>
    </HTML>
      

  4.   

    用ajax可以做吧,网上有,你搜搜
      

  5.   

    我觉得windows的这个应该是图片呢。
    毎1000毫秒刷新一次,计算图片各自的角度。只是不知道这个精度好不好控制。
    同样的思路,我认为用Java也应该能做吧。
      

  6.   

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;import javax.swing.*;
    import javax.swing.Timer;
    public class clock extends JPanel implements ActionListener{
    protected Timer timer=new Timer(1000,this);
    public clock(){
    JPanel panel=new JPanel();
    panel.setSize(250, 250);
    panel.setVisible(true);
    timer.start();
    }
    public static void main(String args[]){
    JFrame a =new JFrame();
    a.setVisible(true);
    a.setSize(250,250);
    a.add(new clock());
    }

    protected void paintComponent(Graphics g){
    super.paintComponent(g);
    int hour_x,hour_y,hour_l=25;
    int min_x,min_y,min_l=30;
    int sec_x,sec_y,sec_l=40;

    Date date = new Date(System.currentTimeMillis());
    int hour=date.getHours();
    int minute=date.getMinutes();
    int second=date.getSeconds();
    double pi=Math.PI;
    System.out.println(hour+" "+minute+" "+second);

    g.drawOval(50, 50, 110, 110);

    sec_x=105+(int)(Math.sin(pi/30*second)*sec_l);
    sec_y=105+(int)(Math.cos(pi/30*second)*sec_l);


    min_x=105+(int)(Math.sin(pi/30*minute)*min_l);
    min_y=105+(int)(Math.cos(pi/30*minute)*min_l);

    hour_x=105+(int)(Math.sin((hour)%12+minute/60.0)*pi/6*hour_l);
    hour_y=105+(int)(Math.cos((hour)%12+minute/60.0)*pi/6*hour_l);

    g.drawLine(105, 105, sec_x, sec_y);
    g.drawLine(105, 105, hour_x, hour_y);
    g.drawLine(105, 105, min_x, min_y);
    }
    public void actionPerformed(ActionEvent arg0) {
    repaint();
    }

    }写了个倒着转的,哈哈
      

  7.   

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;import javax.swing.*;
    import javax.swing.Timer;
    public class clock extends JPanel implements ActionListener{
    protected Timer timer=new Timer(1000,this);
    public clock(){
    JPanel panel=new JPanel();
    panel.setSize(250, 250);
    panel.setVisible(true);
    timer.start();
    }
    public static void main(String args[]){
    JFrame a =new JFrame();
    a.setVisible(true);
    a.setSize(250,250);
    a.add(new clock());
    }

    protected void paintComponent(Graphics g){
    super.paintComponent(g);
    int hour_x,hour_y,hour_l=20;
    int min_x,min_y,min_l=30;
    int sec_x,sec_y,sec_l=40;

    Date date = new Date(System.currentTimeMillis());
    int hour=date.getHours();
    int minute=date.getMinutes();
    int second=date.getSeconds();
    double pi=Math.PI;
    System.out.println(hour+" "+minute+" "+second);

    g.drawOval(50, 50, 110, 110);

    sec_x=105+(int)(Math.sin(pi/30*second)*sec_l);
    sec_y=105-(int)(Math.cos(pi/30*second)*sec_l);


    min_x=105+(int)(Math.sin(pi/30*minute)*min_l);
    min_y=105-(int)(Math.cos(pi/30*minute)*min_l);

    hour_x=105+(int)(Math.sin((hour%12+minute/60.0)*pi/6)*hour_l);
    hour_y=105-(int)(Math.cos((hour%12+minute/60.0)*pi/6)*hour_l);

    g.drawLine(105, 105, sec_x, sec_y);
    g.drawLine(105, 105, hour_x, hour_y);
    g.drawLine(105, 105, min_x, min_y);
    }
    public void actionPerformed(ActionEvent arg0) {
    repaint();
    }

    }这个是正确的,O(∩_∩)O~
      

  8.   

    g.drawString("12", 105-6, 105-55+13);
    g.drawString("6", 105-3, 105+55-5);
    g.drawString("9", 105-55, 105+3);
    g.drawString("3", 105+55-6, 105+3);
    加上这个就能在表上面显示几点了,O(∩_∩)O~
      

  9.   

    200分......咋都是AWT SWING的呢????
      

  10.   

            int hour=date.getHours();
            int minute=date.getMinutes();
            int second=date.getSeconds();
    这些不都是过时的方法了么???  
      

  11.   

    哎 你们是看的jdk自带的 画时钟的吧。。 自己写个ajax的或者是  js的撒。 
      

  12.   

    自己看一下基础数学知识,主要是三角函数部分。
    sin cos 之类的。
    圆心已经定了,根据时间计算出角度,指针的长度也定了,剩下那个点的坐标还计算不出来吗?
      

  13.   

    学习了。。<HTML><HEAD> 
    <META http-equiv=Content-Type content="text/html; charset=gb2312"> 
    <META content="MSHTML 6.00.6000.16414" name=GENERATOR></HEAD> 
    <BODY> 
    <DIV  
    style="LEFT: 155px; WIDTH: 400px; ZOOM: 1; POSITION: absolute; TOP: 133px; HEIGHT: 300px"> 
    <DIV id=bg  
    style="LEFT: -22px; WIDTH: 150px; ZOOM: 1.5; POSITION: absolute; TOP: -57px; HEIGHT: 150px"><IMG  
    style="Z-INDEX: 22; WIDTH: 129px; HEIGHT: 129px" src="/upload/novelty.gif">  
    </DIV> 
    <DIV id=h  
    style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; WIDTH: 129px; PADDING-TOP: 5px; POSITION: absolute; HEIGHT: 129px"><IMG  
    style="Z-INDEX: 22; WIDTH: 129px; HEIGHT: 129px" src="/upload/novelty_h.gif">  
    </DIV> 
    <DIV id=m  
    style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; WIDTH: 129px; PADDING-TOP: 5px; POSITION: absolute; HEIGHT: 129px"><IMG  
    style="Z-INDEX: 22; WIDTH: 129px; HEIGHT: 129px" src="/upload/novelty_m.gif">  
    </DIV> 
    <DIV id=s  
    style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; WIDTH: 129px; PADDING-TOP: 5px; POSITION: absolute; HEIGHT: 129px"><IMG  
    style="Z-INDEX: 22; WIDTH: 129px; HEIGHT: 129px" src="/upload/novelty_s.gif">  
    </DIV> 
    <DIV id=dot  
    style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; WIDTH: 129px; PADDING-TOP: 5px; POSITION: absolute; HEIGHT: 129px"><IMG  
    style="Z-INDEX: 22; WIDTH: 129px; HEIGHT: 129px" src="/upload/novelty_dot.gif">  
    </DIV> 
    <DIV> 
    <SCRIPT> 
    //oObj input requires that a matrix filter be applied.  
    //deg input defines the requested angle of rotation. 
    var deg2radians = Math.PI * 2 / 360; 
    function MatrixFilter(obj) 
    {     
        if(!obj.filters)return; 
        //alert(obj.filters.item(0)); 
        var Matrix; 
        for(p in obj.filters) 
        {        
            if(p=="DXImageTransform.Microsoft.Matrix")Matrix=obj.filters["DXImageTransform.Microsoft.Matrix"];   
        } 
        if(!Matrix) 
        { 
            obj.style.filter+="progid:DXImageTransform.Microsoft.Matrix()"; 
        } 
        Matrix=obj.filters["DXImageTransform.Microsoft.Matrix"]; 
        this.equal=function(Matrix2D_x) 
        { 
            if(Matrix2D_x.M11)Matrix.M11 = Matrix2D_x.M11; 
            if(Matrix2D_x.M12)Matrix.M12 = Matrix2D_x.M12; 
            if(Matrix2D_x.M21)Matrix.M21 = Matrix2D_x.M21; 
            if(Matrix2D_x.M22)Matrix.M22 = Matrix2D_x.M22; 
        } 
         
        if(arguments[1])this.equal(arguments[1]); 
         
        this.Rotate=function(deg) 
        { 
            rad = deg * deg2radians; 
            costheta = Math.cos(rad); 
            sintheta = Math.sin(rad); 
            var d=new Matrix2D(costheta,-sintheta,sintheta,costheta); 
            this.equal(Matrix2D.Mul(Matrix,d)); 
        } 
        this.RotateTo=function(deg) 
        { 
            rad = deg * deg2radians; 
            costheta = Math.cos(rad); 
            sintheta = Math.sin(rad); 
            var d=new Matrix2D(costheta,-sintheta,sintheta,costheta); 
            this.equal(d); 
        } 
        this.RotateAt=function(deg,sx,sy) 
        { 
            rad = deg * deg2radians; 
            costheta = Math.cos(rad); 
            sintheta = Math.sin(rad); 
            var d=new Matrix2D(costheta,-sintheta,sintheta,costheta); 
            var x=sx-Matrix.Dx; 
            var y=sy-Matrix.Dy; 
            this.MoveTo(x*costheta+y*sintheta-x,-x*sintheta+y*costheta-y); 
            this.equal(Matrix2D.Mul(Matrix,d));        
        } 
        this.RotateToAt=function(deg,sx,sy) 
        { 
            rad = deg * deg2radians; 
            costheta = Math.cos(rad); 
            sintheta = Math.sin(rad); 
            var d=new Matrix2D(costheta,-sintheta,sintheta,costheta); 
            var x=sx; 
            var y=sy; 
            this.MoveTo(x-(x*costheta-y*sintheta),-(x*sintheta+y*costheta-x)); 
            this.equal(d); 
        }     this.MoveTo=function(sx,sy) 
        { 
            Matrix.Dx=sx; 
            Matrix.Dy=sy; 
        } 
        this.toMatrix2D=function() 
        { 
            return new Matrix2D(Matrix.M11,Matrix.M12,Matrix.M21,Matrix.M22); 
        } 
        this.ZoomBy=function(sx,sy) 
        { 
            var d=new Matrix2D(sx,0,0,sy); 
            this.equal(Matrix2D.Mul(Matrix,d)); 
        } 
        this.toString=function() 
        { 
            return ""+Matrix.M11+" "+Matrix.M12+"\n"+Matrix.M21+" "+Matrix.M22+"\n" 
        } 
        //Matrix.SizingMethod='clip to original'; 
        //this.fnSetRotation(30); 
        //alert(Matrix.M11); 
        //alert(obj.filters["DXImageTransform.Microsoft.Matrix"]); 

    function Matrix2D() 

        this.M11 = arguments[0]||1; 
        this.M12 = arguments[1]||0; 
        this.M21 = arguments[2]||0; 
        this.M22 = arguments[3]||1; 
        this.Mul_Matrix2D=function(Matrix2D_b) 
        { 
            var r=new Matrix2D(); 
            r=Matrix2D.Mul(this,Matrix2D_b);         
            return r;        
        } 
        this.toString=function() 
        { 
            return ""+this.M11+" "+this.M12+"\n"+this.M21+" "+this.M22+"\n" 
        } 

    Matrix2D.Mul=function(Matrix2D_a,Matrix2D_b) 

        var r=new Matrix2D(); 
        r.M11=Matrix2D_a.M11*Matrix2D_b.M11+Matrix2D_a.M12*Matrix2D_b.M21; 
        r.M12=Matrix2D_a.M11*Matrix2D_b.M12+Matrix2D_a.M12*Matrix2D_b.M22; 
        r.M21=Matrix2D_a.M21*Matrix2D_b.M11+Matrix2D_a.M22*Matrix2D_b.M21; 
        r.M22=Matrix2D_a.M21*Matrix2D_b.M12+Matrix2D_a.M22*Matrix2D_b.M22; 
        return r;      

    var ms=new MatrixFilter(s); 
    var mm=new MatrixFilter(m); 
    var mh=new MatrixFilter(h); 
    var i=1; 
    setInterval("ms.RotateToAt((new Date()).getSeconds()*6+6,69,69)",500); 
    setInterval("mm.RotateToAt((new Date()).getMinutes()*6+6,69,69)",500); 
    setInterval("mh.RotateToAt(((new Date()).getHours()+(new Date()).getMinutes()/60)*30,69,69)",500); 
    //mf.MoveTo(30,70); 
    //mf.ZoomBy(1.5,1.5); 
    //mf.ZoomBy(1.5,1.5); 
    //alert(mf.toMatrix2D()); //alert(Matrix2D.Mul(m2d1,m2d2)); 
    //fnSetRotation(oDiv.filters.item(0),30); 
    </SCRIPT> 
    </DIV></DIV></BODY></HTML> 
      

  14.   

    肯定是画出来的了,Java2D就可以了,Windows用GDI或者WPF,都不是什么难事。