import uwcse.graphics.*; // access the graphics utilities in the uw library import java.awt.Color; // access the Color class /** *
* Create a snow man in a graphics window *
* * @author your name here */ public class SnowMan { // Your instance fields go here private int x, y; private GWindow window; private double scale; /** * Create a snow man in at location (x,y) in the GWindow window. * * @param x * the x coordinate of the center of the head of the snow man * @param y * the y coordinate of the center of the head of the snow man * @scale the factor that multiplies all default dimensions for this snow * man (e.g. if the default head radius is 20, the head radius of * this snow man is scale * 20) * @window the graphics window this snow man belongs to */ public SnowMan(int x, int y, double scale, GWindow window) { // initialize the instance fields this.x = x; this.y = y; this.scale = scale; this.window = window; // Put the details of the drawing in a private method this.draw(); } /** Draw in the graphics window a snow man at location (x,y) */ private void draw() { // body of the snow man // width of the body int bw = (int) (80 * scale); // height of the body int bh = (int) (120 * scale); Oval body = new Oval(x - bw / 2, y - bh / 2, bw, bh, Color.WHITE, true); window.add(body); // head of the snow man int r = (int) (scale * 20); // radius int yHead = (int)(y - bh *.4 - 2 * r); Oval head = new Oval(x - r, yHead, 2 * r, 2 * r, Color.WHITE, true); window.add(head); // hat of the snowman Rectangle hat = new Rectangle(x - r, yHead - 2 * r, 2 * r, 2 * r, Color.BLACK, true); window.add(hat); // brim of the hat Rectangle brim = new Rectangle((int) (x - 1.2 * r), yHead, (int) (2.4 * r), (int) (0.2 * r), Color.RED, true); window.add(brim); } }