0
votes

I am currently working on a clock in java using the StandardPen class. Everything seems to work right except for when i reach about 6-10 minutes while running the program, the clock starts glitching out. The error seems to have come along right after I added the hour hand. The reason the hour hand is code different from the second and minute hand is because of the thread.sleep(20) not being able to work with the hour hand's speed. Somebody please help me fix this fast please

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

1 Answers

0
votes

i assume you mean by "glitching out", you mean losing time? The problem with using sleep() to drive your clock is that you never really know how long it has slept for, and drawing your clock takes time also. small timing errors will accumulate over time. also multitasking and thread scheduling means you never know how much time has actually passed. you can still use the sleep()s to decide when to draw your clock, but you need to "get" the actual time and draw the clock based on that. See: new Date() and GregorianCalendar()

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

Here is a clock renderer i wrote which uses this method...

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