FaceDrag Applet


Your browser is ignoring the <APPLET> tag!


 

Source Code:

 

/**

 * Drag one face around the canvas.  Illustrates event-driven program

 * via mouse dragging.

 *

 * Lisa Ballesteros' adaptation of JEA code

 * @version 1.0

 */

 

 

import java.awt.*;

import objectdraw.*;

 

public class FaceDrag extends WindowController{

 

   // head dimensions

  private static final double FACE_LEFT = 100;

  private static final double FACE_TOP = 100;

  private static final double FACE_WIDTH = 60;

  private static final double FACE_HEIGHT = 60;

  //mouth dimensions

  private static final double MOUTH_LEFT = FACE_LEFT + FACE_WIDTH/4;

  private static final double MOUTH_TOP = FACE_TOP + 3*(FACE_HEIGHT/4);

  private static final double MOUTH_WIDTH = FACE_WIDTH/2;

  private static final double MOUTH_HEIGHT = FACE_WIDTH/8;

  // eye dimensions

  private static final double LEFT_EYE_LEFT = FACE_LEFT + FACE_WIDTH/4;

  private static final double RIGHT_EYE_LEFT = FACE_LEFT + 3*(FACE_WIDTH/4);

  private static final double EYE_TOP = FACE_TOP + FACE_HEIGHT/4;

  private static final double EYE_WIDTH = FACE_WIDTH/8;

  private static final double EYE_HEIGHT = EYE_WIDTH;

  //instance variables for face components

  private FramedOval head, mouth, leftEye, rightEye;

  private  Location lastPoint;  //point where mouse was last seen

  // to store whether mouse click is inside boundary of face

  private boolean faceGrabbed = false;

 

  // create a face using 4 FramedOval objects

  public void begin() {

     head = new FramedOval(FACE_LEFT, FACE_TOP, FACE_WIDTH, FACE_HEIGHT, canvas);

     mouth = new FramedOval(MOUTH_LEFT, MOUTH_TOP, MOUTH_WIDTH, MOUTH_HEIGHT, canvas);

     leftEye = new FramedOval(LEFT_EYE_LEFT, EYE_TOP, EYE_WIDTH, EYE_HEIGHT, canvas);

     rightEye = new FramedOval(RIGHT_EYE_LEFT, EYE_TOP, EYE_WIDTH, EYE_HEIGHT, canvas);

    }

    // indicate whether mouse is on face and save location info

    public void onMousePress (Location pressPoint){

        lastPoint = pressPoint;

        faceGrabbed = head.contains(pressPoint);

    }

    // if mouse was pressed inside face, move each component to the new location

    public void onMouseDrag( Location dragPoint){

        if (faceGrabbed) {

            head.move( dragPoint.getX() - lastPoint.getX(), dragPoint.getY() - lastPoint.getY());

            leftEye.move( dragPoint.getX() - lastPoint.getX(), dragPoint.getY() - lastPoint.getY());

            rightEye.move( dragPoint.getX() - lastPoint.getX(), dragPoint.getY() - lastPoint.getY());

            mouth.move( dragPoint.getX() - lastPoint.getX(), dragPoint.getY() - lastPoint.getY());

            lastPoint = dragPoint;

        }

    }

}