Problem written by Eric Roberts. Updated by Chris Piech and Lisa Yan.
Write a program that draws this picture. You should use the constants provided, but feel free to change them (or recolor any part of the face).
A few constants are provided. Use them so that you don't have "magic" numbers in your program.
/*The width of the robot face:*/
private static final int FACE_WIDTH = 300;
/*The height of the robot face:*/
private static final int FACE_HEIGHT = 350;
/*The diameter of each robot eye:*/
private static final int EYE_DIAMETER = 70;
/*The distance from the top of the head to the top of the eyes:*/
private static final int EYE_Y_OFFSET = 40;
/*The width of the mouth:*/
private static final int MOUTH_WIDTH = 150;
/*The height of the mouth:*/
private static final int MOUTH_HEIGHT = 30;
/*The distance from the top of the head to the top of the mouth:*/
private static final int MOUTH_Y_OFFSET = 200;
/*The distance from the top of the screen to the base of the label:*/
private static final int LABEL_Y = 50;
/**
* Robot Face
* ---------
* Draws an awesome robot face, with label and different colored
* eyes!!!@#$%#@!
*/
public class RobotFace extends GraphicsProgram {
private static final int FACE_WIDTH = 300;
private static final int FACE_HEIGHT = 350;
private static final int EYE_DIAMETER = 70;
private static final int EYE_Y_OFFSET = 40;
private static final int EYE_X_SEPARATION = 40;
private static final int MOUTH_WIDTH = 150;
private static final int MOUTH_HEIGHT = 30;
private static final int MOUTH_Y_OFFSET = 200;
private static final int LABEL_Y = 50;
public void run() {
drawHead();
drawLabel();
drawMouth();
drawEyes();
}
private void drawHead() {
double x = (getWidth() - FACE_WIDTH) / 2;
double y = (getHeight() - FACE_HEIGHT) / 2;
GRect head = new GRect(x, y, FACE_WIDTH, FACE_HEIGHT);
head.setFilled(true);
head.setFillColor(Color.YELLOW);
add(head);
}
private void drawEyes() {
double headY = (getHeight() - FACE_HEIGHT) / 2;
double eyeY = headY + EYE_Y_OFFSET;
double leftX = getWidth()/2 - EYE_X_SEPARATION/2 - EYE_DIAMETER;
GOval leftEye = new GOval(leftX, eyeY, EYE_DIAMETER, EYE_DIAMETER);
leftEye.setFilled(true);
leftEye.setFillColor(Color.BLUE);
add(leftEye);
double rightX = getWidth()/2 + EYE_X_SEPARATION/2;
GOval rightEye = new GOval(rightX, eyeY, EYE_DIAMETER, EYE_DIAMETER);
rightEye.setFilled(true);
rightEye.setFillColor(Color.GREEN);
add(rightEye);
}
private void drawMouth() {
double headX = (getWidth() - FACE_WIDTH) / 2;
double headY = (getHeight() - FACE_HEIGHT) / 2;
double mouthX = headX + (FACE_WIDTH - MOUTH_WIDTH) / 2;
double mouthY = headY + MOUTH_Y_OFFSET; // we did this!!!
GRect mouth = new GRect(mouthX, mouthY, MOUTH_WIDTH, MOUTH_HEIGHT);
mouth.setFilled(true);
add(mouth);
}
private void drawLabel() {
GLabel label = new GLabel("Robot Face");
label.setFont("Times New Roman-44");
double x = (getWidth() - label.getWidth()) / 2;
add(label, x, LABEL_Y);
}
}