为什么我的时钟不能很好地工作?

时间:2014-01-12 03:31:51

标签: java clock turtle-graphics

我目前正在使用StandardPen类处理java中的时钟。一切似乎都正常工作,除非我在运行程序时达到大约6-10分钟,时钟开始出现故障。在添加时针后,错误似乎就出现了。 时针与第二手和分针不同的原因是因为thread.sleep(20)无法使用时针的速度。有人请帮我快点解决这个问题

import java.awt.Color;
import TurtleGraphics.StandardPen;

public class Clock_Final {
    public static void main(String[] args) {

        StandardPen pen = new StandardPen();

        double num=90, num1 = 90, num3 = 90, loop = 1, minute = 90, minuteclear = 90, cont = 0, cont2 = 0, tick1 = 90, hour = 90, hourclear = 90, hourcount = 0, hourcount1 = 0;
        String three = "3", six = "6", nine = "9", twelve = "12", signature = "Created by: Me";

        pen.setColor(Color.black);
        pen.up();
        pen.setDirection(270);
        pen.move(350);
        pen.setDirection(180);
        pen.move(60);
        pen.drawString(signature);
        pen.down();
        pen.home();
//twelve
        pen.setColor(Color.black);
        pen.up();
        pen.setDirection(90);
        pen.move(280);
        pen.setDirection(180);
        pen.move(7);
        pen.down();
        pen.drawString(twelve);
        pen.home();
//three
        pen.up();
        pen.setDirection(0);
        pen.move(278);
        pen.setDirection(270);
        pen.move(5);
        pen.down();
        pen.drawString(three);
        pen.home();

//six                        
        pen.up();
        pen.setDirection(270);
        pen.move(290);
        pen.setDirection(180);
        pen.move(3);
        pen.down();
        pen.drawString(six);
        pen.home();

//nine
        pen.up();
        pen.setDirection(180);
        pen.move(285);
        pen.setDirection(270);
        pen.move(5);
        pen.down();
        pen.drawString(nine);
        pen.home();
//_-_-_outside of the clock (the circle)
        for (cont = 0; cont < 360; cont++) {
            pen.setColor(Color.black);
            pen.setDirection(num);
            pen.setWidth(8);
            pen.up();
            pen.move(270);
            pen.down();
            pen.move(2);
            pen.home();
            num--;

            if (num==0) {
                num = 360;
            }
        }
     //tick start
        for (cont2=0;cont2<60;cont2++) {
            pen.setColor(Color.gray);

            pen.setDirection(tick1);
            pen.setWidth(3);
            pen.up();
//method for minute/second ticks (start)
            if (tick1 == 0 || tick1 == 30 || tick1 == 60 || tick1 == 90 || tick1 == 120 || tick1 == 150 || tick1 == 180 || tick1 == 210 || tick1 == 240 || tick1 == 270 || tick1 == 300 || tick1 == 330) {
                if (tick1 == 0 || tick1 == 90 || tick1 == 180 || tick1 == 270) {
                    pen.setColor(Color.black);
                    pen.setWidth(5);
                    pen.move(225);
                    pen.down();
                    pen.move(39);
                    pen.setColor(Color.gray);
                } else {
                    pen.setColor(Color.darkGray);
                    pen.move(230);
                    pen.down();
                    pen.move(34);
                    pen.setColor(Color.gray);
                }
            } //method for minute ticks (end)
            else if (tick1 != 0 || tick1 != 30 || tick1 != 60 || tick1 != 90 || tick1 != 120 || tick1 != 150 || tick1 != 180 || tick1 != 210 || tick1 != 240 || tick1 != 270 || tick1 != 300 || tick1 != 330) {
                pen.move(250);
                pen.down();
                pen.move(14);
            }
            pen.home();
            if (tick1 == 0) {
                tick1 = 360;
            }
            tick1 = tick1 - 6;
        }
        while (loop == 1) {
        pen.setWidth(2);
        //draw new second hand
                pen.setColor(Color.red);
                pen.setDirection(num1);
                pen.move(220);

                if (num1 < .05) {
                    num1 = 360;
                }
                num1 -= .3;

        //draw new minute hand 
                pen.home();
                pen.setColor(Color.darkGray);
                pen.setDirection(minute);
                pen.move(200);

                if (minute == 1) {
                    minute = 360;
                }
                minute -= .005;

        //draw new hour hand 
                pen.home();
                pen.setColor(Color.darkGray);
                pen.setDirection(hour);
                pen.move(160);

                if (hour == 0) {
                    hour = 360;
                }
                hourcount+=.05;
                if (hourcount % 360 == 0) {
                    hour -= 1;
                }

                //sleep
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

    //clear old second hand
                pen.home();
                pen.setColor(Color.white);
                pen.setDirection(num3);
                pen.move(220);

                if (num3 < .05) {
                    num3 = 360;
                }
                num3 -= .3;

                pen.home();

    //clear old minute hand
                pen.home();
                pen.setColor(Color.white);
                pen.setDirection(minuteclear);
                pen.move(200);

                if (minuteclear == 0) {
                    minuteclear = 360;
                }
                minuteclear -= .005;

                pen.home();

    //clear old hour hand
                pen.home();
                pen.setColor(Color.white);
                pen.setDirection(hourclear);
                pen.move(160);

                if (hourclear == 0) {
                    hourclear = 360;
                }
                hourcount1+=.05;
                if (hourcount1 % 360 == 0) {
                    hourclear -= 1;
                }

                pen.home();

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你的意思是&#34;小故障&#34;你的意思是浪费时间?使用sleep()来驱动你的时钟的问题在于你永远不知道它已经睡了多长时间,并且绘制你的时钟也需要时间。小的定时误差会随着时间的推移而累积。多任务处理和线程调度意味着你永远不知道实际经过了多少时间。你仍然可以使用sleep()来决定何时绘制你的时钟,但你需要&#34;得到&#34;实际时间并根据它绘制时钟。请参阅:new Date()和GregorianCalendar()

Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(new Date()); // current time
int mins = calendar.get(Calendar.MINUTE);
// etc..

这是我写的一个使用这种方法的时钟渲染器...

https://code.google.com/p/catchpole/source/browse/trunk/catchpole-fuse/src/main/java/net/catchpole/awt/ClockFaceRenderer.java