Source Code:
/**
* RevFaceDrag uses a FunnyFace class to construct the face object. Illustrates
* the class declarations and the advantage of creating a class object, as
* opposed to the FaceDrag program, which did not define a class for the
* funny face.
*
* @author Lisa Ballesteros adapting JAEA
code
* @version 1.0
*/
import java.awt.*;
import objectdraw.*;
public class RevFaceDrag extends WindowController {
private static final double FACE_LEFT = 100;
private static final double FACE_TOP = 100;
private FunnyFace happy; // FunnyFace to be dragged
private Location lastPoint;
private boolean happyGrabbed = false; // Whether happy has been grabbed by the mouse
public void begin() { // Make the FunnyFace
happy = new FunnyFace( FACE_LEFT, FACE_TOP, canvas );
}
public void onMousePress( Location pressPoint ){
lastPoint = pressPoint;
happyGrabbed = happy.contains( pressPoint );
}
public void onMouseDrag( Location dragPoint ) {
if (happyGrabbed ) {
happy.move( dragPoint.getX() - lastPoint.getX(), dragPoint.getY() - lastPoint.getY());
lastPoint = dragPoint;
}
}
}
/**
* Creates a funny face object consisting of the head, mouth, and two eyes.
*
* Lisa Ballesteros adapted from JAEA
* @version (a version number or a date)
*/
import java.awt.*;
import objectdraw.*;
public class FunnyFace {
// head dimensions
private static final double FACE_WIDTH = 60;
private static final double FACE_HEIGHT = 60;
//mouth dimensions
private static final double MOUTH_WIDTH = FACE_WIDTH/2;
private static final double MOUTH_HEIGHT = 10;
// eye dimensions
private static final double EYE_OFFSET = 20;
private static final double EYE_RADIUS = 8;
//instance variables for face components
private FramedOval head;
private FramedOval mouth;
private FramedOval leftEye;
private FramedOval rightEye;
//constructor
public FunnyFace (double left, double top, DrawingCanvas faceCanvas){
head = new FramedOval(left, top, FACE_WIDTH, FACE_HEIGHT, faceCanvas);
mouth = new FramedOval(left+(FACE_WIDTH-MOUTH_WIDTH)/2, top+2*FACE_HEIGHT/3, MOUTH_WIDTH, MOUTH_HEIGHT, faceCanvas);
leftEye = new FramedOval(left+EYE_OFFSET-EYE_RADIUS/2, top+EYE_OFFSET, EYE_RADIUS, EYE_RADIUS, faceCanvas);
rightEye = new FramedOval(left+FACE_WIDTH-EYE_OFFSET/2, top+EYE_OFFSET, EYE_RADIUS, EYE_RADIUS, faceCanvas);
}
// method to move entire face by dx in x direction and dy in y direction
public void move (double dx, double dy){
head.move(dx,dy);
mouth.move(dx,dy);
leftEye.move(dx,dy);
rightEye.move(dx,dy);
}
// returns true when @param point is inside face
public boolean contains (Location point){
return head.contains(point);
}
}