java - How do you make variables made inside a method available to all other methods in a class? -
i'm coding newb , want learn more java. i'm taking introductory programming course in high school , trying play around outside of class. program i'm creating supposed print out few instructions , questions in console, want retrieve user input using scanner , variable. goal of program user able create 'drawing' based on pure instinct, drawing displayed after fact.
here's problem. i'm asking user , storing information in variables native main method, need use information in paintcomponent method.
i tried adding static in front of variable such as: static int emptyrectw1 = kboard.nextint();
shows error "illegal modifier parametic emptyrectw1; final permitted."
i knew long shot trying out, that's best i've got.
i'm using java eclipse ide. help, looks way learn mess , have else point out mistake! :)
here's code:
import java.util.scanner; import java.awt.graphics; import java.awt.color; import java.awt.container; import javax.swing.jframe; import javax.swing.jpanel; public class blinddrawing extends jpanel { public static void main (string[] args) { scanner kboard = new scanner(system.in); system.out.println("welcome blind drawing game!"); system.out.println("in game, asked questions how construct image using rectangles , ovals. asked shape's dimensions , position. origin (0,0) top left corner."); system.out.println("first you're making empty rectangle, how wide want be?"); int emptyrectw1 = kboard.nextint(); system.out.println("what height?"); int emptyrecth1 = kboard.nextint(); } public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawrect(0,0,emptyrectw1, emptyrecth1); } }
actually, variable declared inside method local variable.
please extend scope of the 2 variables before main method. see code below.
static int emptyrectw1, emptyrecth1; public static void main (string[] args) {
then manipulate latter code avoid duplicate variable declaration shown below.
emptyrectw1 = kboard.nextint(); system.out.println("what height?"); emptyrecth1 = kboard.nextint();
i hope helps.
Comments
Post a Comment