Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Confused on getWidth() / getHeight() for GCanvas as part of Hangman game

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confused on getWidth() / getHeight() for GCanvas as part of Hangman game

    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).
    /*
     * 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;
     
     
     
    }
    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.

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Confused on getWidth() / getHeight() for GCanvas as part of Hangman game

    Why are you using a third party package (acm) in place of the java SE classes? That will restrict who can offer help.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on getWidth() / getHeight() for GCanvas as part of Hangman game

    I didn't actually realize that this wasn't a standard package used for graphical work in Java. The acm package is used in the CS106A videos and other coursework whenever graphics are involved. I figured that the basic Java packages didn't include it. Now that I've googled ACM I see that it is a package designed for teaching first year CS students Java and that at least one member of the board responsible for the package is a professor at Stanford. Perhaps this class isn't as useful for learning Java as I had thought it would be...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Confused on getWidth() / getHeight() for GCanvas as part of Hangman game

    You may need to find a forum that works with the acm classes.
    There may be some here that know those packages. Wait a day and see if someone shows up. There are guys in other time zones that will be looking in a little later.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused on getWidth() / getHeight() for GCanvas as part of Hangman game

    Ok. Thanks for your quick responses. I will just read some of the tutorials around here while I wait to see if anyone who is familiar with the acm packages shows up

Similar Threads

  1. BorderLayout and getHeight/getWidth
    By DOLZero in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 08:59 AM
  2. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 02:35 PM
  3. Problem with getWidth() and getHeight()
    By dcwang3 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 2nd, 2011, 12:22 AM
  4. Replies: 1
    Last Post: April 26th, 2011, 08:47 AM
  5. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM

Tags for this Thread