Created
November 3, 2011 00:00
-
-
Save NatashaTheRobot/1335354 to your computer and use it in GitHub Desktop.
This is the solution to the Robot Face problem from Section Assignment 2 of the Stanford CS106A Introduction to Programming Methodology Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Author
NatashaTheRobot
commented
Jul 29, 2012
via email
I think I did this problem wrong. At the time, I thought the radius was
actually the diameter, so I was dividing it by 2 to get the radius. So my
solution is not correct.
On Sat, Jul 28, 2012 at 12:32 AM, Raffay < ***@***.*** > wrote:
Hi, very neat code, just one question, why are you dividing eye radius by 2
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1335354
##
natasha murashev | natashamurashev.com | 415-577-5206 | @natashamurashev
Hi, thank you for your reply. i not sure if you could help me out on this or not but i thought i should ask you. i am following the art and science of java book and in the third chapter there is a problem 10 which requires you to centre the tic tac toe board on the screen and use one named constant board_size. i am not getting this right and have searched the net but could not find a solution. any idea how to solve it, i could centre a house but not this…
cheers
Raffay
…On 29-Jul-2012, at 5:05 AM, NatashaTheRobot ***@***.*** wrote:
I think I did this problem wrong. At the time, I thought the radius was
actually the diameter, so I was dividing it by 2 to get the radius. So my
solution is not correct.
On Sat, Jul 28, 2012 at 12:32 AM, Raffay <
***@***.***
> wrote:
>
> Hi, very neat code, just one question, why are you dividing eye radius by 2
> ---
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/1335354
##
natasha murashev | natashamurashev.com | 415-577-5206 | @natashamurashev
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1335354
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment