Posted on Leave a comment

RobotFace solution

[code lang=”java”]
/*
* File: RobotFace.java
* ———————
* This class is a blank one that you can change at will. Remember, if you change
* the class name, you’ll need to change the filename so that it matches.
* Then you can extend GraphicsProgram, ConsoleProgram, or DialogProgram as you like.
*/

import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class RobotFace extends GraphicsProgram {
private static final int HEAD_WIDTH=265;
private static final int HEAD_HEIGHT=400;
private static final int EYE_RADIUS=50;
private static final int MOUTH_WIDTH=200;
private static final int MOUTH_HEIGHT=50;

public void run() {
int middle_x = getWidth()/2;
int middle_y = getHeight()/2;

//Head
GRect myHead = new GRect(middle_x-HEAD_WIDTH/2,middle_y-HEAD_HEIGHT/2,HEAD_WIDTH, HEAD_HEIGHT);
myHead.setFilled(true);
myHead.setFillColor(Color.GRAY);
add(myHead);

//Eyes

GOval leftEye = new GOval(middle_x-HEAD_WIDTH/4-EYE_RADIUS/2, middle_y-HEAD_HEIGHT/4, EYE_RADIUS, EYE_RADIUS);
leftEye.setFilled(true);
leftEye.setFillColor(Color.YELLOW);
add(leftEye);

GOval rightEye = new GOval(middle_x+HEAD_WIDTH/4-EYE_RADIUS/2, middle_y-HEAD_HEIGHT/4, EYE_RADIUS, EYE_RADIUS);
rightEye.setFilled(true);
rightEye.setFillColor(Color.YELLOW);
add(rightEye);

//Mouth

GRect mouth = new GRect(middle_x-MOUTH_WIDTH/2,middle_y+HEAD_HEIGHT/4,MOUTH_WIDTH,MOUTH_HEIGHT);
mouth.setFilled(true);
mouth.setFillColor(Color.WHITE);
add(mouth);
}
}

[/code]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.