This first part is not necessarily related to my problem, but is just a brief explanation of why I am looking for help from this community.
First, I have done a little programming in C++ (nothing complex at all) but realized that I wrote absolutely horrendous code. I decided to expand my knowledge of C++ and eventually came across the CS106A (intro to Java programming / software engineering) course taught at Stanford University. I’m not actually enrolled at Stanford so I am just following along with the lecture series as posted to YouTube and attempting the course work posted on the class website. Since I am not a Stanford student I do not have access to the professor during office hours or any other TAs. I would greatly appreciate any help!
Onto the problem :
I am attempting to write a program to play Hangman in Java composed of one main source file and two additional source files. The main file is a console program which implements the basics of the Hangman game. It chooses a secret word, allows the user to guess a letter, and has some reaction based on whether the guess is in the word or not. The second file is used to choose a secret word from some dictionary. In the third file, I am attempting to draw a graphical representation of the game as part of a GCanvas (relevant code included below).
I need to center the middle of the body around the center of the GCanvas in the x-direction and offset by some value from the center in the y-direction. I am using getWidth() and getHeight() to try and calculate the center coordinates of the GCanvas. I’ve used these methods previously, although not in any program that included both a graphical and console portion, to center graphics successfully. In this code I find that getWidth() returns 0 and getHeight() returns 125 which is definitely not the answer I expect. Can someone explain to me, or point me to an explanation, of why I am getting such odd values back? Also, when I try to use getWidth() and getHeight() outside of a function (and just inside the class) I get a return value of 0 for both getWidth() and getHeight(). If someone could explain why that is to me as well I would appreciate it./* * File: HangmanCanvas.java * ------------------------ * This file keeps track of the Hangman display. */ import acm.graphics.*; public class HangmanCanvas extends GCanvas { /** Resets the display so that only the scaffold appears */ public void reset() { addScaffolding(); /* Draws the scaffolding */ while(true) { } } /** Draws the scaffolding without any body parts */ private void addScaffolding() { /* Additional constants for adding the picture (in pixels) */ final int CANVAS_Y_CENTER = getHeight() / 2; final int CANVAS_X_CENTER = getWidth() / 2; /* Scaffold beam */ final int SCAFFOLD_BASE_Y_OFFSET = -100; /* Y offset from the center line for the base of the scaffolding */ final int SCAFFOLD_BASE_X_COORD = CANVAS_X_CENTER - BEAM_LENGTH; final int SCAFFOLD_BASE_Y_COORD = CANVAS_Y_CENTER + SCAFFOLD_BASE_Y_OFFSET; /* Scaffold arm */ final int SCAFFOLD_ARM_LEFT_X_COORD = SCAFFOLD_BASE_X_COORD; final int SCAFFOLD_ARM_Y_COORD = SCAFFOLD_BASE_Y_COORD - BEAM_LENGTH; /* Scaffold rope */ final int ROPE_X_COORD = CANVAS_X_CENTER; final int ROPE_TOP_Y_COORD = SCAFFOLD_BASE_Y_COORD - BEAM_LENGTH; GLine scaffold = new GLine(SCAFFOLD_BASE_X_COORD, SCAFFOLD_BASE_Y_COORD, SCAFFOLD_BASE_X_COORD, SCAFFOLD_BASE_Y_COORD + BEAM_LENGTH); GLine beam = new GLine(SCAFFOLD_ARM_LEFT_X_COORD, SCAFFOLD_ARM_Y_COORD, SCAFFOLD_ARM_LEFT_X_COORD + BEAM_LENGTH, SCAFFOLD_ARM_Y_COORD); GLine rope = new GLine(ROPE_X_COORD, ROPE_TOP_Y_COORD, ROPE_X_COORD, ROPE_TOP_Y_COORD + ROPE_LENGTH); add(scaffold); add(beam); add(rope); } /* 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; }
This is the main source code which I use to actually play the game and call any methods associated with the display part of the game from. I removed any code that actually plays the game because I don’t think it is relevant to my problem. I can post the entire source if necessary however.
/* * File: Hangman.java * ------------------ * This program will eventually play the Hangman game from * Assignment #4. */ import acm.graphics.*; import acm.program.*; import acm.util.*; import java.awt.*; public class Hangman extends ConsoleProgram { /* Define instance variables */ /** Sets up the HangmanCanvas to add graphics to the game */ private HangmanCanvas canvas; public void run() { init(); canvas.reset(); /* Reset the canvas so only the scaffolding appears */ /** OTHER CODE TO IMPLEMENT THE GAME NOT SHOWN */ } /** Initializes the HangmanCanvas to display graphics for the game. */ public void init() { canvas = new HangmanCanvas(); add(canvas); } } }
I have tried looking at the Java documentation from the Oracle website to get more information on getWidth() and getHeight() (navigating from this page Java Documentation), but it does not really mean a lot to me at this point. I also attempted to add a mouseMotionEvent to my program so that I could get what the coordinates of the center should be roughly. I didn't get that to work successfully, but it seems a little unnecessary at this point since one of the return values is 0 which doesn't seem to make any sense.
Thanks in advance for your help!