We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I found an answer to my original question here (https://forum.processing.org/one/topic/setting-a-variable-to-width-2-or-height-2.html) but now I have some other questions
1) what's the difference between having int x = 1; at the beginning and in setup()?
2) does int x; (not having a value) mean int x=0?
Thanks for your help.
Answers
Both width & height only got a valid value after size() is called! :-B
Hi GoToLoop Thanks so as I understand now no matter you write int = width after size(); Processing will still run int first? ( because I thought it's executed line by line..)
in this example the ellipse won't be in the middle
This below is the proper way to do things. Notice width and height are variables from the PApplet and they are only init after size() gets executed. To be safe, you access those values within any functions provided to your sketch.
More info: https://processing.org/reference/size_.html
Kf
What you tried to do here:
won't work. Anything not inside a method will be executed when the class is instantiated, regardless of where it is in the actual file. So those two lines in the middle will run before setup and width and height will be zero.
Even though x & y are placed between setup() & draw(), they're outside any functions.
So, when variables are declared outside functions and curly
{}
blocks, they're called fields.They're executed even before their class' constructor! @-)
Also, it doesn't matter whether draw() is typed in before setup() or not; b/c they're called back by sketch's "Animation Thread". B-)
I thought they would be 100 rather than 0?
thanks!
thanks all
another question- does int x; mean int x=0;?
Variables are always initialized by Java if you don't do it yourself. Reference to objects are set to null until you call the constructor using the new keyword.
I prefer the practice of initializing everything instead of something doing it for you. A better practice is to init everything in setup but it is more of an user preference.
So in your question, x is set to zero. You could also use
println(x)
:-BKf
Yes for fields. But not for local variables. :-B
Go to "Default Values" section in order to know field's initial default values for all Java datatypes:
http://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
I printed x I can see in the console it's 50? I guess cuz the default size is 100x100?
thanks guys!
println(DEFAULT_WIDTH, DEFAULT_HEIGHT);
are the initial values for width & height before size().