// ClockOldMIDlet.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2002-11-?? File created. // 2008-06-05 Minor cosmetic modifications. // 2009-10-02 Name change ClockMIDlet.java --> ClockOldMIDlet.java // ClockMIDlet.java contains now a renewed version. // This program works much in the same way as ClockApplet.java. // The purpose of this program is to show how a thread can be used // in a midlet. In addition, this program shows how the current time // can be obtained into a midlet program. // It is possible that this program does not show the correct time // when it is executed in an emulator. In a real phone, however, the // this program seems to show the correct time. import java.util.* ; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; class ClockCanvas extends Canvas implements Runnable { Thread thread_that_runs_the_clock ; boolean thread_must_be_executed = false ; Calendar time_now ; int canvas_width, canvas_height ; int clock_center_point_x, clock_center_point_y ; public ClockCanvas() { canvas_width = getWidth() ; canvas_height = getHeight() ; clock_center_point_x = canvas_width / 2 ; clock_center_point_y = canvas_height / 2 + 10 ; } public synchronized void start_animation_thread() { if ( thread_that_runs_the_clock == null ) { thread_that_runs_the_clock = new Thread( this ) ; thread_must_be_executed = true ; thread_that_runs_the_clock.start() ; } } public void stop_animation_thread() { 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 ) { String[] days_of_week = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } ; String[] names_of_months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ; time_now = Calendar.getInstance() ; 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.setColor( 255, 255, 255 ) ; // white graphics.fillRect( 0, 0, canvas_width, canvas_height ) ; graphics.setColor( 0, 0, 0 ) ; // black graphics.drawString( "" + current_day_of_week + " " + current_month + " " + current_day + ", " + current_year, 2, 0, Graphics.TOP | Graphics.LEFT ) ; // The following statements ensure that leading zeroes // precede single-digit minutes and seconds. String minutes_string = "00" + current_minutes ; minutes_string = minutes_string.substring( minutes_string.length() - 2, minutes_string.length() ) ; String seconds_string = "00" + current_seconds ; seconds_string = seconds_string.substring( seconds_string.length() - 2, seconds_string.length() ) ; graphics.drawString( current_hours + ":" + minutes_string + ":" + seconds_string, 2, 12, Graphics.TOP | Graphics.LEFT ) ; /* The following coordinates were originally developed for a larger clock on a larger display. In this program they are divided by 3 in order to get coordinates that are suitable for smaller displays. */ 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.fillArc( clock_center_point_x - 4, clock_center_point_y - 4, 8, 8, 0, 360 ) ; // The following loop prints dots on the clock circle. int minute_index = 0 ; while ( minute_index < 60 ) { graphics.fillArc( clock_center_point_x + minute_hand_end_points_x[ minute_index ] / 3 - 2, clock_center_point_y + minute_hand_end_points_y[ minute_index ] / 3 - 2, 4, 4, 0, 360 ) ; 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 ] / 3, clock_center_point_y + hour_hand_end_points_y[ hour_index ] / 3 ) ; // 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 ] / 3, clock_center_point_y + minute_hand_end_points_y[ current_minutes ] / 3 ); graphics.drawLine( clock_center_point_x, clock_center_point_y, clock_center_point_x + minute_hand_end_points_x[ current_seconds ] / 3, clock_center_point_y + minute_hand_end_points_y[ current_seconds ] / 3 ); } } public class ClockOldMIDlet extends MIDlet implements CommandListener { Display midlet_display = Display.getDisplay( this ) ; ClockCanvas clock_canvas = new ClockCanvas() ; Command exit_command = new Command( "Exit", Command.EXIT, 1 ) ; protected void startApp() throws MIDletStateChangeException { midlet_display.setCurrent( clock_canvas ) ; clock_canvas.start_animation_thread() ; clock_canvas.addCommand( exit_command ) ; clock_canvas.setCommandListener( this ) ; } protected void pauseApp() { clock_canvas.stop_animation_thread() ; } protected void destroyApp( boolean unconditional_destruction_required ) { clock_canvas.stop_animation_thread() ; } public void commandAction( Command given_command, Displayable display_content ) { if ( given_command == exit_command ) { destroyApp( false ) ; notifyDestroyed() ; } } }