// Fig. 10.21: Drag.java // Using the MouseMotionAdapter class. import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Drag extends Applet { private int xValue = -10, yValue = -10; public void init() { addMouseMotionListener( new MotionHandler( this ) ); } public void paint( Graphics g ) { g.drawString( "Drag the mouse to draw", 10, 20 ); g.fillOval( xValue, yValue, 4, 4 ); } // Override Component class update method to allow all ovals // to remain on the screen by not clearing the background. public void update( Graphics g ) { paint( g ); } // set the drawing coordinates and repaint public void setCoordinates( int x, int y ) { xValue = x; yValue = y; repaint(); } } // Class to handle only mouse drag events for the Drag applet class MotionHandler extends MouseMotionAdapter { private Drag dragger; public MotionHandler( Drag d ) { dragger = d; } public void mouseDragged( MouseEvent e ) { dragger.setCoordinates( e.getX(), e.getY() ); } }