// ClockOldApplet.java (c) 2000-2009 Kari Laitinen // http://www.naturalprogramming.com/ // 2000-??-?? File created. // 2009-10-13 Last modification. /* This program prints a clock onto the screen once in every second. The clock thus seems to run when this program is executing. This program demonstrates: - how to use a thread that maintains animation - how to get the current time and date by "reading" the computer's clock See program ClockWithDate.java to find out how to to build a better graphical clock. */ import java.awt.* ; import javax.swing.* ; import java.util.* ; // Calendar and GregorianCalendar classes are here. public class ClockOldApplet extends JApplet implements Runnable { Thread thread_that_runs_the_clock ; boolean thread_must_be_executed ; public void start() { if ( thread_that_runs_the_clock == null ) { thread_must_be_executed = true ; thread_that_runs_the_clock = new Thread( this ) ; thread_that_runs_the_clock.start() ; } } public void stop() { if ( thread_that_runs_the_clock != null ) { thread_must_be_executed = false ; thread_that_runs_the_clock.interrupt() ; thread_that_runs_the_clock = null ; } } public void run() { while ( thread_must_be_executed == true ) { repaint() ; try { Thread.sleep( 1000 ) ; // Suspend for 1 second. } catch ( InterruptedException caught_exception ) { // No actions to handle the exception. } } } public void paint( Graphics graphics ) { super.paint( graphics ) ; String[] days_of_week = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" } ; String[] names_of_months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; Calendar time_now = new GregorianCalendar() ; int current_year = time_now.get( Calendar.YEAR ) ; int current_day = time_now.get( Calendar.DAY_OF_MONTH ) ; int month_index = time_now.get( Calendar.MONTH ) ; int number_of_day_of_week = time_now.get( Calendar.DAY_OF_WEEK ) ; String current_month = names_of_months[ month_index ] ; String current_day_of_week = days_of_week[ number_of_day_of_week - 1 ] ; int current_hours = time_now.get( Calendar.HOUR_OF_DAY ) ; int current_minutes = time_now.get( Calendar.MINUTE ) ; int current_seconds = time_now.get( Calendar.SECOND ) ; graphics.drawString( "" + current_day_of_week + ", " + current_month + " " + current_day + ", " + current_year , 20, 20 ) ; graphics.drawString( String.format( "%d:%02d:%02d", current_hours, current_minutes, current_seconds ), 20, 40 ) ; int clock_center_point_x = 300 ; int clock_center_point_y = 200 ; int[] minute_hand_end_points_x = { 0, 11, 21, 31, 41, 50, 59, 67, 74, 81, 87, 91, 95, 97, 99, 100, 99, 97, 95, 91, 87, 81, 74, 67, 59, 50, 41, 31, 21, 11, 0, -11, -21, -31, -41, -50, -59, -67, -74, -81, -87, -91, -95, -97, -99, -100, -99, -97, -95, -91, -87, -81, -74, -67, -59, -50, -41, -31, -21, -11 } ; int[] minute_hand_end_points_y = { -100, -99, -97, -95, -91, -87, -81, -74, -67, -59, -50, -41, -31, -21, -11, 0, 11, 21, 31, 41, 50, 59, 67, 74, 81, 87, 91, 95, 97, 99, 100, 99, 97, 95, 91, 87, 81, 74, 67, 59, 50, 41, 31, 21, 11, 0, -11, -21, -31, -41, -50, -59, -67, -74, -81, -87, -91, -95, -97, -99 } ; int[] hour_hand_end_points_x = { 0, 7, 13, 19, 24, 30, 35, 40, 44, 48, 52, 55, 57, 58, 59, 60, 59, 58, 57, 55, 52, 48, 44, 40, 35, 30, 24, 19, 13, 7, 0, -7, -13, -19, -24, -30, -35, -40, -44, -48, -52, -55, -57, -58, -59, -60, -59, -58, -57, -55, -52, -48, -44, -40, -35, -30, -24, -19, -13, -7 } ; int[] hour_hand_end_points_y = { -60, -59, -58, -57, -55, -52, -48, -44, -40, -35, -30, -24, -19, -13, -7, 0, 7, 13, 19, 24, 30, 35, 40, 44, 48, 52, 55, 57, 58, 59, 60, 59, 58, 57, 55, 52, 48, 44, 40, 35, 30, 24, 19, 13, 7, 0, -7, -13, -19, -24, -30, -35, -40, -44, -48, -52, -55, -57, -58, -59 } ; // Let's print an 8-point dot in the center of the clock. graphics.fillOval( clock_center_point_x - 4, clock_center_point_y - 4, 8, 8 ) ; // The following loop prints dots on the clock circle. int minute_index = 0 ; while ( minute_index < 60 ) { graphics.fillOval( clock_center_point_x + minute_hand_end_points_x[ minute_index ] - 2, clock_center_point_y + minute_hand_end_points_y[ minute_index ] - 2, 4, 4 ) ; minute_index = minute_index + 5 ; } // A clock goes through its 12-hour scale twice every day. int hour_index ; if ( current_hours >= 12 ) { hour_index = current_hours - 12 ; } else { hour_index = current_hours ; } // Remember that we have 60 minutes in every hour, // but not 60 hours in a day. hour_index = hour_index * 5 + current_minutes / 12 ; // Let's draw the hour hand of the clock. graphics.drawLine( clock_center_point_x, clock_center_point_y, clock_center_point_x + hour_hand_end_points_x[ hour_index ], clock_center_point_y + hour_hand_end_points_y[ hour_index ] ) ; // The minute and second hands are longer than // the hour hand. Therefore we use different // coordinates to print them. graphics.drawLine( clock_center_point_x, clock_center_point_y, clock_center_point_x + minute_hand_end_points_x[ current_minutes ], clock_center_point_y + minute_hand_end_points_y[ current_minutes ] ); graphics.drawLine( clock_center_point_x, clock_center_point_y, clock_center_point_x + minute_hand_end_points_x[ current_seconds ], clock_center_point_y + minute_hand_end_points_y[ current_seconds ]); } }