-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathRobot.java
More file actions
73 lines (63 loc) · 2.19 KB
/
Robot.java
File metadata and controls
73 lines (63 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Importing graphics program.
import acm.program.GraphicsProgram;
// Import for colours.
import java.awt.Color;
// Import for rectangles.
import acm.graphics.GRect;
public class Robot extends GraphicsProgram {
// Some useful constants
private static final int FACE_LENGTH = 300;
private static final int FACE_WIDTH = 400;
private static final int EYE_SIZE = 50;
private static final int NECK_WIDTH = 100;
private static final int NECK_HEIGHT = 300;
private static final int MOUTH_WIDTH = 100;
private static final int MOUTH_HEIGHT = 20;
private static final int MOUTH_OFFSET = 70;
public void run() {
drawRobot();
}
// This problem can be divided into 4 sub-problems.
private void drawRobot() {
drawNeck();
drawFace();
drawEyes();
drawMouth();
}
// This method draws the face of the robot. Robot has gray rectangle face.
private void drawFace() {
GRect face = new GRect(getWidth() / 2 - FACE_WIDTH / 2, getHeight() / 2 - FACE_LENGTH / 2, FACE_WIDTH,
FACE_LENGTH);
face.setFilled(true);
face.setFillColor(Color.LIGHT_GRAY);
add(face);
}
// This method draws the eyes of the robot. Robot has round black eyes.
private void drawEyes() {
// Lets create one universal method in order to not repeat the code.
drawOneEye(getWidth() / 2 - FACE_WIDTH / 3);
drawOneEye(getWidth() / 2 + FACE_WIDTH / 3 - EYE_SIZE);
}
// This method takes the eye width as a parameter and draws black eye with that width.
private void drawOneEye(int width) {
GRect eye = new GRect(width, getHeight() / 2 - FACE_LENGTH / 6, EYE_SIZE, EYE_SIZE);
eye.setFilled(true);
eye.setColor(Color.BLACK);
add(eye);
}
// This method draws the mouth of the robot. Robot has black rectangle mouth.
private void drawMouth() {
GRect mouth = new GRect(getWidth() / 2 - MOUTH_WIDTH / 2, getHeight() / 2 + MOUTH_OFFSET,
MOUTH_WIDTH, MOUTH_HEIGHT);
mouth.setFilled(true);
mouth.setColor(Color.BLACK);
add(mouth);
}
// This method draws the neck of the robot. Robot has gray rectangle neck.
private void drawNeck() {
GRect neck = new GRect(getWidth() / 2 - NECK_WIDTH / 2, getHeight() - NECK_HEIGHT, NECK_WIDTH, NECK_HEIGHT);
neck.setFilled(true);
neck.setFillColor(Color.LIGHT_GRAY);
add(neck);
}
}