// MovingBallManager.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2013-02-05 File created. // 2013-10-03 Last modification. // This program communicates with MovingBallServlet import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import java.io.* ; import java.net.* ; import java.awt.geom.* ; // The Ball class in this program is almost the same as the Ball class // in file Ball.java. The only difference between these two classes // is that the class below is Serializable. class Ball implements Serializable { int ball_center_point_x = 0 ; int ball_center_point_y = 0 ; Color ball_color = Color.red ; int ball_diameter = 100 ; boolean this_ball_is_activated = false ; public Ball( int given_center_point_x, int given_center_point_y, Color given_color ) { ball_center_point_x = given_center_point_x ; ball_center_point_y = given_center_point_y ; ball_color = given_color ; } public void activate_ball() { this_ball_is_activated = true ; } public void deactivate_ball() { this_ball_is_activated = false ; } public int get_ball_center_point_x() { return ball_center_point_x ; } public int get_ball_center_point_y() { return ball_center_point_y ; } public int get_ball_diameter() { return ball_diameter ; } public void move_right() { ball_center_point_x += 3 ; } public void move_left() { ball_center_point_x -= 3 ; } public void move_up() { ball_center_point_y -= 3 ; } public void move_down() { ball_center_point_y += 3 ; } public void move_this_ball( int movement_in_direction_x, int movement_in_direction_y ) { ball_center_point_x = ball_center_point_x + movement_in_direction_x ; ball_center_point_y = ball_center_point_y + movement_in_direction_y ; } public void move_to_position( int new_center_point_x, int new_center_point_y ) { ball_center_point_x = new_center_point_x ; ball_center_point_y = new_center_point_y ; } public void shrink() { // The if-construct ensures that the ball does not become // too small. if ( ball_diameter > 10 ) { ball_diameter -= 6 ; } } public void enlarge() { ball_diameter += 6 ; } public void set_diameter( int new_diameter ) { if ( new_diameter > 5 ) { ball_diameter = new_diameter ; } } public void set_color( Color new_color ) { ball_color = new_color ; } public boolean contains_point( Point given_point ) { int ball_radius = ball_diameter / 2 ; // Here we use the Pythagorean theorem to calculate the distance // from the given point to the center point of the ball. // See the note at the end of this file. int distance_from_given_point_to_ball_center = (int) Math.sqrt( Math.pow( ball_center_point_x - given_point.x, 2 ) + Math.pow( ball_center_point_y - given_point.y, 2 ) ) ; return ( distance_from_given_point_to_ball_center <= ball_radius ) ; } public boolean intersects_rectangle( Rectangle2D given_rectangle ) { Area this_ball_area = new Area( new Ellipse2D.Float( ball_center_point_x - ball_diameter / 2, ball_center_point_y - ball_diameter / 2, ball_diameter, ball_diameter ) ) ; return this_ball_area.intersects( given_rectangle ) ; } public void draw( Graphics graphics ) { graphics.setColor( ball_color ) ; graphics.fillOval( ball_center_point_x - ball_diameter / 2, ball_center_point_y - ball_diameter / 2, ball_diameter, ball_diameter ) ; graphics.setColor( Color.BLACK ) ; graphics.drawOval( ball_center_point_x - ball_diameter / 2, ball_center_point_y - ball_diameter / 2, ball_diameter, ball_diameter ) ; // If this ball is activated, it will have a thick black edge if ( this_ball_is_activated == true ) { graphics.drawOval( ball_center_point_x - ball_diameter / 2 + 1, ball_center_point_y - ball_diameter / 2 + 1, ball_diameter - 2, ball_diameter - 2 ) ; graphics.drawOval( ball_center_point_x - ball_diameter / 2 + 2, ball_center_point_y - ball_diameter / 2 + 2, ball_diameter - 4, ball_diameter - 4 ) ; } } } class MovingBallPanel extends JPanel implements ActionListener, ItemListener { JButton left_button, up_button, down_button, right_button ; int frame_width, frame_height ; Ball ball_on_screen ; public MovingBallPanel( int given_frame_width, int given_frame_height ) { frame_width = given_frame_width ; frame_height = given_frame_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( frame_width / 2, frame_height / 2, Color.RED ) ; 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 ) ; } /** * Get a connection to the servlet. */ private URLConnection get_servlet_connection() throws MalformedURLException, IOException { URL servlet_url = new URL( "http://localhost:8080/MovingBallServlet/") ; URLConnection url_connection = servlet_url.openConnection() ; url_connection.setDoInput( true ) ; url_connection.setDoOutput( true ) ; url_connection.setUseCaches( false ) ; url_connection.setRequestProperty( "Content-Type", "application/x-java-serialized-object") ; return url_connection ; } private void send_ball_to_servlet() { try { // send data to the servlet URLConnection url_connection = get_servlet_connection() ; OutputStream output_stream = url_connection.getOutputStream() ; ObjectOutputStream object_output_stream = new ObjectOutputStream( output_stream ) ; object_output_stream.writeObject( ball_on_screen ) ; object_output_stream.flush() ; object_output_stream.close() ; // receive result from servlet InputStream input_stream = url_connection.getInputStream() ; ObjectInputStream object_input_stream = new ObjectInputStream( input_stream ) ; String string_from_servlet = (String) object_input_stream.readObject() ; object_input_stream.close() ; input_stream.close() ; // show result System.out.print( "\n " + string_from_servlet ) ; } catch ( Exception exception ) { exception.printStackTrace() ; // exceptionArea.setText(ex.toString()); } } 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() ; } send_ball_to_servlet() ; 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 ) ; } send_ball_to_servlet() ; repaint() ; } public void paintComponent( Graphics graphics ) { // The superclass version of paintComponent() clears the // component area and does other necessary things. 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 ) ; } } class MovingBallPanelFrame extends JFrame { public static final int FRAME_WIDTH = 600 ; public static final int FRAME_HEIGHT = 480 ; public MovingBallPanelFrame() { setTitle( "B A L L M A N A G E R" ) ; setSize( FRAME_WIDTH, FRAME_HEIGHT ) ; MovingBallPanel moving_ball_panel = new MovingBallPanel( FRAME_WIDTH, FRAME_HEIGHT ) ; Container content_pane = getContentPane() ; content_pane.add( moving_ball_panel ) ; } } public class MovingBallManager { public static void main( String[] not_in_use ) { MovingBallPanelFrame moving_ball_frame = new MovingBallPanelFrame() ; moving_ball_frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; moving_ball_frame.setVisible( true ) ; } }