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); } }