// MovingBallWithMouseApplet.java (c) 2006 - 2009 Kari Laitinen // http://www.naturalprogramming.com // 2006-09-21 File created. // 2009-10-30 This is now a JPanel-based applet // 2009-11-01 Latest modification. /* This applet draws three Ball objects onto the screen. It is possible to move these balls by dragging them with the mouse. The Ball class provides a method with which it is possible to find out whether a clicked point is inside the ball. In addition the Ball class provides methods for moving the ball. During compilation the file Ball.java must be in the same folder (directory) with this file. */ import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; class MovingBallWithMousePanel extends JPanel implements MouseListener, MouseMotionListener { int panel_width, panel_height ; Ball first_ball, second_ball, third_ball ; Ball ball_being_moved = null ; Point previous_mouse_position ; public MovingBallWithMousePanel( int given_width, int given_height ) { panel_width = given_width ; panel_height = given_height ; first_ball = new Ball( panel_width / 2 - 150, panel_height / 2, Color.red ) ; second_ball = new Ball( panel_width / 2, panel_height / 2, Color.green ) ; third_ball = new Ball( panel_width / 2 + 150, panel_height / 2, 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 paintComponent( Graphics graphics ) { // The superclass version of paintComponent() must be called first. super.paintComponent( graphics ) ; first_ball.draw( graphics ) ; second_ball.draw( graphics ) ; third_ball.draw( graphics ) ; } } public class MovingBallWithMouseApplet extends JApplet { public void init() { getContentPane().add( new MovingBallWithMousePanel( getSize().width, getSize().height ) ) ; } }