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.
Code java:
/*
* 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);
}
}
Re: Variable question from a beginner
Quote:
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.
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.
Code java:
/*
* 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.
Re: Variable question from a beginner
Quote:
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.