Skip to content

Instantly share code, notes, and snippets.

@kenechiokolo
Created March 23, 2015 16:55
Show Gist options
  • Save kenechiokolo/5ba3289b3002e9dc2c5c to your computer and use it in GitHub Desktop.
Save kenechiokolo/5ba3289b3002e9dc2c5c to your computer and use it in GitHub Desktop.
CS106A Section Assignment 2.2
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class robotFace extends GraphicsProgram {
private static final double HEAD_WIDTH = 200;
private static final double HEAD_HEIGHT = 350;
private static final double EYE_RADIUS = 30;
private static final double MOUTH_WIDTH = 110;
private static final double MOUTH_HEIGHT = 20;
public void run() {
// sets x so that head is in centre with respect to x-coordinates
x = getWidth() / 2 - HEAD_WIDTH / 2;
// sets y so that head is in centre with respect to y-coordinates
y = getHeight() / 2 - HEAD_HEIGHT / 2;
robotHead();
}
// creates and adds a robot head with origin (x,y)
private void robotHead() {
add(head());
eyes();
add(mouth());
}
// creates the robot head at coordinates (x,y)
private GRect head() {
GRect head = new GRect (x, y, HEAD_WIDTH, HEAD_HEIGHT);
head.setFilled(true);
head.setFillColor(Color.gray);
return head;
}
/* draws AND adds BOTH eyes to the head according to assignment specifications
* eyes anchored to x and y coordinates
*/
private void eyes() {
GOval leftEye = new GOval(x + (HEAD_WIDTH / 4) - EYE_RADIUS, y + (HEAD_HEIGHT / 4) - EYE_RADIUS, eyeDiameter, eyeDiameter);
leftEye.setFilled(true);
leftEye.setFillColor(Color.yellow);
add(leftEye);
GOval rightEye = new GOval(x + HEAD_WIDTH - (HEAD_WIDTH / 4) - EYE_RADIUS, y + (HEAD_HEIGHT / 4) - EYE_RADIUS, eyeDiameter, eyeDiameter);
rightEye.setFilled(true);
rightEye.setFillColor(Color.yellow);
add(rightEye);
}
/* draws the robot mouth according to assignment specifications
* mouth anchored to x and y coordinates
*/
private GRect mouth() {
GRect mouth = new GRect (x + (HEAD_WIDTH/2) - (MOUTH_WIDTH/2), (y + HEAD_HEIGHT - (HEAD_HEIGHT/4) - (MOUTH_HEIGHT/2)), MOUTH_WIDTH, MOUTH_HEIGHT);
mouth.setFilled(true);
mouth.setFillColor(Color.white);
return mouth;
}
// x and y specify origin of top left corner of robot face
double x = 0;
double y = 0;
double eyeDiameter = EYE_RADIUS*2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment