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 4 of 4

Thread: Variable question from a beginner

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Variable question from a beginner

    Hello all, this is my first post and thanks for any and all replies. I am teaching myself Java from books/tutorials and videos on the internet. I have a fundamental question; In the following code, is it possible to declare the variables (canvasWidth & canvasHeight)
    at the beginning, so as to declare them just one time. All this program does is print a target in the center of the screen. I have commented out where I attempted to do this, but the sub-methods (?) did not recognize the variables. I realize this is Java 101, but I am very new to Java and still trying to understand the basics. Once again, thanks for all replies.

    /*
    * File: Target.java
    * Name: 
    * Section Leader: 
    * -----------------
    * This file is the starter file for the Target problem.
    */
     
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
    import acm.graphics.GCanvas.*;
    //	This program places a target in the center of the screen.
     
    public class Target extends GraphicsProgram {	
     
    public void run() {
    //	 double canvasWidth = getWidth();
    //	 double canvasHeight = getHeight();
    //	 System.out.println ("width: "+canvasWidth+" Height: "+canvasHeight);
    outerRing();
    midRing();
    bullsEye();
    }
     
    //Places outer ring of target
     
    public void outerRing() {
    double canvasWidth = getWidth();
    double canvasHeight = getHeight();
    GOval outerRing = new GOval((canvasWidth/2-36), (canvasHeight/2-36), 72, 72);
    outerRing.setColor (Color.red);
    outerRing.setFillColor(Color.red);
    outerRing.setFilled(true);
    GRectangle bounds = outerRing.getBounds();
    //	 println ("outerRing bounds: " + bounds);
    add (outerRing);	
    }
     
    //Places next ring of target	
     
    public void midRing() {
    double canvasWidth = getWidth();
    double canvasHeight = getHeight();
    GOval midRing = new GOval((canvasWidth/2)-(36*.65), (canvasHeight/2-(36*.65)), 72*.65, 72*.65);
    midRing.setColor (Color.white);
    midRing.setFillColor(Color.white);
    midRing.setFilled(true);
    add (midRing);	
    }
     
    // Places bullseye on target	
     
    public void bullsEye() {
    double canvasWidth = getWidth();
    double canvasHeight = getHeight();
    GOval bullsEye = new GOval((canvasWidth/2)-(36*.3), (canvasHeight/2-(36*.3)), 72*.3, 72*.3);
    bullsEye.setColor (Color.red);
    bullsEye.setFillColor(Color.red);
    bullsEye.setFilled(true);
    add (bullsEye);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Variable question from a beginner

    the sub-methods (?) did not recognize the variables.
    Where are the variables declared as class variables?
    variables defined in a method are only known in that method.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Variable question from a beginner

    When I declare them under class, as shown below, the target is placed in the upper left hand corner.
    /*
     * File: Target.java
     * Name: 
     * Section Leader: 
     * -----------------
     * This file is the starter file for the Target problem.
     */
     
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
    import acm.graphics.GCanvas.*;
    //	This program places a target in the center of the screen.
     
    public class Target extends GraphicsProgram {	
     
    	double canvasWidth = getWidth();
    	double canvasHeight = getHeight();
     
    	public void run() {
    //		double canvasWidth = getWidth();
    //		double canvasHeight = getHeight();
    //		System.out.println ("width: "+canvasWidth+" Height: "+canvasHeight);
    		outerRing();
    		midRing();
    		bullsEye();
    	}
     
    //Places outer ring of target
     
    	public void outerRing() {
    //		double canvasWidth = getWidth();
    //		double canvasHeight = getHeight();
    		GOval outerRing = new GOval((canvasWidth/2-36), (canvasHeight/2-36), 72, 72);
    		outerRing.setColor (Color.red);
    		outerRing.setFillColor(Color.red);
    		outerRing.setFilled(true);
    		GRectangle bounds = outerRing.getBounds();
    //		println ("outerRing bounds: " + bounds);
    		add (outerRing);		
    	}
     
    //Places next ring of target	
     
    	public void midRing() {
    //		double canvasWidth = getWidth();
    //		double canvasHeight = getHeight();
    		GOval midRing = new GOval((canvasWidth/2)-(36*.65), (canvasHeight/2-(36*.65)), 72*.65, 72*.65);
    		midRing.setColor (Color.white);
    		midRing.setFillColor(Color.white);
    		midRing.setFilled(true);
    		add (midRing);	
    	}
     
    // Places bullseye on target	
     
    	public void bullsEye() {
    //		double canvasWidth = getWidth();
    //		double canvasHeight = getHeight();
    		GOval bullsEye = new GOval((canvasWidth/2)-(36*.3), (canvasHeight/2-(36*.3)), 72*.3, 72*.3);
    		bullsEye.setColor (Color.red);
    		bullsEye.setFillColor(Color.red);
    		bullsEye.setFilled(true);
    		add (bullsEye);
    	}
    }

    I have commented out the original placement of the declarations.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Variable question from a beginner

    the target is placed in the upper left hand corner.
    Are you saying that the values of the variables are 0?

    When does the getWidth() method have a useful value that can be assigned to the variable?

    The code should wait until there is a useful value before assigning it to the variable. I'm not familiar with the acm package classes and do not know when the value will be available. Add lots of printlns to see when the value is there. When there is a value, then save it in the class variables.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 03:57 PM
  2. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  3. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  4. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM
  5. beginner's question
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2010, 04:02 PM