Stanford CS106A Section Assignment 2 Robot Face Solution

I have mixed feelings about Graphics programs. They are pretty simple, but time consuming in terms of figuring out the correct coordinates for squares, circles, and words. Here is the section assignment for a graphics program:

Here are the solutions:

To be honest, the solution provided on the class site, seems pretty confusing and not something I would have come up with on my own. Basically, they center the face at width cx, and height cy, in which case that is the center. Then the rest of the face items are configured in relation to the face center.


In my solution below (and on gist), I simply draw one face item at a time.

<pre>import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class DrawFace extends GraphicsProgram {

	private static final int head_width = 100;

	private static final int head_height = 200;

	private static final int eye_radius = 20;

	private static final int mouth_width = 50;

	private static final int mouth_height = 20;

	public void run() {
		buildHead();
		buildLeftEye();
		buildRightEye();
		buildMouth();
	}
	private void buildHead() {
		double startingWidth = ((getWidth()/2) - (head_width/2));
		double startingHeight = ((getHeight()/2) - (head_height/2));
		GRect face = new GRect (startingWidth, startingHeight, head_width, head_height);
		add(face);
		face.setFilled(true);
		face.setFillColor(Color.GRAY);
	}
	private void buildLeftEye() {
		double startingWidth = getWidth()/2 - head_width/4 - eye_radius/2;
		double startingHeight = getHeight()/2 - head_height/4 - eye_radius/2;
		GOval leftEye = new GOval (startingWidth, startingHeight, eye_radius, eye_radius);
		add(leftEye);
		leftEye.setColor(Color.YELLOW);
		leftEye.setFilled(true);
		leftEye.setFillColor(Color.YELLOW);
	}
	private void buildRightEye() {
		double startingWidth = getWidth()/2 + head_width/4 - eye_radius/2;
		double startingHeight = getHeight()/2 - head_height/4 - eye_radius/2;
		GOval rightEye = new GOval (startingWidth, startingHeight, eye_radius, eye_radius);
		add(rightEye);
		rightEye.setColor(Color.YELLOW);
		rightEye.setFilled(true);
		rightEye.setFillColor(Color.YELLOW);
	}
	private void buildMouth() {
		double startingWidth = getWidth()/2 - mouth_width/2;
		double startingHeight = getHeight()/2 + head_height/4 - mouth_height/2;
		GRect mouth = new GRect (startingWidth, startingHeight, mouth_width, mouth_height);
		add(mouth);
		mouth.setColor(Color.WHITE);
		mouth.setFilled(true);
		mouth.setFillColor(Color.WHITE);
	}
}</pre>

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.

  • Nathan

    yeah I don’t like graphics programs either. Mathematically I did it right but if you launch it you’ll see that the mouth is not centered correctly. I initially chose to plot the head first then move other objects in relation to the head’s starting x, y point.

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

    public class DefaultGraphics extends GraphicsProgram{

    public void run(){
    double HEAD_WIDTH = 0.3*getWidth();
    double HEAD_HEIGHT = 0.7*getHeight();
    double EYE_RADIUS = 0.2*HEAD_WIDTH;
    double MOUTH_WIDTH = 0.6*HEAD_WIDTH;
    double MOUTH_HEIGHT = 0.3*HEAD_WIDTH;

    double startY = getHeight()/2;
    double startX = getWidth()/2;
    double HEADX=startX-HEAD_WIDTH/2;
    double HEADY=startY-HEAD_HEIGHT/2;

    //** Plot Head
    GRect head = new GRect( startX-(.15*getWidth()), .15*getHeight(), 0.3*getWidth(), .7*getHeight() );
    fillObj(head, Color.gray);
    add(head);

    //** Plot Eyes
    GOval eye1 = new GOval(HEADX+.25*HEAD_WIDTH, startY-.25*HEAD_WIDTH, EYE_RADIUS, EYE_RADIUS);
    fillObj(eye1, Color.YELLOW);
    add(eye1);
    GOval eye2 = new GOval((HEADX+.75*HEAD_WIDTH)-EYE_RADIUS, startY-.25*HEAD_WIDTH, EYE_RADIUS, EYE_RADIUS);
    fillObj(eye2, Color.YELLOW);
    add(eye2);

    //** Plot Mouth
    GRect mouth = new GRect(HEADX+.2*HEAD_WIDTH, HEADY+(.75*HEAD_HEIGHT), MOUTH_WIDTH, MOUTH_HEIGHT);
    fillObj(mouth, Color.WHITE);
    add(mouth);

    }
    public void fillObj(GFillable a, Color argC){
    a.setFillColor(argC);
    a.setFilled(true);
    }

    }