// MovingBallAWTApplication.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2002-09-16 File created. // 2009-11-05 Latest modification. // This program is a Java application that is similar to // applet MovingBallAWTApplet.java. // This program represents 'old' Java technology as it is not a // swing program. import java.awt.* ; import java.awt.event.* ; public class MovingBallAWTApplication extends Frame implements ActionListener, ItemListener, WindowListener { static final int WINDOW_WIDTH = 600 ; static final int WINDOW_HEIGHT = 480 ; Button left_button, up_button, down_button, right_button ; int ball_center_point_x, ball_center_point_y ; Color current_ball_color = Color.red ; public MovingBallAWTApplication() { setLayout( new BorderLayout() ) ; Panel operations_panel = new Panel() ; left_button = new Button( " < " ) ; up_button = new Button( " Up " ) ; down_button = new Button( " Down " ) ; right_button = new Button( " > " ) ; left_button.addActionListener( this ) ; up_button.addActionListener( this ) ; down_button.addActionListener( this ) ; right_button.addActionListener( this ) ; operations_panel.add( left_button ) ; operations_panel.add( up_button ) ; operations_panel.add( down_button ) ; operations_panel.add( right_button ) ; Choice color_selection_menu = new Choice() ; color_selection_menu.addItem( "red" ) ; color_selection_menu.addItem( "orange" ) ; color_selection_menu.addItem( "yellow" ) ; color_selection_menu.addItem( "green" ) ; color_selection_menu.addItem( "blue" ) ; color_selection_menu.addItem( "magenta" ) ; color_selection_menu.addItem( "cyan" ) ; color_selection_menu.addItem( "pink" ) ; color_selection_menu.addItem( "lightGray" ) ; color_selection_menu.addItemListener( this ) ; operations_panel.add( color_selection_menu ) ; add( "South", operations_panel ) ; // This class implements a WindowListener interface. addWindowListener( this ) ; setSize( WINDOW_WIDTH, WINDOW_HEIGHT ) ; setTitle( "Window for a Moving Ball" ) ; ball_center_point_x = WINDOW_WIDTH / 2 ; ball_center_point_y = WINDOW_HEIGHT / 2 ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof Button ) { if ( event.getSource() == left_button ) { ball_center_point_x -= 3 ; } else if ( event.getSource() == up_button ) { ball_center_point_y -= 3 ; } else if ( event.getSource() == down_button ) { ball_center_point_y += 3 ; } else if ( event.getSource() == right_button ) { ball_center_point_x += 3 ; } repaint() ; } } public void itemStateChanged( ItemEvent menu_selection ) { String selected_color = (String) menu_selection.getItem() ; if ( selected_color.equals( "red" ) ) { current_ball_color = Color.red ; } else if ( selected_color.equals( "orange" ) ) { current_ball_color = Color.orange ; } else if ( selected_color.equals( "yellow" ) ) { current_ball_color = Color.yellow ; } else if ( selected_color.equals( "green" ) ) { current_ball_color = Color.green ; } else if ( selected_color.equals( "blue" ) ) { current_ball_color = Color.blue ; } else if ( selected_color.equals( "magenta" ) ) { current_ball_color = Color.magenta ; } else if ( selected_color.equals( "cyan" ) ) { current_ball_color = Color.cyan ; } else if ( selected_color.equals( "pink" ) ) { current_ball_color = Color.pink ; } else if ( selected_color.equals( "lightGray" ) ) { current_ball_color = Color.lightGray ; } 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 ) { graphics.setColor( current_ball_color ) ; graphics.fillOval( ball_center_point_x - 50, ball_center_point_y - 50, 100, 100 ) ; graphics.setColor( Color.black ) ; // The y coordinate for drawString() is different here // than in MovingBallApplet.java graphics.drawString( "(" + ball_center_point_x + ", " + ball_center_point_y + ")", 20, 50 ) ; graphics.drawOval( ball_center_point_x - 50, ball_center_point_y - 50, 100, 100 ) ; } public static void main( String[] command_line_arguments ) { MovingBallAWTApplication this_application_window = new MovingBallAWTApplication() ; this_application_window.setVisible( true ) ; } }