/* * File: HangmanCanvas.java * ------------------------ * This file keeps track of the Hangman display. */ import java.awt.Color; import acm.graphics.*; public class HangmanCanvas extends GCanvas { private double centerX = getWidth()/2; private double centerY = getHeight()/2; private static final int GAP = 100; private static String DISPLAY_FONT = "SansSerif-20"; /** Resets the display so that only the scaffold appears */ public void test() { // removeAll(); DrawScaffold(); DrawLeftArm(); DrawRightArm(); DrawHead(); DrawBody(); DrawRightLeg(); DrawLeftLeg(); displayWord("HEHEHEHEHEHEHEHEHEH"); } /** * Updates the word on the screen to correspond to the current * state of the game. The argument string shows what letters have * been guessed so far; unguessed letters are indicated by hyphens. */ public void displayWord(String word) { GLabel displayWord = new GLabel(word); displayWord.setFont(DISPLAY_FONT); displayWord.setLocation(centerX, centerY); add(displayWord); } /** * Updates the display to correspond to an incorrect guess by the * user. Calling this method causes the next body part to appear * on the scaffold and adds the letter to the list of incorrect * guesses that appears at the bottom of the window. */ public void noteIncorrectGuess(char letter) { /* You fill this in */ } private void DrawScaffold() { double scaffoldHeight = centerY - (BODY_LENGTH/2) - (2*HEAD_RADIUS) - ROPE_LENGTH; double scaffBeamConnX = centerX - BEAM_LENGTH; GLine scaffold = new GLine(scaffBeamConnX, scaffoldHeight, scaffBeamConnX, scaffoldHeight + SCAFFOLD_HEIGHT); GLine beam = new GLine(scaffBeamConnX, scaffoldHeight, centerX, scaffoldHeight); GLine rope = new GLine(centerX, scaffoldHeight, centerX, scaffoldHeight + ROPE_LENGTH); GRect base = new GRect(scaffBeamConnX - BASE_OFFSET, scaffoldHeight + SCAFFOLD_HEIGHT, BASE_WIDTH, BASE_HEIGHT); base.setFillColor(Color.BLACK); base.setFilled(true); add(scaffold); add(beam); add(rope); add(base); } private void DrawHead() { double scaffoldHeight = centerY - (BODY_LENGTH/2) - (2*HEAD_RADIUS) - ROPE_LENGTH; GOval head = new GOval(centerX - HEAD_RADIUS, scaffoldHeight + ROPE_LENGTH, 2*HEAD_RADIUS, 2*HEAD_RADIUS); add(head); } private void DrawBody() { GLine body = new GLine(centerX, centerY - BODY_LENGTH/2, centerX, centerY + BODY_LENGTH/2); add(body); } private void DrawLeftArm() { double armHeightY = centerY - BODY_LENGTH/2 + ARM_OFFSET_FROM_HEAD; double elbowJointX = centerX - UPPER_ARM_LENGTH; GLine leftArm = new GLine(centerX, armHeightY, elbowJointX, armHeightY); GLine leftHand = new GLine(elbowJointX, armHeightY, elbowJointX, armHeightY + LOWER_ARM_LENGTH); add(leftArm); add(leftHand); } private void DrawRightArm() { double armHeightY = centerY - BODY_LENGTH/2 + ARM_OFFSET_FROM_HEAD; double elbowJointX = centerX + UPPER_ARM_LENGTH; GLine rightArm = new GLine(centerX, armHeightY, elbowJointX, armHeightY); GLine rightHand = new GLine(elbowJointX, armHeightY, elbowJointX, armHeightY + LOWER_ARM_LENGTH); add(rightArm); add(rightHand); } private void DrawLeftLeg() { double legHeightY = centerY + BODY_LENGTH/4; double hipJointX = centerX - HIP_WIDTH/4; double footJointY = legHeightY + LEG_LENGTH; GLine leftHip = new GLine(centerX, legHeightY, hipJointX, legHeightY); GLine leftLeg = new GLine(hipJointX, legHeightY, hipJointX, legHeightY + LEG_LENGTH); GLine leftFoot = new GLine(hipJointX, footJointY, hipJointX - FOOT_LENGTH, footJointY); add(leftHip); add(leftLeg); add(leftFoot); } private void DrawRightLeg() { double legHeightY = centerY + BODY_LENGTH/2; double hipJointX = centerX + HIP_WIDTH/2; double footJointY = legHeightY + LEG_LENGTH; GLine rightHip = new GLine(centerX, legHeightY, hipJointX, legHeightY); GLine rightLeg = new GLine(hipJointX, legHeightY, hipJointX, legHeightY + LEG_LENGTH); GLine rightFoot = new GLine(hipJointX, footJointY, hipJointX + FOOT_LENGTH, footJointY); add(rightHip); add(rightLeg); add(rightFoot); } private static final int BASE_OFFSET = 40; private static final int BASE_WIDTH = 250; private static final int BASE_HEIGHT = 50; /* Constants for the simple version of the picture (in pixels) */ private static final int SCAFFOLD_HEIGHT = 360; private static final int BEAM_LENGTH = 144; private static final int ROPE_LENGTH = 18; private static final int HEAD_RADIUS = 36; private static final int BODY_LENGTH = 144; private static final int ARM_OFFSET_FROM_HEAD = 28; private static final int UPPER_ARM_LENGTH = 72; private static final int LOWER_ARM_LENGTH = 44; private static final int HIP_WIDTH = 36; private static final int LEG_LENGTH = 108; private static final int FOOT_LENGTH = 28; }