Stanford CS106A Section Assignment 2 Robot Face Solution

2 Nov

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>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s