// BallIntersectsRectangeApplet.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-09-24 File created. // 2006-09-24 Latest modification. // This program works like MovingBallWithMouseApplet.java. // There is an extra rectangle on the screen. The program will find // out if you drag a ball so that it intersects with the rectangle. import java.awt.* ; import java.awt.event.* ; import java.awt.geom.* ; public class BallIntersectsRectangleApplet extends java.applet.Applet implements MouseListener, MouseMotionListener { int applet_width, applet_height ; Ball first_ball, second_ball, third_ball ; Rectangle2D.Double rectangle_on_screen = new Rectangle2D.Double( 100, 150, 250, 100 ) ; Ball ball_being_moved = null ; Point previous_mouse_position ; public void init() { setBackground( Color.white ) ; // Let's get the size of the applet (defined in .html file) applet_width = getSize().width ; applet_height = getSize().height ; first_ball = new Ball( applet_width / 2 - 160, applet_height / 2 - 100, Color.red ) ; second_ball = new Ball( applet_width / 2 - 40, applet_height / 2 - 100, Color.green ) ; third_ball = new Ball( applet_width / 2 + 80, applet_height / 2 - 100, Color.blue ) ; addMouseListener(this); addMouseMotionListener(this); } // The following dummy methods are needed to satisfy listener // interfaces. public void mouseMoved( MouseEvent event ) {} public void mouseClicked( MouseEvent event ) {} public void mouseEntered( MouseEvent event ) {} public void mouseExited( MouseEvent event ) {} public void mousePressed( MouseEvent event ) { Point mouse_position = event.getPoint() ; if ( first_ball.contains_point( mouse_position ) ) { ball_being_moved = first_ball ; ball_being_moved.activate_ball() ; } else if ( second_ball.contains_point( mouse_position ) ) { ball_being_moved = second_ball ; ball_being_moved.activate_ball() ; } else if ( third_ball.contains_point( mouse_position ) ) { ball_being_moved = third_ball ; ball_being_moved.activate_ball() ; } previous_mouse_position = mouse_position ; repaint() ; } public void mouseReleased( MouseEvent event ) { if ( ball_being_moved != null ) { ball_being_moved.deactivate_ball() ; ball_being_moved = null ; repaint() ; } } public void mouseDragged( MouseEvent event ) { if ( ball_being_moved != null ) { Point new_mouse_position = event.getPoint() ; int mouse_movement_x = new_mouse_position.x - previous_mouse_position.x ; int mouse_movement_y = new_mouse_position.y - previous_mouse_position.y ; previous_mouse_position = new_mouse_position ; ball_being_moved.move_this_ball( mouse_movement_x, mouse_movement_y ) ; repaint() ; } } public void paint( Graphics graphics ) { Graphics2D graphics2D = (Graphics2D) graphics ; first_ball.draw( graphics ) ; second_ball.draw( graphics ) ; third_ball.draw( graphics ) ; graphics2D.setPaint( Color.cyan ) ; graphics2D.fill( rectangle_on_screen ) ; graphics2D.setPaint( Color.black ) ; graphics2D.draw( rectangle_on_screen ) ; if ( first_ball.intersects_rectangle( rectangle_on_screen ) ) { graphics.drawString( "first_ball intersects rectangle", 20, 20 ) ; } if ( second_ball.intersects_rectangle( rectangle_on_screen ) ) { graphics.drawString( "second_ball intersects rectangle", 20, 20 ) ; } if ( third_ball.intersects_rectangle( rectangle_on_screen ) ) { graphics.drawString( "third_ball intersects rectangle", 20, 20 ) ; } } }