import uwcse.graphics.*; import java.awt.Color; /** * A smiling face in a graphics window */ public class SmilingFace { // The graphics window private GWindow window; // Location of the face private int x, y; // Scale used to draw the face private double scale; /** * Draw a smiling face in a graphics window * * @param x * the x coordinate of the center of the face * @param y * the y coordinate of the center of the face * @param scale * the multiplication factor for all default sizes * @param window * the graphics window where to draw */ public SmilingFace(int x, int y, double scale, GWindow window) { // Initialize the instance fields this.x = x; this.y = y; this.scale = scale; this.window = window; // Draw the face in the window this.drawFace(); // using this is optional since no confusion is // possible } /** * Draw the face in the graphics window */ private void drawFace() { // The head (a circle: default radius = 50) // (int) casts a value to an int // (int) 3.9 is 3 // (int) 3.2 is 3 // (int) -1.2 is -1 int r = (int) (50 * scale); Oval head = new Oval(x - r, y - r, 2 * r, 2 * r, Color.YELLOW, true); window.add(head); // The mouth (use drawMouth) this.drawMouth(x, (int) (y + 0.8 * r)); // The eyes (use drawEye) // left // right // The nose (use drawNose) // Show the face } /** * Draw an eye * * @param eyex * the x coordinate of the center of the eye * @param eyey * the y coordinate of the center of the eye */ private void drawEye(int eyex, int eyey) { // A black circle in a white oval } /** * Draw a nose * * @param nosex * the x coordinate of the top point of the nose * @param nosey * the y coordinate of the top point of the nose */ private void drawNose(int nosex, int nosey) { // A nose is a triangle } /** * Draw a mouth * * @param mouthx * the x coordinate of the middle bottom point of the mouth * @param mouthy * the y coordinate of the middle bottom point of the mouth */ private void drawMouth(int mouthx, int mouthy) { // Draw two circles (one black and one yellow) int r = (int) (40 * scale); Oval blackCircle = new Oval(mouthx - r, mouthy - 2 * r, 2 * r, 2 * r, Color.BLACK, true); Oval yellowCircle = new Oval(mouthx - r, mouthy - 2 * r, 2 * r, 2 * r, Color.YELLOW, true); // The yellow circle is on top of the black circle and slightly shifted // up yellowCircle.moveBy(0, -(int) (0.2 * r)); window.add(blackCircle); window.add(yellowCircle); } }