Posted on Leave a comment

RobotFace with Methods

[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;
int leftEye_x = middle_x-HEAD_WIDTH/4-EYE_RADIUS/2;
int rightEye_x = middle_x+HEAD_WIDTH/4-EYE_RADIUS/2;

add(Head(middle_x,middle_y));
addEyes(leftEye_x,rightEye_x,middle_y);
add(Mouth(middle_x,middle_y));

}

//Head method
private GRect Head(int x,int y){
//Head
GRect myHead = new GRect(x-HEAD_WIDTH/2,y-HEAD_HEIGHT/2,HEAD_WIDTH, HEAD_HEIGHT);
myHead.setFilled(true);
myHead.setFillColor(Color.GRAY);
return myHead;

}

//Eyes method
private void addEyes( int left_eye,int right_eye ,int y){
//add left Eye
add(Eye(left_eye,y));

//add right eye
add(Eye(right_eye,y));

}

//Mouth method
private GRect Mouth(int x,int y){
GRect mouth = new GRect(x-MOUTH_WIDTH/2,y+HEAD_HEIGHT/4,MOUTH_WIDTH,MOUTH_HEIGHT);
mouth.setFilled(true);
mouth.setFillColor(Color.WHITE);
return mouth;

}

//individual Eye method
private GOval Eye(int x,int y){
//Eyes

GOval myEye = new GOval(x, y-HEAD_HEIGHT/4, EYE_RADIUS, EYE_RADIUS);
myEye.setFilled(true);
myEye.setFillColor(Color.YELLOW);
return myEye;

}

}

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *

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