// MovingBallOOApplet.java (c) 2001 - 2009 Kari Laitinen // http://www.naturalprogramming.com // 2005-06-07 File created. // 2009-10-30 This is now a JPanel-based applet // 2009-11-01 Latest modification. // This program is an object-oriented version of program // MovingBallApplet.java. // This one uses a class named Ball that is declared in its // own source program file. import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; class MovingBallOOPanel extends JPanel implements ActionListener, ItemListener { JButton left_button, up_button, down_button, right_button ; int panel_width, panel_height ; Ball ball_on_screen ; public MovingBallOOPanel( int given_width, int given_height ) { panel_width = given_width ; panel_height = given_height ; // Let's create the Ball object so that the ball is in the // middle of the screen at the beginning. ball_on_screen = new Ball( panel_width / 2, panel_height / 2, Color.RED ) ; setLayout( new BorderLayout() ) ; left_button = new JButton( " < " ) ; up_button = new JButton( " Up " ) ; down_button = new JButton( " Down " ) ; right_button = new JButton( " > " ) ; left_button.addActionListener( this ) ; up_button.addActionListener( this ) ; down_button.addActionListener( this ) ; right_button.addActionListener( this ) ; JPanel operations_panel = new JPanel() ; operations_panel.add( left_button ) ; operations_panel.add( up_button ) ; operations_panel.add( down_button ) ; operations_panel.add( right_button ) ; JComboBox color_selection_menu = new JComboBox() ; 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 ) ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { if ( event.getSource() == left_button ) { ball_on_screen.move_left() ; } else if ( event.getSource() == up_button ) { ball_on_screen.move_up() ; } else if ( event.getSource() == down_button ) { ball_on_screen.move_down() ; } else if ( event.getSource() == right_button ) { ball_on_screen.move_right() ; } repaint() ; } } public void itemStateChanged( ItemEvent menu_selection ) { String selected_color = (String) menu_selection.getItem() ; if ( selected_color.equals( "red" ) ) { ball_on_screen.set_color( Color.red ) ; } else if ( selected_color.equals( "orange" ) ) { ball_on_screen.set_color( Color.orange ) ; } else if ( selected_color.equals( "yellow" ) ) { ball_on_screen.set_color( Color.yellow ) ; } else if ( selected_color.equals( "green" ) ) { ball_on_screen.set_color( Color.green ) ; } else if ( selected_color.equals( "blue" ) ) { ball_on_screen.set_color( Color.blue ) ; } else if ( selected_color.equals( "magenta" ) ) { ball_on_screen.set_color( Color.magenta ) ; } else if ( selected_color.equals( "cyan" ) ) { ball_on_screen.set_color( Color.cyan ) ; } else if ( selected_color.equals( "pink" ) ) { ball_on_screen.set_color( Color.pink ) ; } else if ( selected_color.equals( "lightGray" ) ) { ball_on_screen.set_color( Color.lightGray ) ; } repaint() ; } public void paintComponent( Graphics graphics ) { // The superclass version of paintComponent() method draws the buttons // and the color selection menu to the screen. super.paintComponent( graphics ) ; ball_on_screen.draw( graphics ) ; graphics.setColor( Color.black ) ; graphics.drawString( "(" + ball_on_screen.get_ball_center_point_x() + ", " + ball_on_screen.get_ball_center_point_y() + ")", 20, 20 ) ; } } public class MovingBallOOApplet extends JApplet { public void init() { getContentPane().add( new MovingBallOOPanel( getSize().width, getSize().height ) ) ; } }