// BirthdaysApplet.java (c) Kari Laitinen // 2006-02-17 File created. // 2009-01-29 Latest modification. /* This applet is a GUI version of program BirthdaysGregorianCalendar.java. With this program you can check on what days of week your important birthdays occur. */ import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import java.util.Calendar ; import java.util.GregorianCalendar ; class BirthdaysPanel extends JPanel implements ActionListener, ItemListener { int selected_decade = 0 ; JComboBox list_of_years, list_of_months, list_of_days ; JButton previous_decade_button, next_decade_button, mode_button, help_button ; public BirthdaysPanel() { setLayout( new BorderLayout() ) ; list_of_years = new JComboBox() ; list_of_months = new JComboBox() ; list_of_days = new JComboBox() ; Font font_for_combo_boxes = new Font( "Courier", Font.BOLD, 14 ) ; list_of_years.setFont( font_for_combo_boxes ) ; list_of_months.setFont( font_for_combo_boxes ) ; list_of_days.setFont( font_for_combo_boxes ) ; for ( int selectable_birth_year = 1850 ; selectable_birth_year < 2050 ; selectable_birth_year ++ ) { list_of_years.addItem( "" + selectable_birth_year); } for ( int selectable_birth_month = 1 ; selectable_birth_month < 13 ; selectable_birth_month ++ ) { list_of_months.addItem( String.format( "%2d", selectable_birth_month ) ) ; } for ( int selectable_birth_day = 1 ; selectable_birth_day < 32 ; selectable_birth_day ++ ) { list_of_days.addItem( String.format( "%2d", selectable_birth_day ) ); } list_of_years.setSelectedIndex( 130 ) ; list_of_months.setSelectedIndex( 11 ) ; list_of_days.setSelectedIndex( 30 ) ; list_of_years.setMaximumRowCount( 11 ) ; list_of_days.setMaximumRowCount( 11 ) ; list_of_years.addItemListener( this ) ; list_of_months.addItemListener( this ) ; list_of_days.addItemListener( this ) ; JPanel birthday_selections_panel = new JPanel() ; birthday_selections_panel.add( new JLabel( "SET YOUR BIRTHDAY:" ) ); birthday_selections_panel.add( list_of_years ) ; birthday_selections_panel.add( list_of_months ) ; birthday_selections_panel.add( list_of_days ) ; birthday_selections_panel.add( new JLabel( "Format is YYYY-MM-DD" ) ); add( "North", birthday_selections_panel ) ; previous_decade_button = new JButton( "<< DECADE" ) ; next_decade_button = new JButton( "DECADE >>" ) ; mode_button = new JButton( "DAYS MODE" ) ; help_button = new JButton( "HELP" ) ; previous_decade_button.addActionListener( this ) ; next_decade_button.addActionListener( this ) ; mode_button.addActionListener( this ) ; help_button.addActionListener( this ) ; // We'll disable the Mode and Help buttons because they // cannot be used yet. mode_button.setEnabled( false ) ; help_button.setEnabled( false ) ; JPanel southern_operations_panel = new JPanel() ; southern_operations_panel.add( previous_decade_button ) ; southern_operations_panel.add( next_decade_button ) ; southern_operations_panel.add( mode_button ) ; southern_operations_panel.add( help_button ) ; add( "South", southern_operations_panel ) ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { if ( event.getSource() == previous_decade_button ) { selected_decade = selected_decade - 10 ; } else if ( event.getSource() == next_decade_button ) { selected_decade = selected_decade + 10 ; } else if ( event.getSource() == mode_button ) { } else if ( event.getSource() == help_button ) { } repaint() ; } } public void itemStateChanged( ItemEvent event ) { // selected_decade = 0 ; System.out.print( " I " ) ; repaint() ; } public void paintComponent( Graphics graphics ) { // The superclass version of paintComponent() clears the // component area and does other necessary things. super.paintComponent( 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 date_of_birth = new GregorianCalendar( Integer.parseInt( ((String) list_of_years.getSelectedItem()).trim() ), Integer.parseInt( ((String) list_of_months.getSelectedItem()).trim() ) - 1, Integer.parseInt( ((String) list_of_days.getSelectedItem()).trim() ) ) ; graphics.setFont( new Font( "Courier", Font.BOLD, 12 ) ) ; int printing_position_y = 100 ; int years_to_celebrate = selected_decade ; while ( years_to_celebrate < 12 + selected_decade ) { Calendar date_to_celebrate = new GregorianCalendar( date_of_birth.get( Calendar.YEAR ) + years_to_celebrate, date_of_birth.get( Calendar.MONTH ), date_of_birth.get( Calendar.DAY_OF_MONTH ) ) ; String line_to_screen = String.format( "%3d years on %10s, %s %d, %d", years_to_celebrate, days_of_week[ date_to_celebrate.get( Calendar.DAY_OF_WEEK ) - 1 ], names_of_months[ date_to_celebrate.get( Calendar.MONTH ) ], date_to_celebrate.get( Calendar.DAY_OF_MONTH ), date_to_celebrate.get( Calendar.YEAR ) ) ; graphics.drawString( line_to_screen, 100, printing_position_y ) ; printing_position_y = printing_position_y + 20 ; years_to_celebrate = years_to_celebrate + 1 ; } } } public class BirthdaysApplet extends JApplet { public void init() { getContentPane().add( new BirthdaysPanel() ) ; } }