import java.awt.*; import java.awt.event.*; public class MouseClickDistanceFrame extends Frame { private static final int FrameWidth = 400; private static final int FrameHeight = 400; private Label clickDistanceLabel; private int startingX; private int startingY; private int endingX; private int endingY; public MouseClickDistanceFrame() { setTitle( "I'll measure the distance between your clicks..." ); setSize ( FrameWidth, FrameHeight ); clickDistanceLabel = new Label( "You have not clicked yet." ); add( "North", clickDistanceLabel ); startingX = 0; startingY = 0; endingX = 0; endingY = 0; addMouseListener( new MouseClickDistance() ); } public void paint( Graphics g ) { Color originalColor = g.getColor(); g.setColor( Color.blue ); g.drawLine( startingX, startingY, endingX, endingY ); g.setColor( originalColor ); } protected void update( double newDistance ) { clickDistanceLabel.setText( "Distance = " + newDistance ); repaint(); } private class MouseClickDistance implements MouseListener { public void mousePressed( MouseEvent e ) { startingX = e.getX(); startingY = e.getY(); } public void mouseReleased( MouseEvent e ) { endingX = e.getX(); endingY = e.getY(); double distanceBetweenDownAndUp = Math.sqrt( Math.pow( endingX - startingX, 2) + Math.pow( endingY - startingY, 2) ); update( distanceBetweenDownAndUp ); } public void mouseClicked ( MouseEvent e ) {} public void mouseEntered ( MouseEvent e ) {} public void mouseExited ( MouseEvent e ) {} } }