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; /** * Draws a smiling face in a graphics window */ public SmilingFace(int x, int y, double scale, GWindow window) { // Initialize the instance fields this.x = x; // x(instance field) = x(local variable) this.y = y; this.scale = scale; this.window = window; // Draw the face in the window this.drawFace(); } /** * Draws the face in the graphics window */ private void drawFace() { // The head (a circle: default radius = 50) int radius = (int) (50 * scale); Oval head = new Oval(x - radius, y - radius, 2 * radius, 2 * radius, Color.YELLOW, true); window.add(head); // The mouth (use drawMouth) this.drawMouth(x, (int) (y + 0.9 * radius)); // The eyes (use drawEye) // left // right // The nose (use drawNose) // Show the face } /** * Draws 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 } /** * Draws 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 } /** * Draws 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) // The yellow circle is on top of the black circle and slightly shifted // up int r = (int) (0.8 * 50 * scale); Oval blackCircle = new Oval(mouthx - r, mouthy - 2 * r, 2 * r, 2 * r, Color.BLACK, true); window.add(blackCircle); Oval yellowCircle = new Oval(mouthx - r, mouthy - 2 * r, 2 * r, 2 * r, Color.YELLOW, true); yellowCircle.moveBy(0, (int) (-0.2 * r)); // don't write (int) -0.2 * r // since it is always 0! window.add(yellowCircle); } }