// Ex12.27 of D&D 3rd ed.  write a program using methods from
//interface MouseListener that allows the user to press
//the mouse button, drag the mouse and release the mouse button.  
//When the mouse is released, draw a rectangle with the 
//appropriate upper-left corner, width and height(Hint:
//The mousePressed method should capture the set of
//coordinates at which the user presses and holds the
//mouse button initially, and the mouseReleased method 
//should capture the set of coordinates at which the user
//releases the mouse button.  Both methods should store the
//appropriate coordinate values.  All calculations of the width, 
//height and upper-left corner shoulc be performed by the paint 
//method before the shape is drawn.
//if mouseDragged performs same as mouseReleased->rubber-band effect
//for Ex12.28 As the user drags the mouse you can see what the rectangle
//will be when the mouse is released.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class DDtwelve27 extends JFrame {
  private int xValue= -10, yValue = -10 ;
  private int x0Value= -10, y0Value = -10;

public DDtwelve27() {
  super(" making a rectangle ");
  getContentPane().add(  
    new Label("Drag the mouse for a rectangle"), BorderLayout.SOUTH);

addMouseListener(
  new MouseAdapter() {
  public void mousePressed( MouseEvent e) {
    x0Value = e.getX();
    y0Value = e.getY();   
      }
  } 
); 
  


  addMouseMotionListener(
     new MouseMotionAdapter() {
       public void mouseDragged( MouseEvent e ) {
          xValue = e.getX();
          yValue = e.getY();
	  repaint();
        }
      }
   );

 setSize( 300, 150 );
 show();
}

public void paint( Graphics g ) {
//   g.fillOval(xValue, yValue, 4, 4); }
  g.fillRect( x0Value, y0Value, xValue - x0Value, yValue - y0Value); }
public static void main (String args[] ) {   
  DDtwelve27  app = new DDtwelve27();
  app.addWindowListener(
   new WindowAdapter() {
    public void windowClosing( WindowEvent e ) { System.exit(0 ) ;}
                   }
                );
          }
}



