// MovingBallApplet.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2001-03-31 File created. // 2011-12-01 Latest modification. // This program is a Java applet that draws a ball // onto the screen. There is also a "control panel" which provides // buttons to move the ball and a possibility to change the // color of the ball. // It is possible to make this program somewhat shorter // and better if Java Development Kit 1.7 (Java 7) is used. // Such a version of this program is provided in comments // at the end of this file. import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; public class MovingBallApplet extends JApplet implements ActionListener, ItemListener { JButton left_button, up_button, down_button, right_button ; int applet_width, applet_height ; int ball_center_point_x, ball_center_point_y ; Color current_ball_color = Color.red ; public void init() { setLayout( new BorderLayout() ) ; JPanel operations_panel = new JPanel() ; 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 ) ; 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 ) ; // Let's get the size of the applet (defined in .html file) applet_width = getSize().width ; applet_height = getSize().height ; ball_center_point_x = applet_width / 2 ; ball_center_point_y = applet_height / 2 ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { 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 paint( Graphics graphics ) { // The superclass version of paint() method draws the buttons // and the color selection menu to the screen. super.paint( graphics ) ; graphics.setColor( current_ball_color ) ; graphics.fillOval( ball_center_point_x - 50, ball_center_point_y - 50, 100, 100 ) ; graphics.setColor( Color.black ) ; graphics.drawString( "(" + ball_center_point_x + ", " + ball_center_point_y + ")", 20, 20 ) ; graphics.drawOval( ball_center_point_x - 50, ball_center_point_y - 50, 100, 100 ) ; } } /******* // The following is a better version of this program, // to be compiled with JDK 1.7 import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; public class MovingBallApplet extends JApplet implements ActionListener, ItemListener { JButton left_button, up_button, down_button, right_button ; int applet_width, applet_height ; int ball_center_point_x, ball_center_point_y ; Color current_ball_color = Color.red ; String[] selectable_color_names = { "red", "orange", "yellow", "green", "blue", "magenta", "cyan", "pink", "lightGray" } ; JComboBox color_selection_menu = new JComboBox( selectable_color_names ) ; Color[] selectable_colors = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN, Color.PINK, Color.LIGHT_GRAY } ; public void init() { setLayout( new BorderLayout() ) ; JPanel operations_panel = new JPanel() ; 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 ) ; operations_panel.add( left_button ) ; operations_panel.add( up_button ) ; operations_panel.add( down_button ) ; operations_panel.add( right_button ) ; color_selection_menu.addItemListener( this ) ; operations_panel.add( color_selection_menu ) ; add( "South", operations_panel ) ; // Let's get the size of the applet (defined in .html file) applet_width = getSize().width ; applet_height = getSize().height ; ball_center_point_x = applet_width / 2 ; ball_center_point_y = applet_height / 2 ; } public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JButton ) { 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 ) { int index_of_selected_color = color_selection_menu.getSelectedIndex() ; current_ball_color = selectable_colors[ index_of_selected_color ] ; repaint() ; } public void paint( Graphics graphics ) { // The superclass version of paint() method draws the buttons // and the color selection menu to the screen. super.paint( graphics ) ; graphics.setColor( current_ball_color ) ; graphics.fillOval( ball_center_point_x - 50, ball_center_point_y - 50, 100, 100 ) ; graphics.setColor( Color.black ) ; graphics.drawString( "(" + ball_center_point_x + ", " + ball_center_point_y + ")", 20, 20 ) ; graphics.drawOval( ball_center_point_x - 50, ball_center_point_y - 50, 100, 100 ) ; } } ****/