// PlanetsWindow.java (c) 2004 Kari Laitinen // 2004-10-21 File created. // 2006-09-08 Latest modification. // This program is an example in which a menu is // related to an array of objects. import java.awt.* ; import java.awt.event.* ; class Planet { String planet_name ; double mean_distance_from_sun ; double circulation_time_around_sun ; double rotation_time_around_own_axis ; long equatorial_radius_in_kilometers ; double mass_compared_to_earth ; int number_of_moons ; public Planet( String given_planet_name, double given_mean_distance_from_sun, double given_circulation_time_around_sun, double given_rotation_time_around_own_axis, long given_equatorial_radius_in_kilometers, double given_mass_compared_to_earth, int given_number_of_moons ) { planet_name = given_planet_name ; mean_distance_from_sun = given_mean_distance_from_sun ; circulation_time_around_sun = given_circulation_time_around_sun ; rotation_time_around_own_axis = given_rotation_time_around_own_axis ; equatorial_radius_in_kilometers = given_equatorial_radius_in_kilometers ; mass_compared_to_earth = given_mass_compared_to_earth ; number_of_moons = given_number_of_moons ; } public String get_planet_name() { return planet_name ; } public void draw_planet_data( int desired_position_x, int desired_position_y, Graphics graphics ) { graphics.drawString( "" + planet_name + " orbits the sun in average distance of " + ( mean_distance_from_sun * 149500000 ) + " kilometers.", desired_position_x, desired_position_y ) ; graphics.drawString( "It takes " + circulation_time_around_sun + " years for " + planet_name + " to go around the sun once.", desired_position_x, desired_position_y + 20 ) ; graphics.drawString( "The radius of " + planet_name + " is " + equatorial_radius_in_kilometers + " kilometers.", desired_position_x, desired_position_y + 40 ) ; int diameter_to_be_used_in_drawing = (int) equatorial_radius_in_kilometers / 400 ; graphics.fillOval( desired_position_x, desired_position_y + 60, diameter_to_be_used_in_drawing, diameter_to_be_used_in_drawing ) ; } } public class PlanetsWindow extends Frame implements ActionListener, WindowListener { static final int WINDOW_WIDTH = 600 ; static final int WINDOW_HEIGHT = 400 ; Planet[] planet_table = new Planet[ 30 ] ; Planet currently_selected_planet ; PlanetsWindow() { planet_table[ 0 ] = new Planet( "Mercury", 0.387, 0.241, 58.815, 2433, 0.05, 0 ) ; planet_table[ 1 ] = new Planet( "Venus", 0.723, 0.615, 224.588, 6053, 0.82, 0 ) ; planet_table[ 2 ] = new Planet( "Earth", 1.000, 1.000, 1.000, 6379, 1.00, 1 ) ; planet_table[ 3 ] = new Planet( "Mars", 1.523, 1.881, 1.029, 3386, 0.11, 2 ) ; planet_table[ 4 ] = new Planet( "Jupiter", 5.203, 11.861, 0.411, 71370, 317.93, 12 ) ; planet_table[ 5 ] = new Planet( "Saturn", 9.541, 29.457, 0.428, 60369, 95.07, 10 ) ; planet_table[ 6 ] = new Planet( "Uranus", 19.190, 84.001, 0.450, 24045, 14.52, 5 ) ; planet_table[ 7 ] = new Planet( "Neptune", 30.086, 164.784, 0.657, 22716, 17.18, 2 ) ; planet_table[ 8 ] = new Planet( "Pluto", 39.507, 248.35, 6.410, 5700, 0.18, 0 ) ; currently_selected_planet = planet_table[ 0 ] ; Menu planet_selection_menu = new Menu( "Planet" ) ; int planet_index = 0 ; while ( planet_table[ planet_index ] != null ) { MenuItem planet_menu_item = new MenuItem( planet_table[ planet_index ].get_planet_name() ) ; planet_menu_item.addActionListener( this ) ; planet_selection_menu.add( planet_menu_item ) ; planet_index ++ ; } MenuBar menubar_of_this_window = new MenuBar() ; menubar_of_this_window.add( planet_selection_menu ) ; setMenuBar( menubar_of_this_window ) ; // This class implements a WindowListener interface. addWindowListener( this ) ; setSize( WINDOW_WIDTH, WINDOW_HEIGHT ) ; setTitle( "PLANET DATA FINDER" ) ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof MenuItem ) { String name_of_selected_item = ( (MenuItem) event.getSource() ).getLabel() ; int planet_index = 0 ; while ( planet_table[ planet_index ] != null ) { if ( name_of_selected_item.equals( planet_table[ planet_index ].get_planet_name() ) ) { currently_selected_planet = planet_table[ planet_index ] ; } planet_index ++ ; } repaint() ; } } public void windowClosing( WindowEvent event ) { setVisible( false ) ; System.exit( 0 ) ; } // Other methods required by the WindowListener interface: public void windowActivated( WindowEvent event ) {} public void windowClosed( WindowEvent event ) {} public void windowDeactivated( WindowEvent event ) {} public void windowDeiconified( WindowEvent event ) {} public void windowIconified( WindowEvent event ) {} public void windowOpened( WindowEvent event ) {} public void paint( Graphics graphics ) { currently_selected_planet.draw_planet_data( 100, 100, graphics ) ; } public static void main( String[] not_in_use ) { PlanetsWindow this_application_window = new PlanetsWindow() ; this_application_window.setVisible( true ) ; } }