import java.applet.*; import java.awt.*; /** The SelfPortrait applet CS 170 - Homework 2 @author Stephen Gilbert @version 1.0, July 24, 2002 */ public class SelfPortrait extends Applet { // Instance variables for positioning private int HEAD_HEIGHT = 200; private int HEAD_WIDTH = 125; private int X = 15; private int Y = 15; // Eyes centered 1/3 of the way across the face private int LEFT_EYE = X + HEAD_WIDTH / 3; private int RIGHT_EYE = X + HEAD_WIDTH / 3 * 2; private int EYE_Y = Y + HEAD_HEIGHT / 3; private Color skinColor = Color.pink; private Color beardColor = new Color(128, 128, 128); private Color eyeColor = new Color(64, 128, 64); public void paint(Graphics g) { drawHead(g); drawEye(LEFT_EYE, EYE_Y, g); drawEye(RIGHT_EYE, EYE_Y, g); drawNose(g); drawMouth(g); drawEars(g); } /** * Displays a nicely shaped head, * along with the beard and the hair * Uses the fillRoundRect() and setColor() methods */ void drawHead(Graphics g) { int xArc = HEAD_WIDTH / 2; int yArc = HEAD_HEIGHT / 2; // Draw the beard and hair in dark gray Color beardColor = new Color(96, 96, 96); g.setColor(beardColor); g.fillRoundRect(X, Y, HEAD_WIDTH, HEAD_HEIGHT, xArc, yArc); // Now, draw the face in skin color int hairOffset = 5; // Size of the hair int faceX = X + hairOffset; // Some space for hair int faceY = Y + hairOffset; int beardHeight = HEAD_HEIGHT / 4; int faceWidth = HEAD_WIDTH - hairOffset * 2; int faceHeight = HEAD_HEIGHT - hairOffset * 2 - beardHeight; g.setColor(skinColor); g.fillRoundRect(faceX, faceY, faceWidth, faceHeight, xArc, yArc); } /** * Draws an eye, passed x, and y */ void drawEye(int x, int y, Graphics g) { // Let's calcualate the eye size int glassesWidth = HEAD_WIDTH / 3; // 1/3 of head int glassesHeight = glassesWidth * 2 / 3; // 2/3 of width int eyeSize = glassesHeight / 2; // eyball size // Start by drawing the glasses g.setColor(Color.black); g.fillOval( x - glassesWidth / 2, y - glassesHeight / 2, glassesWidth, glassesHeight); // Fill in the inside with skin color int frameThickness = 3; g.setColor(skinColor); g.fillOval( x - glassesWidth / 2 + frameThickness, y - glassesHeight / 2 + frameThickness, glassesWidth - frameThickness * 2, glassesHeight - frameThickness * 2); // Draw the "whites" of the eyes g.setColor(Color.white); int startArc = 0; int degrees = 180; g.fillArc( x - eyeSize, y - eyeSize / 2 + 1, eyeSize * 2, eyeSize + 2, startArc, degrees); // Now draw the eyeballs g.setColor(eyeColor); g.fillOval( x - eyeSize / 2, y - eyeSize / 2, eyeSize, eyeSize); // Now draw the pupils int pupilWidth = 4; g.setColor(Color.black); g.fillOval( x- pupilWidth / 2, y - pupilWidth / 2, pupilWidth, pupilWidth); // Now draw the lids and lashes // Draw the "whites" of the eyes g.setColor(Color.black); g.drawArc( x - eyeSize, y - eyeSize / 2 + 1, eyeSize * 2, eyeSize + 2, 0, 180); } /** * Draws the nose * Uses the Color darker() method, along with the * Graphics setColor(), fillOval(), and fillRoundRect() * methods. */ void drawNose(Graphics g) { int noseHeight = HEAD_HEIGHT / 3; int noseWidth = HEAD_WIDTH / 10; int y = Y + HEAD_HEIGHT / 4; int x = X + HEAD_WIDTH / 2 - noseWidth / 2; // Draw the nose shadow in a darker color Color noseColor = skinColor.darker(); g.setColor(noseColor); // Nose & nostrils on either side g.fillOval( x, y, noseWidth, noseHeight); g.fillOval( x - noseWidth, y + noseHeight - noseWidth, noseWidth * 3 / 2, noseWidth); g.fillOval( x + noseWidth / 2, y + noseHeight - noseWidth, noseWidth * 3 / 2, noseWidth); // Draw a highlight down the center of the nose g.setColor(skinColor); int highlightWidth = noseWidth / 3; g.fillRoundRect( x + highlightWidth, y + noseWidth, highlightWidth, noseHeight - noseWidth - 2, noseWidth, noseWidth); } /** * Draws the ears * Uses Color darker() and the Graphics * fillArc() method */ void drawEars(Graphics g) { int earHeight = HEAD_HEIGHT / 4; int earWidth = earHeight / 3; g.setColor( skinColor.darker() ); // Left ear int startArc = 90; int degrees = 180; g.fillArc( X - earWidth / 2, Y + HEAD_HEIGHT / 2 - earHeight / 2, earWidth, earHeight, startArc, degrees); // Right ear g.fillArc( X + HEAD_WIDTH - earWidth / 2, Y + HEAD_HEIGHT / 2 - earHeight / 2, earWidth, earHeight, -startArc, degrees); } /** * Draws the Mouth */ void drawMouth(Graphics g) { int mouthTop = Y + HEAD_HEIGHT / 2; int mouthWidth = HEAD_WIDTH / 2; int mouthHeight = HEAD_HEIGHT / 5; // Some teeth int teethX = X + HEAD_WIDTH / 4; int startArc = 0; int degrees = -180; g.setColor(Color.white); g.fillArc(teethX, mouthTop, mouthWidth, mouthHeight, startArc, degrees); // The moustache g.setColor(Color.darkGray); startArc = 0; degrees = -130; g.fillArc( X , mouthTop, mouthWidth, mouthHeight, startArc, degrees); startArc = 180; degrees = 130; g.fillArc( X + mouthWidth, mouthTop, mouthWidth, mouthHeight, startArc, degrees); } }