/* Sample to show simple mouse event handling */

import java.applet.*;
import java.awt.*;

public class Event3 extends Applet {

// Define objects, data used within the applet

  String pFirst, pFont;
  Rectangle r1 = new Rectangle(0,0,0,0);

  boolean bMouse = false;
  boolean bPaint = false;
  boolean bRect  = false;
  int xStart, yStart, xEnd, yEnd;
  int dx, dy;
  int iThresh = 10;
  int iMode = 0;
  String sText = "";


/*--------------------------------------------------------*/
/* init method -                                          */
/*  this method performs layout and initialization of     */
/*  objects for the class.                                */
/*--------------------------------------------------------*/

  public void init() {


// Now show the panel (this is required)

    show();

  }

/*--------------------------------------------------------*/
/* paint method -                                         */
/*  this method performs repaint of the text using the    */
/*  specified font parameters.  The font style is         */
/*  determined from the checkbox group.  The font size is */
/*  determined from the size choice list.                 */
/*--------------------------------------------------------*/

  public void paint(Graphics g) {
    int iStyle = Font.BOLD;

    g.setFont(new Font("TimesNewRoman", iStyle, 18 ));
    if (bPaint) {
       bPaint = false;
       switch (iMode) {
       case 2:
          r1.move(xEnd-dx, yEnd-dy);
          break;
       case 3:
          r1.reshape(xEnd, r1.y, r1.width+(r1.x-xEnd), r1.height);
          break;
       case 4:
          r1.reshape(r1.x, r1.y, xEnd-r1.x, r1.height);
          break;
       case 5:
          r1.reshape(r1.x, yEnd, r1.width, r1.height+(r1.y-yEnd));
          break;
       case 6:
          r1.reshape(r1.x, r1.y, r1.width, yEnd-r1.y);
          break;
       default:
         break;
       } /* endswitch */
       
       g.drawRect(r1.x, r1.y, r1.width, r1.height);
    } 
    if (bRect) {
       bRect = false;
       g.drawRect(Math.min(xStart,xEnd),
                  Math.min(yStart,yEnd),
                  Math.max(xStart, xEnd) - Math.min(xStart, xEnd),
                  Math.max(yStart, yEnd) - Math.min(yStart, yEnd) );
    }
    else {
       if (r1.width > 0) {
         g.drawRect(r1.x, r1.y, r1.width, r1.height);
       }
    }
  }


/*---------------------------------------------------------*/
/* handle event method -                                   */
/*---------------------------------------------------------*/

  public boolean mouseDrag(Event e, int x, int y) {

       if ((e.modifiers & Event.META_MASK) == 0 ) {
        if ( iMode >= 2 ) {
          bPaint = true;
          xEnd = x;
          yEnd = y;
          repaint();
        }
       
        else {
         bRect = true;
         iMode = 1;
         xEnd = x;
         yEnd = y;
         repaint();
        }
       }
       return true;
  }

  public boolean mouseDown(Event e, int x, int y) {

    if (e.id == Event.MOUSE_DOWN) {
       xStart = e.x;
       yStart = e.y;
       if ( (e.modifiers & Event.META_MASK) == 0 )
         if ( r1.inside(xStart, yStart) ) {
          dx = xStart - r1.x;
          dy = yStart - r1.y;
          if (dx < iThresh) iMode = 3;
            else if ( (r1.width-dx) < iThresh ) iMode = 4;
            else if (dy < iThresh) iMode = 5;
            else if ( (r1.height-dy) < iThresh ) iMode = 6;
            else iMode = 2;
       }
    } 
    return true;
  }
    
  public boolean mouseUp(Event e, int x, int y) {

    if (iMode == 1) {
       iMode = 0;
       r1.reshape(Math.min(xStart,xEnd),
                  Math.min(yStart,yEnd),
                  Math.max(xStart, xEnd) - Math.min(xStart, xEnd),
                  Math.max(yStart, yEnd) - Math.min(yStart, yEnd) );
       repaint();

    } 
    iMode = 0;
    return true;
  }

}
